Prototypical implementation of new AST modification strategy
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java
index e30a3f4..493d58f 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java
@@ -38,14 +38,24 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IOpenable;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.ui.SharedASTProvider;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.text.edits.MalformedTreeException;
+import org.eclipse.text.edits.TextEdit;
public class ASTutilsUI {
@@ -74,26 +84,23 @@
public static String insertNewBundleRef(IDocument document,
IResource resource, int startPos, int endPos,
String resourceBundleId, String key) {
- String newName = null;
+ boolean createRBReference = false;
String reference = "";
CompilationUnit cu = getCompilationUnit(resource);
-
- if (cu == null) {
- return null;
- }
+ AST ast = cu.getAST();
+ ASTRewrite rewriter = ASTRewrite.create(ast);
String variableName = ASTutils.resolveRBReferenceVar(document,
resource, startPos, resourceBundleId, cu);
if (variableName == null) {
- newName = ASTutils.getNonExistingRBRefName(resourceBundleId,
- document, cu);
+ variableName = ASTutils.getNonExistingRBRefName(resourceBundleId, cu);
+ createRBReference = true;
}
- try {
+ //try {
reference = ASTutils.createResourceReference(resourceBundleId, key,
- null, resource, startPos, variableName == null ? newName
- : variableName, document, cu);
+ null, resource, startPos, variableName, ast, rewriter, cu);
if (startPos > 0 && document.get().charAt(startPos - 1) == '\"') {
startPos--;
@@ -110,64 +117,86 @@
endPos--;
}
- document.replace(startPos, endPos, reference);
+ //document.replace(startPos, endPos, reference);
// create non-internationalisation-comment
- ASTutils.createReplaceNonInternationalisationComment(cu, document,
- startPos);
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
+ //ASTutils.createReplaceNonInternationalisationComment(cu, document,
+ // startPos);
+ //} catch (BadLocationException e) {
+ // e.printStackTrace();
+ //}
- if (variableName == null) {
- // refresh reference to the shared AST of the loaded CompilationUnit
- cu = getCompilationUnit(resource);
-
+ if (createRBReference) {
ASTutils.createResourceBundleReference(resource, startPos,
- document, resourceBundleId, null, true, newName, cu);
- // createReplaceNonInternationalisationComment(cu, document, pos);
+ document, resourceBundleId, null, true, variableName, cu, ast, rewriter);
}
+ // computation of the text edits
+ TextEdit edits = rewriter.rewriteAST(document, null);
+
+ // computation of the new source code
+ try {
+ edits.apply(document);
+ } catch (MalformedTreeException e) {
+ Logger.logError(e);
+ } catch (BadLocationException e) {
+ Logger.logError(e);
+ }
+
+
return reference;
}
public static String insertExistingBundleRef(IDocument document,
IResource resource, int offset, int length,
String resourceBundleId, String key, Locale locale) {
+ boolean createRBReference = false;
String reference = "";
- String newName = null;
CompilationUnit cu = getCompilationUnit(resource);
+ AST ast = cu.getAST();
+ ASTRewrite rewriter = ASTRewrite.create(ast);
String variableName = ASTutils.resolveRBReferenceVar(document,
resource, offset, resourceBundleId, cu);
if (variableName == null) {
- newName = ASTutils.getNonExistingRBRefName(resourceBundleId,
- document, cu);
+ variableName = ASTutils.getNonExistingRBRefName(resourceBundleId, cu);
+ createRBReference = true;
}
- try {
- reference = ASTutils.createResourceReference(resourceBundleId, key,
- locale, resource, offset, variableName == null ? newName
- : variableName, document, cu);
+ reference = ASTutils.createResourceReference(resourceBundleId, key,
+ locale, resource, offset, variableName, ast, rewriter, cu);
- document.replace(offset, length, reference);
-
- // create non-internationalisation-comment
- ASTutils.createReplaceNonInternationalisationComment(cu, document,
- offset);
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
+ //try {
+ // document.replace(offset, length, reference);
+ //} catch (BadLocationException e) {
+ // Logger.logError(e);
+ // return null;
+ //}
+ // create non-internationalisation-comment
+ //ASTutils.createReplaceNonInternationalisationComment(cu, document,
+ // offset);
// TODO retrieve cu in the same way as in createResourceReference
// the current version does not parse method bodies
- if (variableName == null) {
+ if (createRBReference) {
ASTutils.createResourceBundleReference(resource, offset, document,
- resourceBundleId, locale, true, newName, cu);
- // createReplaceNonInternationalisationComment(cu, document, pos);
+ resourceBundleId, locale, true, variableName, cu, ast, rewriter);
}
+
+ // computation of the text edits
+ TextEdit edits = rewriter.rewriteAST(document, null);
+
+ // computation of the new source code
+ try {
+ edits.apply(document);
+ } catch (MalformedTreeException e) {
+ Logger.logError(e);
+ } catch (BadLocationException e) {
+ Logger.logError(e);
+ }
+
return reference;
}
diff --git a/org.eclipse.babel.tapiji.tools.java/src/org/eclipse/babel/tapiji/tools/java/util/ASTutils.java b/org.eclipse.babel.tapiji.tools.java/src/org/eclipse/babel/tapiji/tools/java/util/ASTutils.java
index 25479d2..4b53a01 100644
--- a/org.eclipse.babel.tapiji.tools.java/src/org/eclipse/babel/tapiji/tools/java/util/ASTutils.java
+++ b/org.eclipse.babel.tapiji.tools.java/src/org/eclipse/babel/tapiji/tools/java/util/ASTutils.java
@@ -21,6 +21,7 @@
import org.eclipse.babel.tapiji.tools.core.Logger;
import org.eclipse.babel.tapiji.tools.core.model.SLLocation;
import org.eclipse.babel.tapiji.tools.java.visitor.MethodParameterDescriptor;
+import org.eclipse.core.internal.registry.OffsetTable;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
@@ -58,1017 +59,985 @@
public class ASTutils {
- private static MethodParameterDescriptor rbDefinition;
- private static MethodParameterDescriptor rbAccessor;
+ private static MethodParameterDescriptor rbDefinition;
+ private static MethodParameterDescriptor rbAccessor;
- public static MethodParameterDescriptor getRBDefinitionDesc() {
- if (rbDefinition == null) {
- // Init descriptor for Resource-Bundle-Definition
- List<String> definition = new ArrayList<String>();
- definition.add("getBundle");
- rbDefinition = new MethodParameterDescriptor(definition,
- "java.util.ResourceBundle", true, 0);
- }
+ public static MethodParameterDescriptor getRBDefinitionDesc() {
+ if (rbDefinition == null) {
+ // Init descriptor for Resource-Bundle-Definition
+ List<String> definition = new ArrayList<String>();
+ definition.add("getBundle");
+ rbDefinition = new MethodParameterDescriptor(definition,
+ "java.util.ResourceBundle", true, 0);
+ }
- return rbDefinition;
- }
+ return rbDefinition;
+ }
- public static MethodParameterDescriptor getRBAccessorDesc() {
- if (rbAccessor == null) {
- // Init descriptor for Resource-Bundle-Accessors
- List<String> accessors = new ArrayList<String>();
- accessors.add("getString");
- accessors.add("getStringArray");
- rbAccessor = new MethodParameterDescriptor(accessors,
- "java.util.ResourceBundle", true, 0);
- }
+ public static MethodParameterDescriptor getRBAccessorDesc() {
+ if (rbAccessor == null) {
+ // Init descriptor for Resource-Bundle-Accessors
+ List<String> accessors = new ArrayList<String>();
+ accessors.add("getString");
+ accessors.add("getStringArray");
+ rbAccessor = new MethodParameterDescriptor(accessors,
+ "java.util.ResourceBundle", true, 0);
+ }
- return rbAccessor;
- }
+ return rbAccessor;
+ }
- public static String resolveRBReferenceVar(IDocument document,
- IResource resource, int pos, final String bundleId,
- CompilationUnit cu) {
- String bundleVar;
+ public static String resolveRBReferenceVar(IDocument document,
+ IResource resource, int pos, final String bundleId,
+ CompilationUnit cu) {
+ String bundleVar;
- PositionalTypeFinder typeFinder = new PositionalTypeFinder(pos);
- cu.accept(typeFinder);
- AnonymousClassDeclaration atd = typeFinder.getEnclosingAnonymType();
- TypeDeclaration td = typeFinder.getEnclosingType();
- MethodDeclaration meth = typeFinder.getEnclosingMethod();
+ PositionalTypeFinder typeFinder = new PositionalTypeFinder(pos);
+ cu.accept(typeFinder);
+ AnonymousClassDeclaration atd = typeFinder.getEnclosingAnonymType();
+ TypeDeclaration td = typeFinder.getEnclosingType();
+ MethodDeclaration meth = typeFinder.getEnclosingMethod();
- if (atd == null) {
- BundleDeclarationFinder bdf = new BundleDeclarationFinder(
- bundleId,
- td,
- meth != null
- && (meth.getModifiers() & Modifier.STATIC) == Modifier.STATIC);
- td.accept(bdf);
+ if (atd == null) {
+ BundleDeclarationFinder bdf = new BundleDeclarationFinder(
+ bundleId,
+ td,
+ meth != null
+ && (meth.getModifiers() & Modifier.STATIC) == Modifier.STATIC);
+ td.accept(bdf);
- bundleVar = bdf.getVariableName();
- } else {
- BundleDeclarationFinder bdf = new BundleDeclarationFinder(
- bundleId,
- atd,
- meth != null
- && (meth.getModifiers() & Modifier.STATIC) == Modifier.STATIC);
- atd.accept(bdf);
+ bundleVar = bdf.getVariableName();
+ } else {
+ BundleDeclarationFinder bdf = new BundleDeclarationFinder(
+ bundleId,
+ atd,
+ meth != null
+ && (meth.getModifiers() & Modifier.STATIC) == Modifier.STATIC);
+ atd.accept(bdf);
- bundleVar = bdf.getVariableName();
- }
+ bundleVar = bdf.getVariableName();
+ }
- // Check also method body
- if (meth != null) {
- try {
- InMethodBundleDeclFinder imbdf = new InMethodBundleDeclFinder(
- bundleId, pos);
- typeFinder.getEnclosingMethod().accept(imbdf);
- bundleVar = imbdf.getVariableName() != null ? imbdf
- .getVariableName() : bundleVar;
- } catch (Exception e) {
- // ignore
- }
- }
+ // Check also method body
+ if (meth != null) {
+ try {
+ InMethodBundleDeclFinder imbdf = new InMethodBundleDeclFinder(
+ bundleId, pos);
+ typeFinder.getEnclosingMethod().accept(imbdf);
+ bundleVar = imbdf.getVariableName() != null ? imbdf
+ .getVariableName() : bundleVar;
+ } catch (Exception e) {
+ // ignore
+ }
+ }
- return bundleVar;
- }
+ return bundleVar;
+ }
- public static String getNonExistingRBRefName(String bundleId,
- IDocument document, CompilationUnit cu) {
- String referenceName = null;
- int i = 0;
+ public static String getNonExistingRBRefName(String bundleId,
+ CompilationUnit cu) {
+ String referenceName = null;
+ int i = 0;
- while (referenceName == null) {
- String actRef = bundleId.substring(bundleId.lastIndexOf(".") + 1)
- + "Ref" + (i == 0 ? "" : i);
- actRef = actRef.toLowerCase();
+ while (referenceName == null) {
+ String actRef = bundleId.substring(bundleId.lastIndexOf(".") + 1)
+ + "Ref" + (i == 0 ? "" : i);
+ actRef = actRef.toLowerCase();
- VariableFinder vf = new VariableFinder(actRef);
- cu.accept(vf);
+ VariableFinder vf = new VariableFinder(actRef);
+ cu.accept(vf);
- if (!vf.isVariableFound()) {
- referenceName = actRef;
- break;
- }
+ if (!vf.isVariableFound()) {
+ referenceName = actRef;
+ break;
+ }
- i++;
- }
+ i++;
+ }
- return referenceName;
- }
+ return referenceName;
+ }
- @Deprecated
- public static String resolveResourceBundle(
- MethodInvocation methodInvocation,
- MethodParameterDescriptor rbDefinition,
- Map<IVariableBinding, VariableDeclarationFragment> variableBindingManagers) {
- String bundleName = null;
+ @Deprecated
+ public static String resolveResourceBundle(
+ MethodInvocation methodInvocation,
+ MethodParameterDescriptor rbDefinition,
+ Map<IVariableBinding, VariableDeclarationFragment> variableBindingManagers) {
+ String bundleName = null;
- if (methodInvocation.getExpression() instanceof SimpleName) {
- SimpleName vName = (SimpleName) methodInvocation.getExpression();
- IVariableBinding vBinding = (IVariableBinding) vName
- .resolveBinding();
- VariableDeclarationFragment dec = variableBindingManagers
- .get(vBinding);
+ if (methodInvocation.getExpression() instanceof SimpleName) {
+ SimpleName vName = (SimpleName) methodInvocation.getExpression();
+ IVariableBinding vBinding = (IVariableBinding) vName
+ .resolveBinding();
+ VariableDeclarationFragment dec = variableBindingManagers
+ .get(vBinding);
- if (dec.getInitializer() instanceof MethodInvocation) {
- MethodInvocation init = (MethodInvocation) dec.getInitializer();
+ if (dec.getInitializer() instanceof MethodInvocation) {
+ MethodInvocation init = (MethodInvocation) dec.getInitializer();
- // Check declaring class
- boolean isValidClass = false;
- ITypeBinding type = init.resolveMethodBinding()
- .getDeclaringClass();
- while (type != null) {
- if (type.getQualifiedName().equals(
- rbDefinition.getDeclaringClass())) {
- isValidClass = true;
- break;
- } else {
- if (rbDefinition.isConsiderSuperclass()) {
- type = type.getSuperclass();
- } else {
- type = null;
- }
- }
+ // Check declaring class
+ boolean isValidClass = false;
+ ITypeBinding type = init.resolveMethodBinding()
+ .getDeclaringClass();
+ while (type != null) {
+ if (type.getQualifiedName().equals(
+ rbDefinition.getDeclaringClass())) {
+ isValidClass = true;
+ break;
+ } else {
+ if (rbDefinition.isConsiderSuperclass()) {
+ type = type.getSuperclass();
+ } else {
+ type = null;
+ }
+ }
- }
- if (!isValidClass) {
- return null;
- }
+ }
+ if (!isValidClass) {
+ return null;
+ }
- boolean isValidMethod = false;
- for (String mn : rbDefinition.getMethodName()) {
- if (init.getName().getFullyQualifiedName().equals(mn)) {
- isValidMethod = true;
- break;
- }
- }
- if (!isValidMethod) {
- return null;
- }
+ boolean isValidMethod = false;
+ for (String mn : rbDefinition.getMethodName()) {
+ if (init.getName().getFullyQualifiedName().equals(mn)) {
+ isValidMethod = true;
+ break;
+ }
+ }
+ if (!isValidMethod) {
+ return null;
+ }
- // retrieve bundlename
- if (init.arguments().size() < rbDefinition.getPosition() + 1) {
- return null;
- }
+ // retrieve bundlename
+ if (init.arguments().size() < rbDefinition.getPosition() + 1) {
+ return null;
+ }
- bundleName = ((StringLiteral) init.arguments().get(
- rbDefinition.getPosition())).getLiteralValue();
- }
- }
+ bundleName = ((StringLiteral) init.arguments().get(
+ rbDefinition.getPosition())).getLiteralValue();
+ }
+ }
- return bundleName;
- }
+ return bundleName;
+ }
- public static SLLocation resolveResourceBundleLocation(
- MethodInvocation methodInvocation,
- MethodParameterDescriptor rbDefinition,
- Map<IVariableBinding, VariableDeclarationFragment> variableBindingManagers) {
- SLLocation bundleDesc = null;
+ public static SLLocation resolveResourceBundleLocation(
+ MethodInvocation methodInvocation,
+ MethodParameterDescriptor rbDefinition,
+ Map<IVariableBinding, VariableDeclarationFragment> variableBindingManagers) {
+ SLLocation bundleDesc = null;
- if (methodInvocation.getExpression() instanceof SimpleName) {
- SimpleName vName = (SimpleName) methodInvocation.getExpression();
- IVariableBinding vBinding = (IVariableBinding) vName
- .resolveBinding();
- VariableDeclarationFragment dec = variableBindingManagers
- .get(vBinding);
+ if (methodInvocation.getExpression() instanceof SimpleName) {
+ SimpleName vName = (SimpleName) methodInvocation.getExpression();
+ IVariableBinding vBinding = (IVariableBinding) vName
+ .resolveBinding();
+ VariableDeclarationFragment dec = variableBindingManagers
+ .get(vBinding);
- if (dec.getInitializer() instanceof MethodInvocation) {
- MethodInvocation init = (MethodInvocation) dec.getInitializer();
+ if (dec.getInitializer() instanceof MethodInvocation) {
+ MethodInvocation init = (MethodInvocation) dec.getInitializer();
- // Check declaring class
- boolean isValidClass = false;
- ITypeBinding type = init.resolveMethodBinding()
- .getDeclaringClass();
- while (type != null) {
- if (type.getQualifiedName().equals(
- rbDefinition.getDeclaringClass())) {
- isValidClass = true;
- break;
- } else {
- if (rbDefinition.isConsiderSuperclass()) {
- type = type.getSuperclass();
- } else {
- type = null;
- }
- }
+ // Check declaring class
+ boolean isValidClass = false;
+ ITypeBinding type = init.resolveMethodBinding()
+ .getDeclaringClass();
+ while (type != null) {
+ if (type.getQualifiedName().equals(
+ rbDefinition.getDeclaringClass())) {
+ isValidClass = true;
+ break;
+ } else {
+ if (rbDefinition.isConsiderSuperclass()) {
+ type = type.getSuperclass();
+ } else {
+ type = null;
+ }
+ }
- }
- if (!isValidClass) {
- return null;
- }
+ }
+ if (!isValidClass) {
+ return null;
+ }
- boolean isValidMethod = false;
- for (String mn : rbDefinition.getMethodName()) {
- if (init.getName().getFullyQualifiedName().equals(mn)) {
- isValidMethod = true;
- break;
- }
- }
- if (!isValidMethod) {
- return null;
- }
+ boolean isValidMethod = false;
+ for (String mn : rbDefinition.getMethodName()) {
+ if (init.getName().getFullyQualifiedName().equals(mn)) {
+ isValidMethod = true;
+ break;
+ }
+ }
+ if (!isValidMethod) {
+ return null;
+ }
- // retrieve bundlename
- if (init.arguments().size() < rbDefinition.getPosition() + 1) {
- return null;
- }
+ // retrieve bundlename
+ if (init.arguments().size() < rbDefinition.getPosition() + 1) {
+ return null;
+ }
- StringLiteral bundleLiteral = ((StringLiteral) init.arguments()
- .get(rbDefinition.getPosition()));
- bundleDesc = new SLLocation(null,
- bundleLiteral.getStartPosition(),
- bundleLiteral.getLength()
- + bundleLiteral.getStartPosition(),
- bundleLiteral.getLiteralValue());
- }
- }
+ StringLiteral bundleLiteral = ((StringLiteral) init.arguments()
+ .get(rbDefinition.getPosition()));
+ bundleDesc = new SLLocation(null,
+ bundleLiteral.getStartPosition(),
+ bundleLiteral.getLength()
+ + bundleLiteral.getStartPosition(),
+ bundleLiteral.getLiteralValue());
+ }
+ }
- return bundleDesc;
- }
+ return bundleDesc;
+ }
- private static boolean isMatchingMethodDescriptor(
- MethodInvocation methodInvocation, MethodParameterDescriptor desc) {
- boolean result = false;
+ private static boolean isMatchingMethodDescriptor(
+ MethodInvocation methodInvocation, MethodParameterDescriptor desc) {
+ boolean result = false;
- if (methodInvocation.resolveMethodBinding() == null) {
- return false;
- }
+ if (methodInvocation.resolveMethodBinding() == null) {
+ return false;
+ }
- String methodName = methodInvocation.resolveMethodBinding().getName();
+ String methodName = methodInvocation.resolveMethodBinding().getName();
- // Check declaring class
- ITypeBinding type = methodInvocation.resolveMethodBinding()
- .getDeclaringClass();
- while (type != null) {
- if (type.getQualifiedName().equals(desc.getDeclaringClass())) {
- result = true;
- break;
- } else {
- if (desc.isConsiderSuperclass()) {
- type = type.getSuperclass();
- } else {
- type = null;
- }
- }
+ // Check declaring class
+ ITypeBinding type = methodInvocation.resolveMethodBinding()
+ .getDeclaringClass();
+ while (type != null) {
+ if (type.getQualifiedName().equals(desc.getDeclaringClass())) {
+ result = true;
+ break;
+ } else {
+ if (desc.isConsiderSuperclass()) {
+ type = type.getSuperclass();
+ } else {
+ type = null;
+ }
+ }
- }
+ }
- if (!result) {
- return false;
- }
+ if (!result) {
+ return false;
+ }
- result = !result;
+ result = !result;
- // Check method name
- for (String method : desc.getMethodName()) {
- if (method.equals(methodName)) {
- result = true;
- break;
- }
- }
+ // Check method name
+ for (String method : desc.getMethodName()) {
+ if (method.equals(methodName)) {
+ result = true;
+ break;
+ }
+ }
- return result;
- }
+ return result;
+ }
- public static boolean isMatchingMethodParamDesc(
- MethodInvocation methodInvocation, String literal,
- MethodParameterDescriptor desc) {
- boolean result = isMatchingMethodDescriptor(methodInvocation, desc);
+ public static boolean isMatchingMethodParamDesc(
+ MethodInvocation methodInvocation, String literal,
+ MethodParameterDescriptor desc) {
+ boolean result = isMatchingMethodDescriptor(methodInvocation, desc);
- if (!result) {
- return false;
- } else {
- result = false;
- }
+ if (!result) {
+ return false;
+ } else {
+ result = false;
+ }
- if (methodInvocation.arguments().size() > desc.getPosition()) {
- if (methodInvocation.arguments().get(desc.getPosition()) instanceof StringLiteral) {
- StringLiteral sl = (StringLiteral) methodInvocation.arguments()
- .get(desc.getPosition());
- if (sl.getLiteralValue().trim().toLowerCase()
- .equals(literal.toLowerCase())) {
- result = true;
- }
- }
- }
+ if (methodInvocation.arguments().size() > desc.getPosition()) {
+ if (methodInvocation.arguments().get(desc.getPosition()) instanceof StringLiteral) {
+ StringLiteral sl = (StringLiteral) methodInvocation.arguments()
+ .get(desc.getPosition());
+ if (sl.getLiteralValue().trim().toLowerCase()
+ .equals(literal.toLowerCase())) {
+ result = true;
+ }
+ }
+ }
- return result;
- }
+ return result;
+ }
- public static boolean isMatchingMethodParamDesc(
- MethodInvocation methodInvocation, StringLiteral literal,
- MethodParameterDescriptor desc) {
- int keyParameter = desc.getPosition();
- boolean result = isMatchingMethodDescriptor(methodInvocation, desc);
+ public static boolean isMatchingMethodParamDesc(
+ MethodInvocation methodInvocation, StringLiteral literal,
+ MethodParameterDescriptor desc) {
+ int keyParameter = desc.getPosition();
+ boolean result = isMatchingMethodDescriptor(methodInvocation, desc);
- if (!result) {
- return false;
- }
+ if (!result) {
+ return false;
+ }
- // Check position within method call
- StructuralPropertyDescriptor spd = literal.getLocationInParent();
- if (spd.isChildListProperty()) {
- @SuppressWarnings("unchecked")
- List<ASTNode> arguments = (List<ASTNode>) methodInvocation
- .getStructuralProperty(spd);
- result = (arguments.size() > keyParameter && arguments
- .get(keyParameter) == literal);
- }
+ // Check position within method call
+ StructuralPropertyDescriptor spd = literal.getLocationInParent();
+ if (spd.isChildListProperty()) {
+ @SuppressWarnings("unchecked")
+ List<ASTNode> arguments = (List<ASTNode>) methodInvocation
+ .getStructuralProperty(spd);
+ result = (arguments.size() > keyParameter && arguments
+ .get(keyParameter) == literal);
+ }
- return result;
- }
+ return result;
+ }
- public static ICompilationUnit createCompilationUnit(IResource resource) {
- // Instantiate a new AST parser
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- parser.setResolveBindings(true);
+ public static ICompilationUnit createCompilationUnit(IResource resource) {
+ // Instantiate a new AST parser
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setResolveBindings(true);
- ICompilationUnit cu = JavaCore.createCompilationUnitFrom(resource
- .getProject().getFile(resource.getRawLocation()));
+ ICompilationUnit cu = JavaCore.createCompilationUnitFrom(resource
+ .getProject().getFile(resource.getRawLocation()));
- return cu;
- }
+ return cu;
+ }
- public static CompilationUnit createCompilationUnit(IDocument document) {
- // Instantiate a new AST parser
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- parser.setResolveBindings(true);
+ public static CompilationUnit createCompilationUnit(IDocument document) {
+ // Instantiate a new AST parser
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setResolveBindings(true);
- parser.setSource(document.get().toCharArray());
- return (CompilationUnit) parser.createAST(null);
- }
+ parser.setSource(document.get().toCharArray());
+ return (CompilationUnit) parser.createAST(null);
+ }
- public static void createImport(IDocument doc, IResource resource,
- CompilationUnit cu, AST ast, ASTRewrite rewriter,
- String qualifiedClassName) throws CoreException,
- BadLocationException {
+ public static void createImport(IDocument doc, IResource resource,
+ CompilationUnit cu, AST ast, ASTRewrite rewriter,
+ String qualifiedClassName) throws CoreException,
+ BadLocationException {
- ImportFinder impFinder = new ImportFinder(qualifiedClassName);
+ ImportFinder impFinder = new ImportFinder(qualifiedClassName);
- cu.accept(impFinder);
+ cu.accept(impFinder);
- if (!impFinder.isImportFound()) {
- // ITextFileBufferManager bufferManager =
- // FileBuffers.getTextFileBufferManager();
- // IPath path = resource.getFullPath();
- //
- // bufferManager.connect(path, LocationKind.IFILE, null);
- // ITextFileBuffer textFileBuffer =
- // bufferManager.getTextFileBuffer(doc);
+ if (!impFinder.isImportFound()) {
+ ImportDeclaration id = ast.newImportDeclaration();
+ id.setName(ast.newName(qualifiedClassName.split("\\.")));
+ id.setStatic(false);
- // TODO create new import
- ImportDeclaration id = ast.newImportDeclaration();
- id.setName(ast.newName(qualifiedClassName.split("\\.")));
- id.setStatic(false);
+ ListRewrite lrw = rewriter.getListRewrite(cu,
+ CompilationUnit.IMPORTS_PROPERTY);
+ lrw.insertFirst(id, null);
+ }
- ListRewrite lrw = rewriter.getListRewrite(cu,
- CompilationUnit.IMPORTS_PROPERTY);
- lrw.insertFirst(id, null);
+ }
- // TextEdit te = rewriter.rewriteAST(doc, null);
- // te.apply(doc);
- //
- // if (textFileBuffer != null)
- // textFileBuffer.commit(null, false);
- // else
- // FileUtils.saveTextFile(resource.getProject().getFile(resource.getProjectRelativePath()),
- // doc.get());
- // bufferManager.disconnect(path, LocationKind.IFILE, null);
- }
+ public static void createReplaceNonInternationalisationComment(
+ CompilationUnit cu, IDocument doc, int position) {
+ int i = findNonInternationalisationPosition(cu, doc, position);
- }
+ IRegion reg;
+ try {
+ reg = doc.getLineInformationOfOffset(position);
+ doc.replace(reg.getOffset() + reg.getLength(), 0, " //$NON-NLS-"
+ + i + "$");
+ } catch (BadLocationException e) {
+ Logger.logError(e);
+ }
+ }
- public static void createReplaceNonInternationalisationComment(
- CompilationUnit cu, IDocument doc, int position) {
- int i = findNonInternationalisationPosition(cu, doc, position);
+ // TODO export initializer specification into a methodinvocationdefinition
+ @SuppressWarnings("unchecked")
+ public static void createResourceBundleReference(IResource resource,
+ int typePos, IDocument doc, String bundleId, Locale locale,
+ boolean globalReference, String variableName, CompilationUnit cu, AST ast, ASTRewrite rewriter) {
- IRegion reg;
- try {
- reg = doc.getLineInformationOfOffset(position);
- doc.replace(reg.getOffset() + reg.getLength(), 0, " //$NON-NLS-"
- + i + "$");
- } catch (BadLocationException e) {
- Logger.logError(e);
- }
- }
+ try {
- // TODO export initializer specification into a methodinvocationdefinition
- @SuppressWarnings("unchecked")
- public static void createResourceBundleReference(IResource resource,
- int typePos, IDocument doc, String bundleId, Locale locale,
- boolean globalReference, String variableName, CompilationUnit cu) {
+ if (globalReference) {
- try {
+ // retrieve compilation unit from document
+ PositionalTypeFinder typeFinder = new PositionalTypeFinder(
+ typePos);
+ cu.accept(typeFinder);
+ ASTNode node = typeFinder.getEnclosingType();
+ ASTNode anonymNode = typeFinder.getEnclosingAnonymType();
+ if (anonymNode != null) {
+ node = anonymNode;
+ }
- if (globalReference) {
+ MethodDeclaration meth = typeFinder.getEnclosingMethod();
- // retrieve compilation unit from document
- PositionalTypeFinder typeFinder = new PositionalTypeFinder(
- typePos);
- cu.accept(typeFinder);
- ASTNode node = typeFinder.getEnclosingType();
- ASTNode anonymNode = typeFinder.getEnclosingAnonymType();
- if (anonymNode != null) {
- node = anonymNode;
- }
+ VariableDeclarationFragment vdf = ast
+ .newVariableDeclarationFragment();
+ vdf.setName(ast.newSimpleName(variableName));
- MethodDeclaration meth = typeFinder.getEnclosingMethod();
- AST ast = node.getAST();
+ // set initializer
+ vdf.setInitializer(createResourceBundleGetter(ast, bundleId,
+ locale));
- VariableDeclarationFragment vdf = ast
- .newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName(variableName));
+ FieldDeclaration fd = ast.newFieldDeclaration(vdf);
+ fd.setType(ast.newSimpleType(ast
+ .newName(new String[] { "ResourceBundle" })));
- // set initializer
- vdf.setInitializer(createResourceBundleGetter(ast, bundleId,
- locale));
+ if (meth != null
+ && (meth.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
+ fd.modifiers().addAll(ast.newModifiers(Modifier.STATIC));
+ }
- FieldDeclaration fd = ast.newFieldDeclaration(vdf);
- fd.setType(ast.newSimpleType(ast
- .newName(new String[] { "ResourceBundle" })));
+ // rewrite AST
+ ListRewrite lrw = rewriter
+ .getListRewrite(
+ node,
+ node instanceof TypeDeclaration ? TypeDeclaration.BODY_DECLARATIONS_PROPERTY
+ : AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
+ lrw.insertAt(fd, 0, null);
- if (meth != null
- && (meth.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
- fd.modifiers().addAll(ast.newModifiers(Modifier.STATIC));
- }
+ // create import if required
+ createImport(doc, resource, cu, ast, rewriter,
+ getRBDefinitionDesc().getDeclaringClass());
+ } else {
- // rewrite AST
- ASTRewrite rewriter = ASTRewrite.create(ast);
- ListRewrite lrw = rewriter
- .getListRewrite(
- node,
- node instanceof TypeDeclaration ? TypeDeclaration.BODY_DECLARATIONS_PROPERTY
- : AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
- lrw.insertAt(fd, /*
- * findIndexOfLastField(node.bodyDeclarations())
- * +1
- */
- 0, null);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
- // create import if required
- createImport(doc, resource, cu, ast, rewriter,
- getRBDefinitionDesc().getDeclaringClass());
+ @SuppressWarnings("unchecked")
+ protected static MethodInvocation createResourceBundleGetter(AST ast,
+ String bundleId, Locale locale) {
+ MethodInvocation mi = ast.newMethodInvocation();
- TextEdit te = rewriter.rewriteAST(doc, null);
- te.apply(doc);
- } else {
+ mi.setName(ast.newSimpleName("getBundle"));
+ mi.setExpression(ast.newName(new String[] { "ResourceBundle" }));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
+ // Add bundle argument
+ StringLiteral sl = ast.newStringLiteral();
+ sl.setLiteralValue(bundleId);
+ mi.arguments().add(sl);
- @SuppressWarnings("unchecked")
- protected static MethodInvocation createResourceBundleGetter(AST ast,
- String bundleId, Locale locale) {
- MethodInvocation mi = ast.newMethodInvocation();
+ // TODO Add Locale argument
- mi.setName(ast.newSimpleName("getBundle"));
- mi.setExpression(ast.newName(new String[] { "ResourceBundle" }));
+ return mi;
+ }
- // Add bundle argument
- StringLiteral sl = ast.newStringLiteral();
- sl.setLiteralValue(bundleId);
- mi.arguments().add(sl);
+ public static ASTNode getEnclosingType(CompilationUnit cu, int pos) {
+ PositionalTypeFinder typeFinder = new PositionalTypeFinder(pos);
+ cu.accept(typeFinder);
+ return (typeFinder.getEnclosingAnonymType() != null) ? typeFinder
+ .getEnclosingAnonymType() : typeFinder.getEnclosingType();
+ }
- // TODO Add Locale argument
+ public static ASTNode getEnclosingType(ASTNode cu, int pos) {
+ PositionalTypeFinder typeFinder = new PositionalTypeFinder(pos);
+ cu.accept(typeFinder);
+ return (typeFinder.getEnclosingAnonymType() != null) ? typeFinder
+ .getEnclosingAnonymType() : typeFinder.getEnclosingType();
+ }
- return mi;
- }
+ @SuppressWarnings("unchecked")
+ protected static MethodInvocation referenceResource(AST ast,
+ String accessorName, String key, Locale locale) {
+ MethodParameterDescriptor accessorDesc = getRBAccessorDesc();
+ MethodInvocation mi = ast.newMethodInvocation();
- public static ASTNode getEnclosingType(CompilationUnit cu, int pos) {
- PositionalTypeFinder typeFinder = new PositionalTypeFinder(pos);
- cu.accept(typeFinder);
- return (typeFinder.getEnclosingAnonymType() != null) ? typeFinder
- .getEnclosingAnonymType() : typeFinder.getEnclosingType();
- }
+ mi.setName(ast.newSimpleName(accessorDesc.getMethodName().get(0)));
- public static ASTNode getEnclosingType(ASTNode cu, int pos) {
- PositionalTypeFinder typeFinder = new PositionalTypeFinder(pos);
- cu.accept(typeFinder);
- return (typeFinder.getEnclosingAnonymType() != null) ? typeFinder
- .getEnclosingAnonymType() : typeFinder.getEnclosingType();
- }
+ // Declare expression
+ StringLiteral sl = ast.newStringLiteral();
+ sl.setLiteralValue(key);
- @SuppressWarnings("unchecked")
- protected static MethodInvocation referenceResource(AST ast,
- String accessorName, String key, Locale locale) {
- MethodParameterDescriptor accessorDesc = getRBAccessorDesc();
- MethodInvocation mi = ast.newMethodInvocation();
+ // TODO define locale expression
+ if (mi.arguments().size() == accessorDesc.getPosition()) {
+ mi.arguments().add(sl);
+ }
- mi.setName(ast.newSimpleName(accessorDesc.getMethodName().get(0)));
+ SimpleName name = ast.newSimpleName(accessorName);
+ mi.setExpression(name);
- // Declare expression
- StringLiteral sl = ast.newStringLiteral();
- sl.setLiteralValue(key);
+ return mi;
+ }
- // TODO define locale expression
- if (mi.arguments().size() == accessorDesc.getPosition()) {
- mi.arguments().add(sl);
- }
+ public static String createResourceReference(String bundleId, String key,
+ Locale locale, IResource resource, int typePos,
+ String accessorName, AST ast, ASTRewrite astRewrite, CompilationUnit cu) {
+
+ final StringLiteralFinder literalFinder = new StringLiteralFinder(typePos);
+ cu.accept(literalFinder);
+ final StringLiteral literal = literalFinder.getStringLiteral();
- SimpleName name = ast.newSimpleName(accessorName);
- mi.setExpression(name);
-
- return mi;
- }
-
- public static String createResourceReference(String bundleId, String key,
- Locale locale, IResource resource, int typePos,
- String accessorName, IDocument doc, CompilationUnit cu) {
-
- PositionalTypeFinder typeFinder = new PositionalTypeFinder(typePos);
- cu.accept(typeFinder);
- AnonymousClassDeclaration atd = typeFinder.getEnclosingAnonymType();
- TypeDeclaration td = typeFinder.getEnclosingType();
-
- // retrieve compilation unit from document
- ASTNode node = atd == null ? td : atd;
- AST ast = node.getAST();
-
- ExpressionStatement expressionStatement = ast
- .newExpressionStatement(referenceResource(ast, accessorName,
- key, locale));
+ final MethodInvocation methodInvocation = referenceResource(ast, accessorName,
+ key, locale);
- String exp = expressionStatement.toString();
+ astRewrite.replace(literal, methodInvocation, null);
+
+ String exp = methodInvocation.toString();
- // remove semicolon and line break at the end of this expression
- // statement
- if (exp.endsWith(";\n")) {
- exp = exp.substring(0, exp.length() - 2);
- }
+ // remove semicolon and line break at the end of this expression
+ // statement
+ if (exp.endsWith(";\n")) {
+ exp = exp.substring(0, exp.length() - 2);
+ }
- return exp;
- }
+ return exp;
+ }
- private static int findNonInternationalisationPosition(CompilationUnit cu,
- IDocument doc, int offset) {
- LinePreStringsFinder lsfinder = null;
- try {
- lsfinder = new LinePreStringsFinder(offset, doc);
- cu.accept(lsfinder);
- } catch (BadLocationException e) {
- Logger.logError(e);
- }
- if (lsfinder == null) {
- return 1;
- }
+ private static int findNonInternationalisationPosition(CompilationUnit cu,
+ IDocument doc, int offset) {
+ LinePreStringsFinder lsfinder = null;
+ try {
+ lsfinder = new LinePreStringsFinder(offset, doc);
+ cu.accept(lsfinder);
+ } catch (BadLocationException e) {
+ Logger.logError(e);
+ }
+ if (lsfinder == null) {
+ return 1;
+ }
- List<StringLiteral> strings = lsfinder.getStrings();
+ List<StringLiteral> strings = lsfinder.getStrings();
- return strings.size() + 1;
- }
+ return strings.size() + 1;
+ }
- public static boolean existsNonInternationalisationComment(
- StringLiteral literal) throws BadLocationException {
- CompilationUnit cu = (CompilationUnit) literal.getRoot();
- ICompilationUnit icu = (ICompilationUnit) cu.getJavaElement();
+ public static boolean existsNonInternationalisationComment(
+ StringLiteral literal) throws BadLocationException {
+ CompilationUnit cu = (CompilationUnit) literal.getRoot();
+ ICompilationUnit icu = (ICompilationUnit) cu.getJavaElement();
- IDocument doc = null;
- try {
- doc = new Document(icu.getSource());
- } catch (JavaModelException e) {
- Logger.logError(e);
- }
+ IDocument doc = null;
+ try {
+ doc = new Document(icu.getSource());
+ } catch (JavaModelException e) {
+ Logger.logError(e);
+ }
- // get whole line in which string literal
- int lineNo = doc.getLineOfOffset(literal.getStartPosition());
- int lineOffset = doc.getLineOffset(lineNo);
- int lineLength = doc.getLineLength(lineNo);
- String lineOfString = doc.get(lineOffset, lineLength);
+ // get whole line in which string literal
+ int lineNo = doc.getLineOfOffset(literal.getStartPosition());
+ int lineOffset = doc.getLineOffset(lineNo);
+ int lineLength = doc.getLineLength(lineNo);
+ String lineOfString = doc.get(lineOffset, lineLength);
- // search for a line comment in this line
- int indexComment = lineOfString.indexOf("//");
+ // search for a line comment in this line
+ int indexComment = lineOfString.indexOf("//");
- if (indexComment == -1) {
- return false;
- }
+ if (indexComment == -1) {
+ return false;
+ }
- String comment = lineOfString.substring(indexComment);
+ String comment = lineOfString.substring(indexComment);
- // remove first "//" of line comment
- comment = comment.substring(2).toLowerCase();
+ // remove first "//" of line comment
+ comment = comment.substring(2).toLowerCase();
- // split line comments, necessary if more NON-NLS comments exist in one line, eg.: $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3
- String[] comments = comment.split("//");
+ // split line comments, necessary if more NON-NLS comments exist in one line, eg.: $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3
+ String[] comments = comment.split("//");
- for (String commentFrag : comments) {
- commentFrag = commentFrag.trim();
+ for (String commentFrag : comments) {
+ commentFrag = commentFrag.trim();
- // if comment match format: "$non-nls$" then ignore whole line
- if (commentFrag.matches("^\\$non-nls\\$$")) {
- return true;
+ // if comment match format: "$non-nls$" then ignore whole line
+ if (commentFrag.matches("^\\$non-nls\\$$")) {
+ return true;
- // if comment match format: "$non-nls-{number}$" then only
- // ignore string which is on given position
- } else if (commentFrag.matches("^\\$non-nls-\\d+\\$$")) {
- int iString = findNonInternationalisationPosition(cu, doc,
- literal.getStartPosition());
- int iComment = new Integer(commentFrag.substring(9, 10));
- if (iString == iComment) {
- return true;
- }
- }
- }
- return false;
- }
+ // if comment match format: "$non-nls-{number}$" then only
+ // ignore string which is on given position
+ } else if (commentFrag.matches("^\\$non-nls-\\d+\\$$")) {
+ int iString = findNonInternationalisationPosition(cu, doc,
+ literal.getStartPosition());
+ int iComment = new Integer(commentFrag.substring(9, 10));
+ if (iString == iComment) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
- public static StringLiteral getStringLiteralAtPos(CompilationUnit cu,
- int position) {
- StringLiteralFinder strFinder = new StringLiteralFinder(position);
- cu.accept(strFinder);
- return strFinder.getStringLiteral();
- }
+ public static StringLiteral getStringLiteralAtPos(CompilationUnit cu,
+ int position) {
+ StringLiteralFinder strFinder = new StringLiteralFinder(position);
+ cu.accept(strFinder);
+ return strFinder.getStringLiteral();
+ }
- static class PositionalTypeFinder extends ASTVisitor {
+ static class PositionalTypeFinder extends ASTVisitor {
- private int position;
- private TypeDeclaration enclosingType;
- private AnonymousClassDeclaration enclosingAnonymType;
- private MethodDeclaration enclosingMethod;
+ private int position;
+ private TypeDeclaration enclosingType;
+ private AnonymousClassDeclaration enclosingAnonymType;
+ private MethodDeclaration enclosingMethod;
- public PositionalTypeFinder(int pos) {
- position = pos;
- }
+ public PositionalTypeFinder(int pos) {
+ position = pos;
+ }
- public TypeDeclaration getEnclosingType() {
- return enclosingType;
- }
+ public TypeDeclaration getEnclosingType() {
+ return enclosingType;
+ }
- public AnonymousClassDeclaration getEnclosingAnonymType() {
- return enclosingAnonymType;
- }
+ public AnonymousClassDeclaration getEnclosingAnonymType() {
+ return enclosingAnonymType;
+ }
- public MethodDeclaration getEnclosingMethod() {
- return enclosingMethod;
- }
+ public MethodDeclaration getEnclosingMethod() {
+ return enclosingMethod;
+ }
- @Override
- public boolean visit(MethodDeclaration node) {
- if (position >= node.getStartPosition()
- && position <= (node.getStartPosition() + node.getLength())) {
- enclosingMethod = node;
- return true;
- } else {
- return false;
- }
- }
+ @Override
+ public boolean visit(MethodDeclaration node) {
+ if (position >= node.getStartPosition()
+ && position <= (node.getStartPosition() + node.getLength())) {
+ enclosingMethod = node;
+ return true;
+ } else {
+ return false;
+ }
+ }
- @Override
- public boolean visit(TypeDeclaration node) {
- if (position >= node.getStartPosition()
- && position <= (node.getStartPosition() + node.getLength())) {
- enclosingType = node;
- return true;
- } else {
- return false;
- }
- }
+ @Override
+ public boolean visit(TypeDeclaration node) {
+ if (position >= node.getStartPosition()
+ && position <= (node.getStartPosition() + node.getLength())) {
+ enclosingType = node;
+ return true;
+ } else {
+ return false;
+ }
+ }
- @Override
- public boolean visit(AnonymousClassDeclaration node) {
- if (position >= node.getStartPosition()
- && position <= (node.getStartPosition() + node.getLength())) {
- enclosingAnonymType = node;
- return true;
- } else {
- return false;
- }
- }
- }
+ @Override
+ public boolean visit(AnonymousClassDeclaration node) {
+ if (position >= node.getStartPosition()
+ && position <= (node.getStartPosition() + node.getLength())) {
+ enclosingAnonymType = node;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
- static class ImportFinder extends ASTVisitor {
+ static class ImportFinder extends ASTVisitor {
- String qName;
- boolean importFound = false;
+ String qName;
+ boolean importFound = false;
- public ImportFinder(String qName) {
- this.qName = qName;
- }
+ public ImportFinder(String qName) {
+ this.qName = qName;
+ }
- public boolean isImportFound() {
- return importFound;
- }
+ public boolean isImportFound() {
+ return importFound;
+ }
- @Override
- public boolean visit(ImportDeclaration id) {
- if (id.getName().getFullyQualifiedName().equals(qName)) {
- importFound = true;
- }
+ @Override
+ public boolean visit(ImportDeclaration id) {
+ if (id.getName().getFullyQualifiedName().equals(qName)) {
+ importFound = true;
+ }
- return true;
- }
- }
+ return true;
+ }
+ }
- static class VariableFinder extends ASTVisitor {
+ static class VariableFinder extends ASTVisitor {
- boolean found = false;
- String variableName;
+ boolean found = false;
+ String variableName;
- public boolean isVariableFound() {
- return found;
- }
+ public boolean isVariableFound() {
+ return found;
+ }
- public VariableFinder(String variableName) {
- this.variableName = variableName;
- }
+ public VariableFinder(String variableName) {
+ this.variableName = variableName;
+ }
- @Override
- public boolean visit(VariableDeclarationFragment vdf) {
- if (vdf.getName().getFullyQualifiedName().equals(variableName)) {
- found = true;
- return false;
- }
+ @Override
+ public boolean visit(VariableDeclarationFragment vdf) {
+ if (vdf.getName().getFullyQualifiedName().equals(variableName)) {
+ found = true;
+ return false;
+ }
- return true;
- }
- }
+ return true;
+ }
+ }
- static class InMethodBundleDeclFinder extends ASTVisitor {
- String varName;
- String bundleId;
- int pos;
+ static class InMethodBundleDeclFinder extends ASTVisitor {
+ String varName;
+ String bundleId;
+ int pos;
- public InMethodBundleDeclFinder(String bundleId, int pos) {
- this.bundleId = bundleId;
- this.pos = pos;
- }
+ public InMethodBundleDeclFinder(String bundleId, int pos) {
+ this.bundleId = bundleId;
+ this.pos = pos;
+ }
- public String getVariableName() {
- return varName;
- }
+ public String getVariableName() {
+ return varName;
+ }
- @Override
- public boolean visit(VariableDeclarationFragment fdvd) {
- if (fdvd.getStartPosition() > pos) {
- return false;
- }
+ @Override
+ public boolean visit(VariableDeclarationFragment fdvd) {
+ if (fdvd.getStartPosition() > pos) {
+ return false;
+ }
- // boolean bStatic = (fdvd.resolveBinding().getModifiers() &
- // Modifier.STATIC) == Modifier.STATIC;
- // if (!bStatic && isStatic)
- // return true;
+ // boolean bStatic = (fdvd.resolveBinding().getModifiers() &
+ // Modifier.STATIC) == Modifier.STATIC;
+ // if (!bStatic && isStatic)
+ // return true;
- String tmpVarName = fdvd.getName().getFullyQualifiedName();
+ String tmpVarName = fdvd.getName().getFullyQualifiedName();
- if (fdvd.getInitializer() instanceof MethodInvocation) {
- MethodInvocation fdi = (MethodInvocation) fdvd.getInitializer();
- if (isMatchingMethodParamDesc(fdi, bundleId,
- getRBDefinitionDesc())) {
- varName = tmpVarName;
- }
- }
- return true;
- }
- }
+ if (fdvd.getInitializer() instanceof MethodInvocation) {
+ MethodInvocation fdi = (MethodInvocation) fdvd.getInitializer();
+ if (isMatchingMethodParamDesc(fdi, bundleId,
+ getRBDefinitionDesc())) {
+ varName = tmpVarName;
+ }
+ }
+ return true;
+ }
+ }
- static class BundleDeclarationFinder extends ASTVisitor {
+ static class BundleDeclarationFinder extends ASTVisitor {
- String varName;
- String bundleId;
- ASTNode typeDef;
- boolean isStatic;
+ String varName;
+ String bundleId;
+ ASTNode typeDef;
+ boolean isStatic;
- public BundleDeclarationFinder(String bundleId, ASTNode td,
- boolean isStatic) {
- this.bundleId = bundleId;
- this.typeDef = td;
- this.isStatic = isStatic;
- }
+ public BundleDeclarationFinder(String bundleId, ASTNode td,
+ boolean isStatic) {
+ this.bundleId = bundleId;
+ this.typeDef = td;
+ this.isStatic = isStatic;
+ }
- public String getVariableName() {
- return varName;
- }
+ public String getVariableName() {
+ return varName;
+ }
- @Override
- public boolean visit(MethodDeclaration md) {
- return true;
- }
+ @Override
+ public boolean visit(MethodDeclaration md) {
+ return true;
+ }
- @Override
- public boolean visit(FieldDeclaration fd) {
- if (getEnclosingType(typeDef, fd.getStartPosition()) != typeDef) {
- return false;
- }
+ @Override
+ public boolean visit(FieldDeclaration fd) {
+ if (getEnclosingType(typeDef, fd.getStartPosition()) != typeDef) {
+ return false;
+ }
- boolean bStatic = (fd.getModifiers() & Modifier.STATIC) == Modifier.STATIC;
- if (!bStatic && isStatic) {
- return true;
- }
+ boolean bStatic = (fd.getModifiers() & Modifier.STATIC) == Modifier.STATIC;
+ if (!bStatic && isStatic) {
+ return true;
+ }
- if (fd.getType() instanceof SimpleType) {
- SimpleType fdType = (SimpleType) fd.getType();
- String typeName = fdType.getName().getFullyQualifiedName();
- String referenceName = getRBDefinitionDesc()
- .getDeclaringClass();
- if (typeName.equals(referenceName)
- || (referenceName.lastIndexOf(".") >= 0 && typeName
- .equals(referenceName.substring(referenceName
- .lastIndexOf(".") + 1)))) {
- // Check VariableDeclarationFragment
- if (fd.fragments().size() == 1) {
- if (fd.fragments().get(0) instanceof VariableDeclarationFragment) {
- VariableDeclarationFragment fdvd = (VariableDeclarationFragment) fd
- .fragments().get(0);
- String tmpVarName = fdvd.getName()
- .getFullyQualifiedName();
+ if (fd.getType() instanceof SimpleType) {
+ SimpleType fdType = (SimpleType) fd.getType();
+ String typeName = fdType.getName().getFullyQualifiedName();
+ String referenceName = getRBDefinitionDesc()
+ .getDeclaringClass();
+ if (typeName.equals(referenceName)
+ || (referenceName.lastIndexOf(".") >= 0 && typeName
+ .equals(referenceName.substring(referenceName
+ .lastIndexOf(".") + 1)))) {
+ // Check VariableDeclarationFragment
+ if (fd.fragments().size() == 1) {
+ if (fd.fragments().get(0) instanceof VariableDeclarationFragment) {
+ VariableDeclarationFragment fdvd = (VariableDeclarationFragment) fd
+ .fragments().get(0);
+ String tmpVarName = fdvd.getName()
+ .getFullyQualifiedName();
- if (fdvd.getInitializer() instanceof MethodInvocation) {
- MethodInvocation fdi = (MethodInvocation) fdvd
- .getInitializer();
- if (isMatchingMethodParamDesc(fdi, bundleId,
- getRBDefinitionDesc())) {
- varName = tmpVarName;
- }
- }
- }
- }
- }
- }
- return false;
- }
+ if (fdvd.getInitializer() instanceof MethodInvocation) {
+ MethodInvocation fdi = (MethodInvocation) fdvd
+ .getInitializer();
+ if (isMatchingMethodParamDesc(fdi, bundleId,
+ getRBDefinitionDesc())) {
+ varName = tmpVarName;
+ }
+ }
+ }
+ }
+ }
+ }
+ return false;
+ }
- }
+ }
- static class LinePreStringsFinder extends ASTVisitor {
- private int position;
- private int line;
- private List<StringLiteral> strings;
- private IDocument document;
+ static class LinePreStringsFinder extends ASTVisitor {
+ private int position;
+ private int line;
+ private List<StringLiteral> strings;
+ private IDocument document;
- public LinePreStringsFinder(int position, IDocument document)
- throws BadLocationException {
- this.document = document;
- this.position = position;
- line = document.getLineOfOffset(position);
- strings = new ArrayList<StringLiteral>();
- }
+ public LinePreStringsFinder(int position, IDocument document)
+ throws BadLocationException {
+ this.document = document;
+ this.position = position;
+ line = document.getLineOfOffset(position);
+ strings = new ArrayList<StringLiteral>();
+ }
- public List<StringLiteral> getStrings() {
- return strings;
- }
+ public List<StringLiteral> getStrings() {
+ return strings;
+ }
- @Override
- public boolean visit(StringLiteral node) {
- try {
- if (line == document.getLineOfOffset(node.getStartPosition())
- && node.getStartPosition() < position) {
- strings.add(node);
- return true;
- }
- } catch (BadLocationException e) {
- }
- return true;
- }
- }
+ @Override
+ public boolean visit(StringLiteral node) {
+ try {
+ if (line == document.getLineOfOffset(node.getStartPosition())
+ && node.getStartPosition() < position) {
+ strings.add(node);
+ return true;
+ }
+ } catch (BadLocationException e) {
+ }
+ return true;
+ }
+ }
- static class StringLiteralFinder extends ASTVisitor {
- private int position;
- private StringLiteral string;
+ static class StringLiteralFinder extends ASTVisitor {
+ private int position;
+ private StringLiteral string;
- public StringLiteralFinder(int position) {
- this.position = position;
- }
+ public StringLiteralFinder(int position) {
+ this.position = position;
+ }
- public StringLiteral getStringLiteral() {
- return string;
- }
+ public StringLiteral getStringLiteral() {
+ return string;
+ }
- @Override
- public boolean visit(StringLiteral node) {
- if (position > node.getStartPosition()
- && position < (node.getStartPosition() + node.getLength())) {
- string = node;
- }
- return true;
- }
- }
+ @Override
+ public boolean visit(StringLiteral node) {
+ if (position > node.getStartPosition()
+ && position < (node.getStartPosition() + node.getLength())) {
+ string = node;
+ }
+ return true;
+ }
+ }
- /**
- * Decides if an enumeration can be refactored or not. In other words, gives
- * the OK for the Proposal to display.
- *
- * @author Alexej Strelzow
- */
- static class Cal10nEnumLiteralFinder extends ASTVisitor {
- private int position;
- private String projectName;
+ /**
+ * Decides if an enumeration can be refactored or not. In other words, gives
+ * the OK for the Proposal to display.
+ *
+ * @author Alexej Strelzow
+ */
+ static class Cal10nEnumLiteralFinder extends ASTVisitor {
+ private int position;
+ private String projectName;
- private String[] metaData;
+ private String[] metaData;
- // [0] resourceBunldeId
- // [1] key value
- // [2] relative path (to the project) of the enum file
+ // [0] resourceBunldeId
+ // [1] key value
+ // [2] relative path (to the project) of the enum file
- /**
- * Constructor.
- *
- * @param position
- * The position of the CTRL + Shift + Space operation
- */
- public Cal10nEnumLiteralFinder(String projectName, int position) {
- this.position = position;
- this.projectName = projectName;
- }
+ /**
+ * Constructor.
+ *
+ * @param position
+ * The position of the CTRL + Shift + Space operation
+ */
+ public Cal10nEnumLiteralFinder(String projectName, int position) {
+ this.position = position;
+ this.projectName = projectName;
+ }
- /**
- * Following constraints must be fulfilled to make an enum
- * "refactorable":<br>
- * <ol>
- * <li>It must be known by the system (stored in a resource bundle file)
- * </li>
- * <li>It must be used by the class ch.qos.cal10n.IMessageConveyor
- * (Cal10n framework)</li>
- * </ol>
- *
- * {@inheritDoc}
- */
- @Override
- public boolean visit(MethodInvocation node) {
+ /**
+ * Following constraints must be fulfilled to make an enum
+ * "refactorable":<br>
+ * <ol>
+ * <li>It must be known by the system (stored in a resource bundle file)
+ * </li>
+ * <li>It must be used by the class ch.qos.cal10n.IMessageConveyor
+ * (Cal10n framework)</li>
+ * </ol>
+ *
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean visit(MethodInvocation node) {
- boolean isCal10nCall = "ch.qos.cal10n.IMessageConveyor".equals(node
- .resolveMethodBinding().getDeclaringClass()
- .getQualifiedName());
+ boolean isCal10nCall = "ch.qos.cal10n.IMessageConveyor".equals(node
+ .resolveMethodBinding().getDeclaringClass()
+ .getQualifiedName());
- if (!isCal10nCall) {
- if (node.arguments().size() == 0) {
- return false;
- } else {
- return true; // because the Cal10n call may be an argument!
- }
- } else {
- int startPosition = node.getStartPosition();
- int length = startPosition + node.getLength();
- if (startPosition < this.position && length > position
- && !node.arguments().isEmpty()
- && node.arguments().get(0) instanceof QualifiedName) {
- QualifiedName qName = (QualifiedName) node.arguments().get(
- 0);
- startPosition = qName.getStartPosition();
- length = startPosition + qName.getLength();
+ if (!isCal10nCall) {
+ if (node.arguments().size() == 0) {
+ return false;
+ } else {
+ return true; // because the Cal10n call may be an argument!
+ }
+ } else {
+ int startPosition = node.getStartPosition();
+ int length = startPosition + node.getLength();
+ if (startPosition < this.position && length > position
+ && !node.arguments().isEmpty()
+ && node.arguments().get(0) instanceof QualifiedName) {
+ QualifiedName qName = (QualifiedName) node.arguments().get(
+ 0);
+ startPosition = qName.getStartPosition();
+ length = startPosition + qName.getLength();
- if (startPosition < this.position && length > position) {
- String resourceBundleId = getResourceBundleId(qName);
- String keyName = qName.getName().toString();
+ if (startPosition < this.position && length > position) {
+ String resourceBundleId = getResourceBundleId(qName);
+ String keyName = qName.getName().toString();
- if (isKnownBySystem(resourceBundleId, keyName)) {
- String path = qName.resolveTypeBinding()
- .getJavaElement().getResource()
- .getFullPath().toPortableString();
- this.metaData = new String[3];
- this.metaData[0] = resourceBundleId;
- this.metaData[1] = keyName;
- this.metaData[2] = path;
- }
- }
- }
- }
+ if (isKnownBySystem(resourceBundleId, keyName)) {
+ String path = qName.resolveTypeBinding()
+ .getJavaElement().getResource()
+ .getFullPath().toPortableString();
+ this.metaData = new String[3];
+ this.metaData[0] = resourceBundleId;
+ this.metaData[1] = keyName;
+ this.metaData[2] = path;
+ }
+ }
+ }
+ }
- return false;
- }
+ return false;
+ }
- private boolean isKnownBySystem(String resourceBundleId, String keyName) {
- IMessagesBundleGroup messagesBundleGroup = RBManager.getInstance(
- this.projectName).getMessagesBundleGroup(resourceBundleId);
- return messagesBundleGroup.containsKey(keyName);
- }
+ private boolean isKnownBySystem(String resourceBundleId, String keyName) {
+ IMessagesBundleGroup messagesBundleGroup = RBManager.getInstance(
+ this.projectName).getMessagesBundleGroup(resourceBundleId);
+ return messagesBundleGroup.containsKey(keyName);
+ }
- private String getResourceBundleId(QualifiedName qName) {
- IAnnotationBinding[] annotations = qName.resolveTypeBinding()
- .getAnnotations();
- for (IAnnotationBinding annotation : annotations) {
- if ("BaseName".equals(annotation.getName())) {
- return (String) annotation.getAllMemberValuePairs()[0]
- .getValue();
- }
- }
+ private String getResourceBundleId(QualifiedName qName) {
+ IAnnotationBinding[] annotations = qName.resolveTypeBinding()
+ .getAnnotations();
+ for (IAnnotationBinding annotation : annotations) {
+ if ("BaseName".equals(annotation.getName())) {
+ return (String) annotation.getAllMemberValuePairs()[0]
+ .getValue();
+ }
+ }
- return null;
- }
+ return null;
+ }
- public String[] getMetaData() {
- return metaData;
- }
- }
+ public String[] getMetaData() {
+ return metaData;
+ }
+ }
- /**
- * Returns whether a refactoring proposal will be shown or not.
- *
- * @param projectName
- * The name of the project the cu is in
- * @param cu
- * The {@link CompilationUnit} to analyze
- * @param i
- * The starting point to begin the analysis
- * @return Meta data of the enum key to refactor or null
- */
- public static String[] getCal10nEnumLiteralDataAtPos(String projectName,
- CompilationUnit cu, int i) {
- Cal10nEnumLiteralFinder enumFinder = new Cal10nEnumLiteralFinder(
- projectName, i);
- cu.accept(enumFinder);
- return enumFinder.getMetaData();
- }
+ /**
+ * Returns whether a refactoring proposal will be shown or not.
+ *
+ * @param projectName
+ * The name of the project the cu is in
+ * @param cu
+ * The {@link CompilationUnit} to analyze
+ * @param i
+ * The starting point to begin the analysis
+ * @return Meta data of the enum key to refactor or null
+ */
+ public static String[] getCal10nEnumLiteralDataAtPos(String projectName,
+ CompilationUnit cu, int i) {
+ Cal10nEnumLiteralFinder enumFinder = new Cal10nEnumLiteralFinder(
+ projectName, i);
+ cu.accept(enumFinder);
+ return enumFinder.getMetaData();
+ }
}