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 fix: Invalid use of type access generics #1454

Merged
merged 3 commits into from
Sep 26, 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
17 changes: 14 additions & 3 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import spoon.SpoonException;
import spoon.compiler.Environment;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtAnnotationFieldAccess;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtArrayRead;
Expand Down Expand Up @@ -482,7 +483,12 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
printer.write(" ");
printer.write(OperatorHelper.getOperatorText(operator.getKind()));
printer.write(" ");
scan(operator.getRightHandOperand());
try (Writable _context = context.modify()) {
if (operator.getKind() == BinaryOperatorKind.INSTANCEOF) {
_context.forceWildcardGenerics(true);
}
scan(operator.getRightHandOperand());
}
exitCtExpression(operator);
}

Expand Down Expand Up @@ -778,7 +784,7 @@ public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
private <T> void printCtFieldAccess(CtFieldAccess<T> f) {
enterCtExpression(f);
try (Writable _context = context.modify()) {
if (f.getVariable().isStatic() && f.getTarget() instanceof CtTypeAccess) {
if ((f.getVariable().isStatic() || "class".equals(f.getVariable().getSimpleName())) && f.getTarget() instanceof CtTypeAccess) {
_context.ignoreGenerics(true);
}
CtExpression<?> target = f.getTarget();
Expand Down Expand Up @@ -1374,7 +1380,12 @@ public <T> void visitCtLambda(CtLambda<T> lambda) {
@Override
public <T, E extends CtExpression<?>> void visitCtExecutableReferenceExpression(CtExecutableReferenceExpression<T, E> expression) {
enterCtExpression(expression);
scan(expression.getTarget());
try (Writable _context = context.modify()) {
if (expression.getExecutable().isStatic()) {
_context.ignoreGenerics(true);
}
scan(expression.getTarget());
}
printer.write("::");
if (expression.getExecutable().isConstructor()) {
printer.write("new");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ public void writeActualTypeArguments(CtActualTypeContainer ctGenericElementRefer
for (CtTypeReference<?> argument : arguments) {
if (!argument.isImplicit()) {
lp.printSeparatorIfAppropriate();
prettyPrinter.scan(argument);
if (prettyPrinter.context.forceWildcardGenerics()) {
printer.write('?');
} else {
prettyPrinter.scan(argument);
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/spoon/reflect/visitor/PrintingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class PrintingContext {
private long SKIP_ARRAY = 1 << 2;
private long IGNORE_STATIC_ACCESS = 1 << 3;
private long IGNORE_ENCLOSING_CLASS = 1 << 4;
private long FORCE_WILDCARD_GENERICS = 1 << 5;

private long state;

Expand All @@ -49,6 +50,9 @@ public boolean ignoreStaticAccess() {
public boolean ignoreEnclosingClass() {
return (state & IGNORE_ENCLOSING_CLASS) != 0L;
}
public boolean forceWildcardGenerics() {
return (state & FORCE_WILDCARD_GENERICS) != 0L;
}

public class Writable implements AutoCloseable {
private long oldState;
Expand Down Expand Up @@ -81,6 +85,10 @@ public <T extends Writable> T ignoreEnclosingClass(boolean v) {
setState(IGNORE_ENCLOSING_CLASS, v);
return (T) this;
}
public <T extends Writable> T forceWildcardGenerics(boolean v) {
setState(FORCE_WILDCARD_GENERICS, v);
return (T) this;
}
private void setState(long mask, boolean v) {
state = v ? state | mask : state & ~mask;
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1041,5 +1041,9 @@ public void substituteTypeAccessReference() throws Exception {
assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getParameters().get(0).getType().toString());
assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.out", method.getBody().getStatement(0).toString());
assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date> ret = new spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>()", method.getBody().getStatement(1).toString());
assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.currentTimeMillis()", method.getBody().getStatement(2).toString());
assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.class", method.getBody().getStatement(3).toString());
assertEquals("o = (o) instanceof spoon.test.template.TypeReferenceClassAccess.Example<?>", method.getBody().getStatement(4).toString());
assertEquals("java.util.function.Supplier<java.lang.Long> p = spoon.test.template.TypeReferenceClassAccess.Example::currentTimeMillis", method.getBody().getStatement(5).toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package spoon.test.template.testclasses;

import java.util.function.Supplier;

import spoon.reflect.reference.CtTypeReference;
import spoon.template.ExtensionTemplate;
import spoon.template.Local;
Expand All @@ -11,6 +13,10 @@ public class TypeReferenceClassAccessTemplate extends ExtensionTemplate {
$Type$ someMethod($Type$ param) {
o = $Type$.out;
$Type$ ret = new $Type$();
o = $Type$.currentTimeMillis();
o = $Type$.class;
o = o instanceof $Type$;
Supplier<Long> p = $Type$::currentTimeMillis;
return ret;
}

Expand Down