Skip to content

WIP: Refactor return type of metamodel methods #1852

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

Closed
8 changes: 8 additions & 0 deletions src/main/java/spoon/metamodel/MetamodelProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -263,6 +264,13 @@ public List<MMMethod> getMethods(MMMethodKind kind) {
return ms == null ? Collections.emptyList() : Collections.unmodifiableList(ms);
}

public Set<MMMethod> getMethods() {
Set<MMMethod> res = new HashSet();
for (List<MMMethod> methods : methodsByKind.values()) {
res.addAll(methods);
}
return Collections.unmodifiableSet(res);
}

void sortByBestMatch() {
//resolve conflicts using value type. Move the most matching method to 0 index
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/pattern/internal/ValueConvertorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ public <T> T getValueAs(Factory factory, String parameterName, Object value, Cla
if (value.getClass().isArray()) {
Class<?> itemClass = value.getClass().getComponentType();
if (CtExpression.class.isAssignableFrom(itemClass)) {
CtNewArray<Object> arr = factory.Core().createNewArray().setType(factory.Type().objectType());
CtNewArray<Object> arr = (CtNewArray<Object>) factory.Core().createNewArray().setType(factory.Type().objectType());
for (CtExpression expr : (CtExpression[]) value) {
arr.addElement(expr);
}
return (T) arr;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
CtNewArray<?> arr = factory.Core().createNewArray().setType(factory.Type().createArrayReference(itemClass.getName()));
CtNewArray<?> arr = (CtNewArray<?>) factory.Core().createNewArray().setType(factory.Type().createArrayReference(itemClass.getName()));
for (Object v : (Object[]) value) {
if (v == null || v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Character) {
//convert String to code element as Literal
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/code/CtCatchVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ public interface CtCatchVariable<T> extends CtVariable<T>, CtMultiTypedElement,

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<T> type);
CtCatchVariable setType(CtTypeReference<T> type);
}
6 changes: 3 additions & 3 deletions src/main/java/spoon/reflect/code/CtConditional.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ public interface CtConditional<T> extends CtExpression<T> {
* Sets the "false" expression.
*/
@PropertySetter(role = ELSE)
<C extends CtConditional<T>> C setElseExpression(CtExpression<T> elseExpression);
CtConditional<T> setElseExpression(CtExpression<T> elseExpression);

/**
* Sets the "true" expression.
*/
@PropertySetter(role = THEN)
<C extends CtConditional<T>> C setThenExpression(CtExpression<T> thenExpression);
CtConditional<T> setThenExpression(CtExpression<T> thenExpression);

/**
* Sets the condition expression.
*/
@PropertySetter(role = CONDITION)
<C extends CtConditional<T>> C setCondition(CtExpression<Boolean> condition);
CtConditional<T> setCondition(CtExpression<Boolean> condition);

@Override
CtConditional<T> clone();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/code/CtConstructorCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface CtConstructorCall<T> extends CtTargetedExpression<T, CtExpressi
*/
@Override
@PropertySetter(role = TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);
CtConstructorCall setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);

/**
* Delegate to the executable reference of the constructor call.
Expand All @@ -68,7 +68,7 @@ public interface CtConstructorCall<T> extends CtTargetedExpression<T, CtExpressi
*/
@Override
@PropertySetter(role = TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T addActualTypeArgument(CtTypeReference<?> actualTypeArgument);
CtConstructorCall addActualTypeArgument(CtTypeReference<?> actualTypeArgument);

@Override
CtConstructorCall<T> clone();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/code/CtExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public interface CtExpression<T> extends CtCodeElement, CtTypedElement<T>, Templ
* Sets the type casts.
*/
@PropertySetter(role = CAST)
<C extends CtExpression<T>> C setTypeCasts(List<CtTypeReference<?>> types);
CtExpression<T> setTypeCasts(List<CtTypeReference<?>> types);

/**
* Adds a type cast.
*/
@PropertySetter(role = CAST)
<C extends CtExpression<T>> C addTypeCast(CtTypeReference<?> type);
CtExpression<T> addTypeCast(CtTypeReference<?> type);

@Override
CtExpression<T> clone();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/spoon/reflect/code/CtIf.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ public interface CtIf extends CtStatement, TemplateParameter<Void> {
* condition.
*/
@PropertySetter(role = CONDITION)
<T extends CtIf> T setCondition(CtExpression<Boolean> expression);
CtIf setCondition(CtExpression<Boolean> expression);

/**
* Sets the statement executed when the condition is false.
*/
@PropertySetter(role = ELSE)
<T extends CtIf> T setElseStatement(CtStatement elseStatement);
CtIf setElseStatement(CtStatement elseStatement);

/**
* Sets the statement executed when the condition is true.
*/
@PropertySetter(role = THEN)
<T extends CtIf> T setThenStatement(CtStatement thenStatement);
CtIf setThenStatement(CtStatement thenStatement);

@Override
CtIf clone();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/code/CtInvocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface CtInvocation<T> extends CtAbstractInvocation<T>, CtStatement, C
*/
@Override
@PropertySetter(role = TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);
CtInvocation setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);

/**
* Delegate to the executable reference of the invocation.
Expand All @@ -68,7 +68,7 @@ public interface CtInvocation<T> extends CtAbstractInvocation<T>, CtStatement, C
*/
@Override
@PropertySetter(role = TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T addActualTypeArgument(CtTypeReference<?> actualTypeArgument);
CtInvocation addActualTypeArgument(CtTypeReference<?> actualTypeArgument);

/**
* Return the type returned by the invocation. If the invocation is to a
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/spoon/reflect/code/CtNewClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.reference.CtActualTypeContainer;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;
Expand Down Expand Up @@ -56,24 +55,6 @@ public interface CtNewClass<T> extends CtConstructorCall<T> {
@PropertyGetter(role = TYPE_ARGUMENT)
List<CtTypeReference<?>> getActualTypeArguments();

/**
* Delegate to the executable reference of the new class.
*
* @see CtExecutableReference#getActualTypeArguments()
*/
@Override
@PropertySetter(role = TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);

/**
* Delegate to the executable reference of the new class.
*
* @see CtExecutableReference#getActualTypeArguments()
*/
@Override
@PropertySetter(role = TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T addActualTypeArgument(CtTypeReference<?> actualTypeArgument);

/**
* Gets the created class.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/code/CtTypeAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public interface CtTypeAccess<A> extends CtExpression<Void> {

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<Void> type);
CtTypeAccess setType(CtTypeReference<Void> type);

@Override
CtTypeAccess<A> clone();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/declaration/CtAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public interface CtAnnotation<A extends Annotation> extends CtExpression<A>, CtS

@Override
@UnsettableProperty
<C extends CtExpression<A>> C setTypeCasts(List<CtTypeReference<?>> types);
CtExpression<A> setTypeCasts(List<CtTypeReference<?>> types);

static CtAnnotatedElementType getAnnotatedElementTypeForCtElement(CtElement element) {
if (element == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface CtAnonymousExecutable extends CtExecutable<Void>, CtTypeMember

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<Void> type);
CtAnonymousExecutable setType(CtTypeReference<Void> type);

@Override
@UnsettableProperty
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/declaration/CtConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface CtConstructor<T> extends CtExecutable<T>, CtTypeMember, CtForma

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<T> type);
CtConstructor setType(CtTypeReference<T> type);

@Override
@UnsettableProperty
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/spoon/reflect/declaration/CtElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ <A extends Annotation> CtAnnotation<A> getAnnotation(
* @return <tt>true</tt> if this element changed as a result of the call
*/
@PropertySetter(role = ANNOTATION)
<E extends CtElement> E addAnnotation(CtAnnotation<? extends Annotation> annotation);
CtElement addAnnotation(CtAnnotation<? extends Annotation> annotation);

/**
* Remove an annotation for this element
Expand Down Expand Up @@ -185,7 +185,7 @@ <A extends Annotation> CtAnnotation<A> getAnnotation(
* of this element in the input source files
*/
@PropertySetter(role = POSITION)
<E extends CtElement> E setPosition(SourcePosition position);
CtElement setPosition(SourcePosition position);

/**
* Gets the child elements annotated with the given annotation type's
Expand Down Expand Up @@ -213,7 +213,7 @@ <E extends CtElement> List<E> getAnnotatedChildren(
* Sets this element to be implicit.
*/
@PropertySetter(role = IS_IMPLICIT)
<E extends CtElement> E setImplicit(boolean b);
CtElement setImplicit(boolean b);

/**
* Calculates and returns the set of all the types referenced by this
Expand All @@ -236,13 +236,13 @@ <E extends CtElement> List<E> getAnnotatedChildren(
* @param position
* of this element and all children in the input source file
*/
<E extends CtElement> E setPositions(SourcePosition position);
CtElement setPositions(SourcePosition position);

/**
* Sets the annotations for this element.
*/
@PropertySetter(role = ANNOTATION)
<E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotation);
CtElement setAnnotations(List<CtAnnotation<? extends Annotation>> annotation);

/**
* Gets the parent of current reference.
Expand Down Expand Up @@ -328,7 +328,7 @@ <E extends CtElement> List<E> getAnnotatedChildren(
* Set the comment list
*/
@PropertySetter(role = COMMENT)
<E extends CtElement> E setComments(List<CtComment> comments);
CtElement setComments(List<CtComment> comments);

/**
* The list of comments
Expand All @@ -343,14 +343,14 @@ <E extends CtElement> List<E> getAnnotatedChildren(
* @param comment the comment
*/
@PropertySetter(role = COMMENT)
<E extends CtElement> E addComment(CtComment comment);
CtElement addComment(CtComment comment);

/**
* Remove a comment
* @param comment the comment to remove
*/
@PropertySetter(role = COMMENT)
<E extends CtElement> E removeComment(CtComment comment);
CtElement removeComment(CtComment comment);

/**
* Clone the element which calls this method in a new object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ public interface CtTypedElement<T> extends CtElement {
* Sets this element's type.
*/
@PropertySetter(role = TYPE)
<C extends CtTypedElement> C setType(CtTypeReference<T> type);
CtTypedElement setType(CtTypeReference<T> type);
}
16 changes: 11 additions & 5 deletions src/main/java/spoon/reflect/factory/CodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public <T> CtTypeAccess<T> createTypeAccess(CtTypeReference<T> accessedType) {
* @return a accessed type expression.
*/
public <T> CtTypeAccess<T> createTypeAccess(CtTypeReference<T> accessedType, boolean isImplicit) {
return createTypeAccess(accessedType).setImplicit(isImplicit);
return (CtTypeAccess<T>) createTypeAccess(accessedType).setImplicit(isImplicit);
}

/**
Expand Down Expand Up @@ -289,7 +289,8 @@ public <T> CtNewArray<T[]> createLiteralArray(T[] value) {
}
final CtTypeReference<T> componentTypeRef = factory.Type().createReference((Class<T>) value.getClass().getComponentType());
final CtArrayTypeReference<T[]> arrayReference = factory.Type().createArrayReference(componentTypeRef);
CtNewArray<T[]> array = factory.Core().<T[]>createNewArray().setType(arrayReference);
CtNewArray<T[]> array = factory.Core().<T[]>createNewArray();
array.setType(arrayReference);
for (T e : value) {
CtLiteral<T> l = factory.Core().createLiteral();
l.setValue(e);
Expand All @@ -312,7 +313,10 @@ public <T> CtNewArray<T[]> createLiteralArray(T[] value) {
* @return a new local variable declaration
*/
public <T> CtLocalVariable<T> createLocalVariable(CtTypeReference<T> type, String name, CtExpression<T> defaultExpression) {
return factory.Core().<T>createLocalVariable().<CtLocalVariable<T>>setSimpleName(name).<CtLocalVariable<T>>setType(type).setDefaultExpression(defaultExpression);
CtLocalVariable<T> localVariable = factory.Core().<T>createLocalVariable();
localVariable.<CtLocalVariable<T>>setSimpleName(name).<CtLocalVariable<T>>setType(type);
localVariable.setDefaultExpression(defaultExpression);
return localVariable;
}

/**
Expand Down Expand Up @@ -491,8 +495,10 @@ public <T> CtStatementList createVariableAssignments(List<? extends CtVariable<T
* @return a field
*/
public <T> CtField<T> createCtField(String name, CtTypeReference<T> type, String exp, ModifierKind... visibilities) {
return factory.Core().createField().<CtField<T>>setModifiers(modifiers(visibilities)).<CtField<T>>setSimpleName(name).<CtField<T>>setType(type)
.setDefaultExpression(this.<T>createCodeSnippetExpression(exp));
CtField<T> field = factory.Core().createField();
field.<CtField<T>>setModifiers(modifiers(visibilities)).<CtField<T>>setSimpleName(name).<CtField<T>>setType(type);
field.setDefaultExpression(createCodeSnippetExpression(exp));
return field;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public interface CtActualTypeContainer {
* Sets the type arguments.
*/
@PropertySetter(role = CtRole.TYPE_ARGUMENT)
<T extends CtActualTypeContainer> T setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);
CtActualTypeContainer setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);

/**
* Adds a type argument.
*/
<T extends CtActualTypeContainer> T addActualTypeArgument(CtTypeReference<?> actualTypeArgument);
CtActualTypeContainer addActualTypeArgument(CtTypeReference<?> actualTypeArgument);

/**
* Removes a type argument.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/reference/CtReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ public interface CtReference extends CtElement {
/** comments are not possible for references */
@Override
@UnsettableProperty
<E extends CtElement> E setComments(List<CtComment> comments);
CtReference setComments(List<CtComment> comments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface CtTypeParameterReference extends CtTypeReference<Object> {

@Override
@UnsettableProperty
<T extends CtActualTypeContainer> T setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);
CtTypeParameterReference setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments);

/**
* Returns true if this has the default bounding type that is java.lang.Object (which basically means that there is no bound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface CtUnboundVariableReference<T> extends CtVariableReference<T> {

@Override
@UnsettableProperty
<E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotation);
CtUnboundVariableReference<T> setAnnotations(List<CtAnnotation<? extends Annotation>> annotation);
}
Loading