From 6f454bb58e52a3caf4e50efc46b5ae9df41f0dbd Mon Sep 17 00:00:00 2001 From: Pavel Vojtechovsky Date: Sat, 20 May 2017 21:45:02 +0200 Subject: [PATCH] replace string literals by constants --- .../java/spoon/reflect/reference/CtExecutableReference.java | 2 ++ src/main/java/spoon/reflect/visitor/JavaIdentifiers.java | 4 +++- src/main/java/spoon/support/reflect/code/CtStatementImpl.java | 3 ++- .../spoon/support/reflect/declaration/CtConstructorImpl.java | 3 ++- .../spoon/support/reflect/eval/VisitorPartialEvaluator.java | 3 ++- .../support/reflect/reference/CtExecutableReferenceImpl.java | 4 ++-- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/spoon/reflect/reference/CtExecutableReference.java b/src/main/java/spoon/reflect/reference/CtExecutableReference.java index 91a9f723cce..d8c9b500d22 100644 --- a/src/main/java/spoon/reflect/reference/CtExecutableReference.java +++ b/src/main/java/spoon/reflect/reference/CtExecutableReference.java @@ -33,6 +33,8 @@ public interface CtExecutableReference extends CtReference, CtActualTypeConta String CONSTRUCTOR_NAME = ""; + String LAMBDA_NAME_PREFIX = "lambda$"; + String UNKNOWN_TYPE = ""; /** diff --git a/src/main/java/spoon/reflect/visitor/JavaIdentifiers.java b/src/main/java/spoon/reflect/visitor/JavaIdentifiers.java index 3f8f46fde08..02595199ffd 100644 --- a/src/main/java/spoon/reflect/visitor/JavaIdentifiers.java +++ b/src/main/java/spoon/reflect/visitor/JavaIdentifiers.java @@ -21,6 +21,8 @@ import java.util.Set; import java.util.StringTokenizer; +import spoon.reflect.reference.CtExecutableReference; + /** * This enum defines the Java keywords and some helper method to determine if * some strings are Java identifiers. @@ -99,7 +101,7 @@ public static boolean isLegalJavaExecutableIdentifier(String string) { if (string == null) { return false; } - if (string.equals("")) { + if (string.equals(CtExecutableReference.CONSTRUCTOR_NAME)) { return true; } return isLegalJavaIdentifier(string); diff --git a/src/main/java/spoon/support/reflect/code/CtStatementImpl.java b/src/main/java/spoon/support/reflect/code/CtStatementImpl.java index ca5a6fdba28..55c3c5babfd 100644 --- a/src/main/java/spoon/support/reflect/code/CtStatementImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtStatementImpl.java @@ -29,6 +29,7 @@ import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtExecutable; import spoon.reflect.declaration.ParentNotInitializedException; +import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.visitor.CtInheritanceScanner; import java.util.ArrayList; @@ -69,7 +70,7 @@ public static void insertBefore(CtStatement target, CtStatementList statementsTo } try { if (target.getParent(CtConstructor.class) != null) { - if (target instanceof CtInvocation && ((CtInvocation) target).getExecutable().getSimpleName().startsWith("")) { + if (target instanceof CtInvocation && ((CtInvocation) target).getExecutable().getSimpleName().startsWith(CtExecutableReference.CONSTRUCTOR_NAME)) { throw new SpoonException("cannot insert a statement before a super or this invocation."); } } diff --git a/src/main/java/spoon/support/reflect/declaration/CtConstructorImpl.java b/src/main/java/spoon/support/reflect/declaration/CtConstructorImpl.java index 478132c71a6..7a961edd42c 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtConstructorImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtConstructorImpl.java @@ -25,6 +25,7 @@ import spoon.reflect.declaration.CtTypeParameter; import spoon.reflect.declaration.CtTypedElement; import spoon.reflect.declaration.ModifierKind; +import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.reference.CtTypeParameterReference; import spoon.reflect.reference.CtTypeReference; import spoon.reflect.visitor.CtVisitor; @@ -60,7 +61,7 @@ public C setSimpleName(String simpleName) { @Override public String getSimpleName() { - return ""; + return CtExecutableReference.CONSTRUCTOR_NAME; } @Override diff --git a/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java b/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java index f4b7e847eb3..9f9863f983b 100644 --- a/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java +++ b/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java @@ -49,6 +49,7 @@ import spoon.reflect.declaration.CtVariable; import spoon.reflect.declaration.ModifierKind; import spoon.reflect.eval.PartialEvaluator; +import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.reference.CtFieldReference; import spoon.reflect.reference.CtTypeReference; import spoon.reflect.visitor.CtScanner; @@ -380,7 +381,7 @@ public void visitCtInvocation(CtInvocation invocation) { i.addArgument(re); } // do not partially evaluate super(...) - if (i.getExecutable().getSimpleName().equals("")) { + if (i.getExecutable().getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME)) { setResult(i); return; } diff --git a/src/main/java/spoon/support/reflect/reference/CtExecutableReferenceImpl.java b/src/main/java/spoon/support/reflect/reference/CtExecutableReferenceImpl.java index 262f40a94f9..a194b824211 100644 --- a/src/main/java/spoon/support/reflect/reference/CtExecutableReferenceImpl.java +++ b/src/main/java/spoon/support/reflect/reference/CtExecutableReferenceImpl.java @@ -158,13 +158,13 @@ private CtExecutable getCtExecutable(CtType typeDecl) { return null; } CtExecutable method = typeDecl.getMethod(getSimpleName(), parameters.toArray(new CtTypeReferenceImpl[parameters.size()])); - if ((method == null) && (typeDecl instanceof CtClass) && (getSimpleName().equals(""))) { + if ((method == null) && (typeDecl instanceof CtClass) && (getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME))) { try { return (CtExecutable) ((CtClass) typeDecl).getConstructor(parameters.toArray(new CtTypeReferenceImpl[parameters.size()])); } catch (ClassCastException e) { Launcher.LOGGER.error(e.getMessage(), e); } - } else if (method == null && getSimpleName().startsWith("lambda$")) { + } else if (method == null && getSimpleName().startsWith(CtExecutableReference.LAMBDA_NAME_PREFIX)) { final List> elements = (List>) typeDecl.getElements(new NameFilter>(getSimpleName())); if (elements.size() == 0) { return null;