Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: replace string literals by constants #1316

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface CtExecutableReference<T> extends CtReference, CtActualTypeConta

String CONSTRUCTOR_NAME = "<init>";

String LAMBDA_NAME_PREFIX = "lambda$";

String UNKNOWN_TYPE = "<unknown>";

/**
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spoon/reflect/visitor/JavaIdentifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -99,7 +101,7 @@ public static boolean isLegalJavaExecutableIdentifier(String string) {
if (string == null) {
return false;
}
if (string.equals("<init>")) {
if (string.equals(CtExecutableReference.CONSTRUCTOR_NAME)) {
return true;
}
return isLegalJavaIdentifier(string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<init>")) {
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.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +61,7 @@ public <C extends CtNamedElement> C setSimpleName(String simpleName) {

@Override
public String getSimpleName() {
return "<init>";
return CtExecutableReference.CONSTRUCTOR_NAME;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -380,7 +381,7 @@ public <T> void visitCtInvocation(CtInvocation<T> invocation) {
i.addArgument(re);
}
// do not partially evaluate super(...)
if (i.getExecutable().getSimpleName().equals("<init>")) {
if (i.getExecutable().getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME)) {
setResult(i);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ private CtExecutable<T> getCtExecutable(CtType<?> typeDecl) {
return null;
}
CtExecutable<T> method = typeDecl.getMethod(getSimpleName(), parameters.toArray(new CtTypeReferenceImpl<?>[parameters.size()]));
if ((method == null) && (typeDecl instanceof CtClass) && (getSimpleName().equals("<init>"))) {
if ((method == null) && (typeDecl instanceof CtClass) && (getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME))) {
try {
return (CtExecutable<T>) ((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<CtLambda<T>> elements = (List<CtLambda<T>>) typeDecl.getElements(new NameFilter<CtLambda<T>>(getSimpleName()));
if (elements.size() == 0) {
return null;
Expand Down