001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.fix;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.CoreException;
017:
018: import org.eclipse.jdt.core.dom.CompilationUnit;
019:
020: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
021: import org.eclipse.jdt.internal.corext.fix.IFix;
022: import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFix;
023:
024: public class VariableDeclarationCleanUp extends AbstractCleanUp {
025:
026: public VariableDeclarationCleanUp(Map options) {
027: super (options);
028: }
029:
030: public VariableDeclarationCleanUp() {
031: super ();
032: }
033:
034: /**
035: * {@inheritDoc}
036: */
037: public CleanUpRequirements getRequirements() {
038: return new CleanUpRequirements(requireAST(), false, null);
039: }
040:
041: private boolean requireAST() {
042: boolean addFinal = isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL);
043: if (!addFinal)
044: return false;
045:
046: return isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS)
047: || isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS)
048: || isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES);
049: }
050:
051: /**
052: * {@inheritDoc}
053: */
054: public IFix createFix(CleanUpContext context) throws CoreException {
055: CompilationUnit compilationUnit = context.getAST();
056: if (compilationUnit == null)
057: return null;
058:
059: boolean addFinal = isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL);
060: if (!addFinal)
061: return null;
062:
063: return VariableDeclarationFix
064: .createCleanUp(
065: compilationUnit,
066: isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS),
067: isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS),
068: isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES));
069: }
070:
071: /**
072: * {@inheritDoc}
073: */
074: public String[] getDescriptions() {
075: List result = new ArrayList();
076: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL)
077: && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS))
078: result
079: .add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalField_description);
080: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL)
081: && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS))
082: result
083: .add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalParameters_description);
084: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL)
085: && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES))
086: result
087: .add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalLocals_description);
088:
089: return (String[]) result.toArray(new String[result.size()]);
090: }
091:
092: /**
093: * {@inheritDoc}
094: */
095: public String getPreview() {
096: StringBuffer buf = new StringBuffer();
097:
098: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL)
099: && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS)) {
100: buf.append("private final int i= 0;\n"); //$NON-NLS-1$
101: } else {
102: buf.append("private int i= 0;\n"); //$NON-NLS-1$
103: }
104: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL)
105: && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS)) {
106: buf.append("public void foo(final int j) {\n"); //$NON-NLS-1$
107: } else {
108: buf.append("public void foo(int j) {\n"); //$NON-NLS-1$
109: }
110: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL)
111: && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES)) {
112: buf.append(" final int k;\n"); //$NON-NLS-1$
113: buf.append(" int h;\n"); //$NON-NLS-1$
114: buf.append(" h= 0;\n"); //$NON-NLS-1$
115: } else {
116: buf.append(" int k, h;\n"); //$NON-NLS-1$
117: buf.append(" h= 0;\n"); //$NON-NLS-1$
118: }
119: buf.append("}\n"); //$NON-NLS-1$
120:
121: return buf.toString();
122: }
123:
124: }
|