Skip to content

Commit

Permalink
refactor: Use get/setType methods in executable in CtNewClass.
Browse files Browse the repository at this point in the history
In CtNewClass#getType(), we use getExecutable().getType.
In CtNewClass#setType(), we throw an UnsupportedOperationException.
  • Loading branch information
GerardPaligot committed Nov 20, 2015
1 parent 1b99d90 commit 8a0927b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
48 changes: 25 additions & 23 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,14 @@ void enter(CtElement e, ASTNode node) {
((CtStatement) current).setLabel(context.label.pop());
}

if (e instanceof CtTypedElement && node instanceof Expression) {
if (((CtTypedElement<?>) e).getType() == null && !(e instanceof CtInvocation)) {
((CtTypedElement<Object>) e).setType(references.getTypeReference(((Expression) node).resolvedType));
try {
if (e instanceof CtTypedElement && node instanceof Expression) {
if (((CtTypedElement<?>) e).getType() == null) {
((CtTypedElement<Object>) e).setType(references.getTypeReference(((Expression) node).resolvedType));
}
}
} catch (UnsupportedOperationException ignore) {
// For some element, we throw an UnsupportedOperationException when we call setType().
}

}
Expand Down Expand Up @@ -1654,6 +1658,22 @@ public boolean visit(AllocationExpression allocationExpression, BlockScope scope
}

private <T extends CtConstructorCall<Object>> T buildCommonPartForCtNewClassAndCtConstructorCall(AllocationExpression allocationExpression, BlockScope scope, T constructorCall) {
if (allocationExpression.binding != null) {
constructorCall.setExecutable(references.getExecutableReference(allocationExpression.binding));
} else {
final CtExecutableReference<Object> ref = factory.Core().createExecutableReference();
ref.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME);
ref.setType(references.getTypeReference(null, allocationExpression.type));
ref.setDeclaringType(references.getTypeReference(null, allocationExpression.type));

final List<CtTypeReference<?>> parameters = new ArrayList<CtTypeReference<?>>(allocationExpression.argumentTypes.length);
for (TypeBinding b : allocationExpression.argumentTypes) {
parameters.add(references.getTypeReference(b));
}
ref.setParameters(parameters);
constructorCall.setExecutable(ref);
}

if (allocationExpression.type != null) {
final TypeReference[][] typeArguments = allocationExpression.type.getTypeArguments();
// If typeArguments are null or empty, we have an element with a generic type.
Expand All @@ -1668,28 +1688,10 @@ private <T extends CtConstructorCall<Object>> T buildCommonPartForCtNewClassAndC
}
}
}
constructorCall.setType(references.getTypeReference(allocationExpression.type.resolvedType));
constructorCall.getExecutable().setType(references.getTypeReference(allocationExpression.type.resolvedType));
context.isGenericTypeExplicit = true;
} else if (allocationExpression.expectedType() != null) {
constructorCall.setType(references.getTypeReference(allocationExpression.expectedType()));
}
if (allocationExpression.binding != null) {
constructorCall.setExecutable(references.getExecutableReference(allocationExpression.binding));
} else {
final CtExecutableReference<Object> ref = factory.Core().createExecutableReference();
ref.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME);
ref.setType(references.getTypeReference(null, allocationExpression.type));
ref.setDeclaringType(references.getTypeReference(null, allocationExpression.type));

final List<CtTypeReference<?>> parameters = new ArrayList<CtTypeReference<?>>(allocationExpression.argumentTypes.length);
for (TypeBinding b : allocationExpression.argumentTypes) {
parameters.add(references.getTypeReference(b));
}
ref.setParameters(parameters);
constructorCall.setExecutable(ref);
}
if (constructorCall.getExecutable() != null) {
constructorCall.getExecutable().setType(constructorCall.getType());
constructorCall.getExecutable().setType(references.getTypeReference(allocationExpression.expectedType()));
}

if (allocationExpression.genericTypeArguments() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtGenericElementReference;
import spoon.reflect.reference.CtTypeReference;
Expand Down Expand Up @@ -152,4 +153,14 @@ public boolean removeActualTypeArgument(CtTypeReference<?> actualTypeArgument) {
return actualTypeArguments != CtElementImpl.<CtTypeReference<?>>emptyList()
&& actualTypeArguments.remove(actualTypeArgument);
}

@Override
public CtTypeReference<T> getType() {
return getExecutable() == null ? null : getExecutable().getType();
}

@Override
public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {
throw new UnsupportedOperationException("Uses getExecutable().setType(CtTypeReference<T>)");
}
}
3 changes: 2 additions & 1 deletion src/test/java/spoon/test/executable/ExecutableRefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public void testSameTypeInConstructorCallBetweenItsObjectAndItsExecutable() {

for (CtConstructorCall constructorCall : ctConstructorCalls) {
assertNotNull(constructorCall.getExecutable());
assertEquals(constructorCall.getExecutable().getType(), constructorCall.getType());
}

TestUtils.canBeBuilt("./target/executable", 8, true);
}

private CtAbstractInvocation<?> getInvocationFromMethod(String methodName) throws Exception {
Expand Down

0 comments on commit 8a0927b

Please sign in to comment.