Skip to content

Commit

Permalink
feat(ast): Introduces new metamodel class CtTypeParameter for represe…
Browse files Browse the repository at this point in the history
…nting a generic type parameter. (Closes #606)
  • Loading branch information
GerardPaligot authored and monperrus committed Aug 24, 2016
1 parent e166275 commit 25f9202
Show file tree
Hide file tree
Showing 47 changed files with 1,157 additions and 130 deletions.
Binary file modified doc/_jekyll/images/structural-elements.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_release/spoon-models.graffle
Binary file not shown.
14 changes: 14 additions & 0 deletions src/main/java/spoon/generating/replace/ReplaceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package spoon.generating.replace;

import spoon.SpoonException;
import spoon.generating.ReplacementVisitorGenerator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
Expand Down Expand Up @@ -76,6 +78,13 @@ public <T> void visitCtMethod(CtMethod<T> element) {
for (int i = 1; i < element.getBody().getStatements().size() - 1; i++) {
CtInvocation inv = element.getBody().getStatement(i);
CtInvocation getter = (CtInvocation) inv.getArguments().get(0);
if (clone.getComments().size() == 0) {
// Add auto-generated comment.
final CtComment comment = factory.Core().createComment();
comment.setCommentType(CtComment.CommentType.INLINE);
comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
clone.addComment(comment);
}
if (excludes.contains(getter.getExecutable().toString())) {
continue;
}
Expand Down Expand Up @@ -116,6 +125,11 @@ private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candid
final CtField field = updateField(listener, setter.getDeclaringType().getReference());
updateConstructor(listener, setter.getDeclaringType().getReference());
updateSetter(factory, (CtMethod<?>) listener.getMethodsByName("set").get(0), getterType, field, setter);
// Add auto-generated comment.
final CtComment comment = factory.Core().createComment();
comment.setCommentType(CtComment.CommentType.INLINE);
comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
listener.addComment(comment);
listeners.put(listenerName, listener);
}

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/spoon/reflect/declaration/CtClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public interface CtClass<T extends Object> extends CtType<T>, CtStatement {
*/
void removeConstructor(CtConstructor<T> constructor);

/**
* Sets the superclass type.
*/
<C extends CtClass<T>> C setSuperclass(CtTypeReference<?> classType);

/**
* Return {@code true} if the referenced type is a anonymous type
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,62 @@

/**
* This abstract element defines a declaration that accepts formal type
* parameters (aka generics).
* parameters (aka generics), such as a CtType (<code>class A&lt;E&gt;</code>), CtMethod or CtConstructor.
*/
public interface CtFormalTypeDeclarer extends CtElement {
/**
* Returns the formal type parameters of this generic element.
*
* @see #getFormalCtTypeParameters()
*/
@Deprecated
List<CtTypeParameterReference> getFormalTypeParameters();

/**
* Returns the formal type parameters of this generic element.
*/
List<CtTypeParameter> getFormalCtTypeParameters();

/**
* Sets the type parameters of this generic element.
*
* @see #setFormalCtTypeParameters(List)
*/
@Deprecated
<T extends CtFormalTypeDeclarer> T setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters);

/**
* Sets the type parameters of this generic element.
*/
<T extends CtFormalTypeDeclarer> T setFormalCtTypeParameters(List<CtTypeParameter> 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
* @see #addFormalCtTypeParameter(CtTypeParameter)
*/
@Deprecated
<T extends CtFormalTypeDeclarer> T addFormalTypeParameter(CtTypeParameterReference formalTypeParameter);

/**
* Add a type parameter to this generic element.
*/
<T extends CtFormalTypeDeclarer> T addFormalCtTypeParameter(CtTypeParameter 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
* @see #removeFormalCtTypeParameter(CtTypeParameter)
*/
@Deprecated
boolean removeFormalTypeParameter(CtTypeParameterReference formalTypeParameter);

/**
* Removes a type parameters from this generic element.
*/
boolean removeFormalCtTypeParameter(CtTypeParameter formalTypeParameter);
}
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtType.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ public interface CtType<T> extends CtNamedElement, CtTypeInformation, CtTypeMemb
*/
<M> boolean removeMethod(CtMethod<M> method);

/**
* Sets the superclass type.
*/
<C extends CtType<T>> C setSuperclass(CtTypeReference<?> superClass);

/**
* Sets the super interfaces of this type.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtTypeParameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.declaration;

import spoon.reflect.reference.CtTypeParameterReference;

/**
* This element defines a declaration of a type parameter (aka generics).
* For example, in class A&lt;E&gt; { ... }, the "E" is modeled as an instance of CtTypeParameter.
*/
public interface CtTypeParameter extends CtType<Object> {
// override the return type
@Override
CtTypeParameterReference getReference();

// override the return type
@Override
CtTypeParameter clone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public <T> CtConstructor<T> create(CtClass<T> target, CtMethod<?> source) {
newConstructor.setBody(method.getBody());
newConstructor.setDocComment(method.getDocComment());
newConstructor.setFormalTypeParameters(method.getFormalTypeParameters());
newConstructor.setFormalCtTypeParameters(method.getFormalCtTypeParameters());
newConstructor.setModifiers(method.getModifiers());
newConstructor.setParameters(method.getParameters());
target.addConstructor(newConstructor);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtCatchVariableReference;
import spoon.reflect.reference.CtExecutableReference;
Expand Down Expand Up @@ -187,6 +188,11 @@ public interface CoreFactory {
*/
<T> CtClass<T> createClass();

/**
* Creates a type parameter declaration.
*/
CtTypeParameter createTypeParameter();

/**
* Creates a conditional expression (<code>boolExpr?ifTrue:ifFalse</code>).
*/
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package spoon.reflect.factory;

import spoon.reflect.code.CtNewClass;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtIntersectionTypeReference;
import spoon.reflect.reference.CtTypeParameterReference;
Expand All @@ -29,6 +31,7 @@
import spoon.support.StandardEnvironment;
import spoon.support.visitor.java.JavaReflectionTreeBuilder;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -314,6 +317,23 @@ public <T> CtTypeReference<T> createReference(CtType<T> type) {
return ref;
}

/**
* Create a reference to a simple type
*/
public CtTypeParameterReference createReference(CtTypeParameter type) {
CtTypeParameterReference ref = factory.Core().createTypeParameterReference();

if (type.getSuperclass() != null) {
ref.setBoundingType(type.getSuperclass().clone());
}

for (CtAnnotation<? extends Annotation> ctAnnotation : type.getAnnotations()) {
ref.addAnnotation(ctAnnotation.clone());
}
ref.setSimpleName(type.getSimpleName());
return ref;
}

/**
* Create a reference to a simple type
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/spoon/reflect/visitor/CtAbstractVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.internal.CtCircularTypeReference;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtCatchVariableReference;
Expand Down Expand Up @@ -179,6 +180,11 @@ public <T> void visitCtClass(CtClass<T> ctClass) {

}

@Override
public void visitCtTypeParameter(CtTypeParameter typeParameter) {

}

@Override
public <T> void visitCtConditional(CtConditional<T> conditional) {

Expand Down
18 changes: 14 additions & 4 deletions src/main/java/spoon/reflect/visitor/CtBiScannerDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public <T> void visitCtClass(final spoon.reflect.declaration.CtClass<T> ctClass)
biScan(ctClass.getAnnotations(), other.getAnnotations());
biScan(ctClass.getSuperclass(), other.getSuperclass());
biScan(ctClass.getSuperInterfaces(), other.getSuperInterfaces());
biScan(ctClass.getFormalTypeParameters(), other.getFormalTypeParameters());
biScan(ctClass.getFormalCtTypeParameters(), other.getFormalCtTypeParameters());
biScan(ctClass.getAnonymousExecutables(), other.getAnonymousExecutables());
biScan(ctClass.getNestedTypes(), other.getNestedTypes());
biScan(ctClass.getFields(), other.getFields());
Expand All @@ -182,6 +182,16 @@ public <T> void visitCtClass(final spoon.reflect.declaration.CtClass<T> ctClass)
exit(ctClass);
}

@Override
public void visitCtTypeParameter(spoon.reflect.declaration.CtTypeParameter typeParameter) {
spoon.reflect.declaration.CtTypeParameter other = ((spoon.reflect.declaration.CtTypeParameter) (stack.peek()));
enter(typeParameter);
biScan(typeParameter.getAnnotations(), other.getAnnotations());
biScan(typeParameter.getSuperclass(), other.getSuperclass());
biScan(typeParameter.getComments(), other.getComments());
exit(typeParameter);
}

public <T> void visitCtConditional(final spoon.reflect.code.CtConditional<T> conditional) {
spoon.reflect.code.CtConditional other = ((spoon.reflect.code.CtConditional) (stack.peek()));
enter(conditional);
Expand All @@ -200,7 +210,7 @@ public <T> void visitCtConstructor(final spoon.reflect.declaration.CtConstructor
biScan(c.getAnnotations(), other.getAnnotations());
biScan(c.getParameters(), other.getParameters());
biScan(c.getThrownTypes(), other.getThrownTypes());
biScan(c.getFormalTypeParameters(), other.getFormalTypeParameters());
biScan(c.getFormalCtTypeParameters(), other.getFormalCtTypeParameters());
biScan(c.getBody(), other.getBody());
biScan(c.getComments(), other.getComments());
exit(c);
Expand Down Expand Up @@ -342,7 +352,7 @@ public <T> void visitCtInterface(final spoon.reflect.declaration.CtInterface<T>
enter(intrface);
biScan(intrface.getAnnotations(), other.getAnnotations());
biScan(intrface.getSuperInterfaces(), other.getSuperInterfaces());
biScan(intrface.getFormalTypeParameters(), other.getFormalTypeParameters());
biScan(intrface.getFormalCtTypeParameters(), other.getFormalCtTypeParameters());
biScan(intrface.getNestedTypes(), other.getNestedTypes());
biScan(intrface.getFields(), other.getFields());
biScan(intrface.getMethods(), other.getMethods());
Expand Down Expand Up @@ -411,7 +421,7 @@ public <T> void visitCtMethod(final spoon.reflect.declaration.CtMethod<T> m) {
spoon.reflect.declaration.CtMethod other = ((spoon.reflect.declaration.CtMethod) (stack.peek()));
enter(m);
biScan(m.getAnnotations(), other.getAnnotations());
biScan(m.getFormalTypeParameters(), other.getFormalTypeParameters());
biScan(m.getFormalCtTypeParameters(), other.getFormalCtTypeParameters());
biScan(m.getType(), other.getType());
biScan(m.getParameters(), other.getParameters());
biScan(m.getThrownTypes(), other.getThrownTypes());
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/spoon/reflect/visitor/CtInheritanceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeInformation;
import spoon.reflect.declaration.CtTypeMember;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.internal.CtCircularTypeReference;
Expand Down Expand Up @@ -459,6 +460,18 @@ public <T> void visitCtClass(CtClass<T> e) {
scanCtVisitable(e);
}

@Override
public void visitCtTypeParameter(CtTypeParameter typeParameter) {
scanCtType(typeParameter);
scanCtTypeInformation(typeParameter);
scanCtFormalTypeDeclarer(typeParameter);
scanCtNamedElement(typeParameter);
scanCtTypeMember(typeParameter);
scanCtElement(typeParameter);
scanCtModifiable(typeParameter);
scanCtVisitable(typeParameter);
}

public <T> void visitCtConditional(CtConditional<T> e) {
scanCtExpression(e);
scanCtCodeElement(e);
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/spoon/reflect/visitor/CtScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.internal.CtCircularTypeReference;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtCatchVariableReference;
Expand Down Expand Up @@ -301,7 +302,7 @@ public <T> void visitCtClass(final CtClass<T> ctClass) {
scan(ctClass.getAnnotations());
scan(ctClass.getSuperclass());
scan(ctClass.getSuperInterfaces());
scan(ctClass.getFormalTypeParameters());
scan(ctClass.getFormalCtTypeParameters());
scan(ctClass.getAnonymousExecutables());
scan(ctClass.getNestedTypes());
scan(ctClass.getFields());
Expand All @@ -311,6 +312,15 @@ public <T> void visitCtClass(final CtClass<T> ctClass) {
exit(ctClass);
}

@Override
public void visitCtTypeParameter(CtTypeParameter typeParameter) {
enter(typeParameter);
scan(typeParameter.getAnnotations());
scan(typeParameter.getSuperclass());
scan(typeParameter.getComments());
exit(typeParameter);
}

public <T> void visitCtConditional(final CtConditional<T> conditional) {
enter(conditional);
scan(conditional.getAnnotations());
Expand All @@ -327,7 +337,7 @@ public <T> void visitCtConstructor(final CtConstructor<T> c) {
scan(c.getAnnotations());
scan(c.getParameters());
scan(c.getThrownTypes());
scan(c.getFormalTypeParameters());
scan(c.getFormalCtTypeParameters());
scan(c.getBody());
scan(c.getComments());
exit(c);
Expand Down Expand Up @@ -458,7 +468,7 @@ public <T> void visitCtInterface(final CtInterface<T> intrface) {
enter(intrface);
scan(intrface.getAnnotations());
scan(intrface.getSuperInterfaces());
scan(intrface.getFormalTypeParameters());
scan(intrface.getFormalCtTypeParameters());
scan(intrface.getNestedTypes());
scan(intrface.getFields());
scan(intrface.getMethods());
Expand Down Expand Up @@ -522,7 +532,7 @@ public <T> void visitCtCatchVariableReference(final CtCatchVariableReference<T>
public <T> void visitCtMethod(final CtMethod<T> m) {
enter(m);
scan(m.getAnnotations());
scan(m.getFormalTypeParameters());
scan(m.getFormalCtTypeParameters());
scan(m.getType());
scan(m.getParameters());
scan(m.getThrownTypes());
Expand Down
Loading

0 comments on commit 25f9202

Please sign in to comment.