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

CtGenericElement#formalTypeParameters use CtTypeParameterReference #595

Merged
merged 3 commits into from
Apr 22, 2016
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
12 changes: 6 additions & 6 deletions src/main/java/spoon/reflect/declaration/CtGenericElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package spoon.reflect.declaration;

import java.util.List;
import spoon.reflect.reference.CtTypeParameterReference;

import spoon.reflect.reference.CtTypeReference;
import java.util.List;

/**
* This abstract element defines a declaration that accepts formal type
Expand All @@ -28,27 +28,27 @@ public interface CtGenericElement extends CtElement {
/**
* Returns the formal type parameters of this generic element.
*/
List<CtTypeReference<?>> getFormalTypeParameters();
List<CtTypeParameterReference> getFormalTypeParameters();

/**
* Sets the type parameters of this generic element.
*/
<T extends CtGenericElement> T setFormalTypeParameters(List<CtTypeReference<?>> formalTypeParameters);
<T extends CtGenericElement> T setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters);

/**
* Add a type parameter to this generic element.
*
* @param formalTypeParameter
* @return <tt>true</tt> if this element changed as a result of the call
*/
<T extends CtGenericElement> T addFormalTypeParameter(CtTypeReference<?> formalTypeParameter);
<T extends CtGenericElement> T addFormalTypeParameter(CtTypeParameterReference formalTypeParameter);

/**
* Removes a type parameters from this generic element.
*
* @param formalTypeParameter
* @return <tt>true</tt> if this element changed as a result of the call
*/
boolean removeFormalTypeParameter(CtTypeReference<?> formalTypeParameter);
boolean removeFormalTypeParameter(CtTypeParameterReference formalTypeParameter);

}
68 changes: 21 additions & 47 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,22 @@ public <T> void visitCtConstructorCall(CtConstructorCall<T> ctConstructorCall) {
enterCtStatement(ctConstructorCall);
enterCtExpression(ctConstructorCall);

printConstructorCall(ctConstructorCall);

exitCtExpression(ctConstructorCall);
}

public <T> void visitCtNewClass(CtNewClass<T> newClass) {
enterCtStatement(newClass);
enterCtExpression(newClass);

printConstructorCall(newClass);

scan(newClass.getAnonymousClass());
exitCtExpression(newClass);
}

