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

feature: partial evaluation of new Object[]{...}.length -> Literal<Number> #1750

Merged
merged 3 commits into from
Nov 25, 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
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public class TypeFactory extends SubFactory {
public final CtTypeReference<List> LIST = createReference(List.class);
public final CtTypeReference<Set> SET = createReference(Set.class);
public final CtTypeReference<Map> MAP = createReference(Map.class);
public final CtTypeReference<Enum> ENUM = createReference(Enum.class);

private final Map<Class<?>, CtType<?>> shadowCache = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtSynchronized;
Expand Down Expand Up @@ -101,7 +102,9 @@ public <R extends CtElement> R evaluate(R element) {
}
element.accept(this);
if (result != null) {
result.setParent(element.getParent());
if (element.isParentInitialized()) {
result.setParent(element.getParent());
}
return (R) result;
}

Expand Down Expand Up @@ -283,7 +286,16 @@ private <T> void visitFieldAccess(CtFieldAccess<T> fieldAccess) {
return;
}
}
if (fieldAccess.getFactory().Type().createReference(Enum.class)
if ("length".equals(fieldAccess.getVariable().getSimpleName())) {
CtExpression<?> target = fieldAccess.getTarget();
if (target instanceof CtNewArray<?>) {
CtNewArray<?> newArr = (CtNewArray<?>) target;
CtLiteral<Number> literal = fieldAccess.getFactory().createLiteral(newArr.getElements().size());
setResult(literal);
return;
}
}
if (fieldAccess.getFactory().Type().ENUM
.isSubtypeOf(fieldAccess.getVariable().getDeclaringType())) {
CtLiteral<CtFieldReference<?>> l = fieldAccess.getFactory().Core().createLiteral();
l.setValue(fieldAccess.getVariable());
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/spoon/test/eval/EvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public void testStringConcatenation() throws Exception {
assertEquals("// if removed", b.getStatements().get(0).toString());
}

@Test
public void testArrayLength() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("testArray").get(0).getBody();
assertEquals(1, b.getStatements().size());
b = b.partiallyEvaluate();
assertEquals("// if removed", b.getStatements().get(0).toString());
}

@Test
public void testVisitorPartialEvaluator_binary() throws Exception {
Launcher launcher = new Launcher();
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/eval/ToEvaluate.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public static void testInts() {
}
}

public static void testArray() {
// all the following code will be removed by the partial evaluator
if (new String[]{"a", null, "b"}.length == 11) {
System.out.println("dead code");
}
}
}