private <T> void printConstructorCall(CtConstructorCall<T> ctConstructorCall) {
if (ctConstructorCall.getTarget() != null) {
scan(ctConstructorCall.getTarget()).write(".");
context.ignoreEnclosingClass = true;
Expand All @@ -1744,19 +1760,14 @@ public <T> void visitCtConstructorCall(CtConstructorCall<T> ctConstructorCall) {
context.ignoreEnclosingClass = false;

write("(");
boolean remove = false;
for (CtCodeElement e : ctConstructorCall.getArguments()) {
scan(e);
write(" , ");
remove = true;
for (CtCodeElement exp : ctConstructorCall.getArguments()) {
scan(exp);
write(", ");
}
if (remove) {
if (ctConstructorCall.getArguments().size() > 0) {
removeLastChar();
}

write(")");

exitCtExpression(ctConstructorCall);
}

/**
Expand Down Expand Up @@ -1789,43 +1800,6 @@ private <T> boolean hasDeclaringTypeWithGenerics(CtTypeReference<T> reference) {
return hasDeclaringTypeWithGenerics(reference.getDeclaringType());
}

public <T> void visitCtNewClass(CtNewClass<T> newClass) {
enterCtStatement(newClass);
enterCtExpression(newClass);

if (newClass.getTarget() != null) {
scan(newClass.getTarget()).write(".");
}

write("new ");
if (newClass.getAnonymousClass().getSuperclass() != null) {
if (hasDeclaringTypeWithGenerics(newClass.getAnonymousClass().getSuperclass())) {
context.ignoreEnclosingClass = true;
}
scan(newClass.getAnonymousClass().getSuperclass());
} else if (newClass.getAnonymousClass().getSuperInterfaces().size() > 0) {
for (CtTypeReference<?> ref : newClass.getAnonymousClass().getSuperInterfaces()) {
if (hasDeclaringTypeWithGenerics(ref)) {
context.ignoreEnclosingClass = true;
}
scan(ref);
}
}
context.ignoreEnclosingClass = false;

write("(");
for (CtExpression<?> exp : newClass.getArguments()) {
scan(exp);
write(", ");
}
if (newClass.getArguments().size() > 0) {
removeLastChar();
}
write(")");
scan(newClass.getAnonymousClass());
exitCtExpression(newClass);
}

@Override
public <T> void visitCtLambda(CtLambda<T> lambda) {
enterCtExpression(lambda);
Expand Down Expand Up @@ -2209,7 +2183,7 @@ public DefaultJavaPrettyPrinter writeAnnotationElement(Factory factory, Object v
* List of formal type parameters.
* @return current instance of the {@link DefaultJavaPrettyPrinter}
*/
public DefaultJavaPrettyPrinter writeFormalTypeParameters(Collection<CtTypeReference<?>> params) {
public DefaultJavaPrettyPrinter writeFormalTypeParameters(Collection<CtTypeParameterReference> params) {
if (params == null) {
return this;
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1384,21 +1384,6 @@ CtType<?> createType(TypeDeclaration typeDeclaration) {
if (typeDeclaration.superclass != null) {
((CtClass) type).setSuperclass(buildTypeReference(typeDeclaration.superclass, typeDeclaration.scope));
}

// If the current class is an anonymous class with a super interface and generic types, we add generic
// types
// in the current class.
if (typeDeclaration.binding.isAnonymousType() && typeDeclaration.superInterfaces != null) {
final TypeReference superInterface = typeDeclaration.superInterfaces[0];
if (superInterface.resolvedType instanceof ParameterizedTypeBinding) {
final ParameterizedTypeBinding resolvedType = (ParameterizedTypeBinding) superInterface.resolvedType;
if (resolvedType.arguments != null) {
for (TypeBinding b : resolvedType.arguments) {
type.addFormalTypeParameter(references.getTypeReference(b));
}
}
}
}
}
if (type instanceof CtClass) {
if (typeDeclaration.binding.isAnonymousType() || (typeDeclaration.binding instanceof LocalTypeBinding && typeDeclaration.binding.enclosingMethod() != null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@
*/
package spoon.support.reflect.declaration;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import spoon.reflect.declaration.CtAnnotationType;
import spoon.reflect.declaration.CtGenericElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* The implementation for {@link spoon.reflect.declaration.CtAnnotationType}.
*
Expand Down Expand Up @@ -84,17 +85,17 @@ public <M> boolean removeMethod(CtMethod<M> method) {
}

@Override
public <C extends CtGenericElement> C setFormalTypeParameters(List<CtTypeReference<?>> formalTypeParameters) {
public <C extends CtGenericElement> C setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters) {
throw new UnsupportedOperationException("You can't have generics in an annotation.");
}

@Override
public <C extends CtGenericElement> C addFormalTypeParameter(CtTypeReference<?> formalTypeParameter) {
public <C extends CtGenericElement> C addFormalTypeParameter(CtTypeParameterReference formalTypeParameter) {
throw new UnsupportedOperationException("You can't have generics in an annotation.");
}

@Override
public boolean removeFormalTypeParameter(CtTypeReference<?> formalTypeParameter) {
public boolean removeFormalTypeParameter(CtTypeParameterReference formalTypeParameter) {
throw new UnsupportedOperationException("You can't have generics in an annotation.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;

Expand All @@ -31,13 +32,12 @@
import java.util.List;
import java.util.Set;

import static spoon.reflect.ModelElementContainerDefaultCapacities
.CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY;
import static spoon.reflect.ModelElementContainerDefaultCapacities.CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY;

public class CtConstructorImpl<T> extends CtExecutableImpl<T> implements CtConstructor<T> {
private static final long serialVersionUID = 1L;

List<CtTypeReference<?>> formalTypeParameters = emptyList();
List<CtTypeParameterReference> formalTypeParameters = emptyList();

Set<ModifierKind> modifiers = CtElementImpl.emptySet();

Expand Down Expand Up @@ -76,42 +76,38 @@ public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {
}

@Override
public List<CtTypeReference<?>> getFormalTypeParameters() {
public List<CtTypeParameterReference> getFormalTypeParameters() {
return formalTypeParameters;
}

@Override
public <T extends CtGenericElement> T addFormalTypeParameter(CtTypeReference<?> formalTypeParameter) {
public <T extends CtGenericElement> T addFormalTypeParameter(CtTypeParameterReference formalTypeParameter) {
if (formalTypeParameter == null) {
return (T) this;
}
if (formalTypeParameters == CtElementImpl.<CtTypeReference<?>>emptyList()) {
formalTypeParameters = new ArrayList<CtTypeReference<?>>(
CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
if (formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
formalTypeParameters = new ArrayList<CtTypeParameterReference>(CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
formalTypeParameter.setParent(this);
formalTypeParameters.add(formalTypeParameter);
return (T) this;
}

@Override
public <T extends CtGenericElement> T setFormalTypeParameters(List<CtTypeReference<?>> formalTypeParameters) {
if (this.formalTypeParameters == CtElementImpl.<CtTypeReference<?>>emptyList()) {
this.formalTypeParameters = new ArrayList<CtTypeReference<?>>(
CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
public <T extends CtGenericElement> T setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters) {
if (this.formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
this.formalTypeParameters = new ArrayList<CtTypeParameterReference>(CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
this.formalTypeParameters.clear();
for (CtTypeReference<?> formalTypeParameter : formalTypeParameters) {
for (CtTypeParameterReference formalTypeParameter : formalTypeParameters) {
addFormalTypeParameter(formalTypeParameter);
}
return (T) this;
}

@Override
public boolean removeFormalTypeParameter(CtTypeReference<?> formalTypeParameter) {
return formalTypeParameter != null
&& formalTypeParameters != CtElementImpl.<CtTypeReference<?>>emptyList()
&& formalTypeParameters.remove(formalTypeParameter);
public boolean removeFormalTypeParameter(CtTypeParameterReference formalTypeParameter) {
return formalTypeParameter != null && formalTypeParameters != CtElementImpl.<CtTypeParameterReference>emptyList() && formalTypeParameters.remove(formalTypeParameter);
}

@Override
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/spoon/support/reflect/declaration/CtMethodImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;

Expand All @@ -45,7 +46,7 @@ public class CtMethodImpl<T> extends CtExecutableImpl<T> implements CtMethod<T>

boolean defaultMethod = false;

List<CtTypeReference<?>> formalTypeParameters = emptyList();
List<CtTypeParameterReference> formalTypeParameters = emptyList();

Set<ModifierKind> modifiers = CtElementImpl.emptySet();

Expand Down Expand Up @@ -89,42 +90,38 @@ public <C extends CtMethod<T>> C setDefaultMethod(boolean defaultMethod) {
}

@Override
public List<CtTypeReference<?>> getFormalTypeParameters() {
public List<CtTypeParameterReference> getFormalTypeParameters() {
return formalTypeParameters;
}

@Override
public <T extends CtGenericElement> T addFormalTypeParameter(CtTypeReference<?> formalTypeParameter) {
public <T extends CtGenericElement> T addFormalTypeParameter(CtTypeParameterReference formalTypeParameter) {
if (formalTypeParameter == null) {
return (T) this;
}
if (formalTypeParameters == CtElementImpl.<CtTypeReference<?>>emptyList()) {
formalTypeParameters = new ArrayList<CtTypeReference<?>>(
METHOD_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
if (formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
formalTypeParameters = new ArrayList<CtTypeParameterReference>(METHOD_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
formalTypeParameter.setParent(this);
formalTypeParameters.add(formalTypeParameter);
return (T) this;
}

@Override
public <T extends CtGenericElement> T setFormalTypeParameters(List<CtTypeReference<?>> formalTypeParameters) {
if (this.formalTypeParameters == CtElementImpl.<CtTypeReference<?>>emptyList()) {
this.formalTypeParameters = new ArrayList<CtTypeReference<?>>(
METHOD_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
public <T extends CtGenericElement> T setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters) {
if (this.formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
this.formalTypeParameters = new ArrayList<CtTypeParameterReference>(METHOD_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
this.formalTypeParameters.clear();
for (CtTypeReference<?> formalTypeParameter : formalTypeParameters) {
for (CtTypeParameterReference formalTypeParameter : formalTypeParameters) {
addFormalTypeParameter(formalTypeParameter);
}
return (T) this;
}

@Override
public boolean removeFormalTypeParameter(CtTypeReference<?> formalTypeParameter) {
return formalTypeParameter != null
&& formalTypeParameters != CtElementImpl.<CtTypeReference<?>>emptyList()
&& formalTypeParameters.remove(formalTypeParameter);
public boolean removeFormalTypeParameter(CtTypeParameterReference formalTypeParameter) {
return formalTypeParameter != null && formalTypeParameters != CtElementImpl.<CtTypeParameterReference>emptyList() && formalTypeParameters.remove(formalTypeParameter);
}

@Override
Expand Down
Loading