Skip to content

Commit

Permalink
feat(role): add field annotation with a role (#1368)
Browse files Browse the repository at this point in the history
* feat(role): add role annotation on implementation fields

* remove unused field

* fix javadoc

* move MetamodelPropertyField to reflect.annotations

* add POSITION role

* add test

* fix test

* fix javadoc test

* add test

* fix test
  • Loading branch information
tdurieux authored and surli committed Jun 9, 2017
1 parent bf03cb2 commit e738a1b
Show file tree
Hide file tree
Showing 70 changed files with 402 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2006-2017 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.annotations;

import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.path.CtRole;
import spoon.support.reflect.declaration.CtClassImpl;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Tells that a field is a property of the metamodel
* For instance {@link CtClassImpl#simpleName} is the property name of {@link CtNamedElement}
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface MetamodelPropertyField {
CtRole role();
}
6 changes: 3 additions & 3 deletions src/main/java/spoon/reflect/code/CtLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;

import static spoon.reflect.path.CtRole.EXPRESSION;
import static spoon.reflect.path.CtRole.VALUE;

/**
* This code element defines a literal value (an int, a string, etc).
Expand All @@ -37,13 +37,13 @@ public interface CtLiteral<T> extends CtExpression<T> {
/**
* Gets the actual value of the literal (statically known).
*/
@PropertyGetter(role = EXPRESSION)
@PropertyGetter(role = VALUE)
T getValue();

/**
* Sets the actual value of the literal.
*/
@PropertySetter(role = EXPRESSION)
@PropertySetter(role = VALUE)
<C extends CtLiteral<T>> C setValue(T value);

/** Overriding return type, a clone of a CtLiteral returns a CtLiteral */
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/spoon/reflect/code/CtNewClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.List;

import static spoon.reflect.path.CtRole.EXPRESSION;
import static spoon.reflect.path.CtRole.TYPE_PARAMETER;

/**
Expand Down Expand Up @@ -76,11 +77,13 @@ public interface CtNewClass<T> extends CtConstructorCall<T> {
/**
* Gets the created class.
*/
@PropertyGetter(role = EXPRESSION)
CtClass<?> getAnonymousClass();

/**
* Sets the created class.
*/
@PropertySetter(role = EXPRESSION)
<N extends CtNewClass> N setAnonymousClass(CtClass<?> anonymousClass);

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/code/CtSynchronized.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface CtSynchronized extends CtStatement {
/**
* Sets the synchronized block.
*/
@PropertyGetter(role = BODY)
@PropertySetter(role = BODY)
<T extends CtSynchronized> T setBlock(CtBlock<?> block);

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/spoon/reflect/declaration/CtCodeSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.reflect.declaration;

import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;

import static spoon.reflect.path.CtRole.EXPRESSION;
Expand All @@ -35,7 +36,7 @@ public interface CtCodeSnippet {
/**
* Sets the textual value of the code.
*/
@PropertySetter(role = EXPRESSION)
@PropertyGetter(role = EXPRESSION)
<C extends CtCodeSnippet> C setValue(String value);

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static spoon.reflect.path.CtRole.ANNOTATION;
import static spoon.reflect.path.CtRole.COMMENT;
import static spoon.reflect.path.CtRole.IS_IMPLICIT;
import static spoon.reflect.path.CtRole.POSITION;

/**
* This interface is the root interface for the metamodel elements (any program
Expand Down Expand Up @@ -100,6 +101,7 @@ <A extends Annotation> CtAnnotation<A> getAnnotation(
*
* @return Source file and line number of this element or null
*/
@PropertyGetter(role = POSITION)
SourcePosition getPosition();

/**
Expand All @@ -113,6 +115,7 @@ <A extends Annotation> CtAnnotation<A> getAnnotation(
* @param annotation
* @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);

/**
Expand All @@ -121,6 +124,7 @@ <A extends Annotation> CtAnnotation<A> getAnnotation(
* @param annotation
* @return <tt>true</tt> if this element changed as a result of the call
*/
@PropertySetter(role = ANNOTATION)
boolean removeAnnotation(CtAnnotation<? extends Annotation> annotation);

/**
Expand All @@ -139,6 +143,7 @@ <A extends Annotation> CtAnnotation<A> getAnnotation(
* @param position
* of this element in the input source files
*/
@PropertySetter(role = POSITION)
<E extends CtElement> E setPosition(SourcePosition position);

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/spoon/reflect/declaration/CtPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ public interface CtPackage extends CtNamedElement, CtShadowable {
/**
* Adds a type to this package.
*/
@PropertySetter(role = TYPE)
<T extends CtPackage> T addType(CtType<?> type);

/**
* Removes a type from this package.
*/
@PropertySetter(role = TYPE)
void removeType(CtType<?> type);

/**
Expand All @@ -128,6 +130,7 @@ public interface CtPackage extends CtNamedElement, CtShadowable {
* @param pack
* @return <tt>true</tt> if this element changed as a result of the call
*/
@PropertySetter(role = SUB_PACKAGE)
boolean removePackage(CtPackage pack);

/**
Expand All @@ -136,7 +139,7 @@ public interface CtPackage extends CtNamedElement, CtShadowable {
* @param types
* new Set of types
*/
@PropertyGetter(role = TYPE)
@PropertySetter(role = TYPE)
<T extends CtPackage> T setTypes(Set<CtType<?>> types);

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/spoon/reflect/path/CtRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public enum CtRole {
TYPE_PARAMETER,
COMMENT_TAG,
COMMENT_CONTENT,
COMMENT_TYPE;
COMMENT_TYPE,
POSITION;

/**
* Get the {@link CtRole} associated to the field name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtExpression;
import spoon.reflect.path.CtRole;
import spoon.reflect.annotations.MetamodelPropertyField;

public abstract class CtArrayAccessImpl<T, V extends CtExpression<?>> extends CtTargetedExpressionImpl<T, V> implements CtArrayAccess<T, V> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.EXPRESSION)
private CtExpression<Integer> expression;

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtAssertImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

import spoon.reflect.code.CtAssert;
import spoon.reflect.code.CtExpression;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;

public class CtAssertImpl<T> extends CtStatementImpl implements CtAssert<T> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.CONDITION)
CtExpression<Boolean> asserted;

@MetamodelPropertyField(role = CtRole.EXPRESSION)
CtExpression<T> value;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import spoon.reflect.code.CtRHSReceiver;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.support.reflect.declaration.CtElementImpl;

import java.util.ArrayList;
Expand All @@ -33,12 +35,16 @@
public class CtAssignmentImpl<T, A extends T> extends CtStatementImpl implements CtAssignment<T, A> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.ASSIGNED)
CtExpression<T> assigned;

@MetamodelPropertyField(role = CtRole.ASSIGNMENT)
CtExpression<A> assignment;

@MetamodelPropertyField(role = CtRole.TYPE)
CtTypeReference<T> type;

@MetamodelPropertyField(role = CtRole.CAST)
List<CtTypeReference<?>> typeCasts = emptyList();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtExpression;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;

public class CtBinaryOperatorImpl<T> extends CtExpressionImpl<T> implements CtBinaryOperator<T> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.OPERATOR_KIND)
BinaryOperatorKind kind;

@MetamodelPropertyField(role = CtRole.LEFT_OPERAND)
CtExpression<?> leftHandOperand;

@MetamodelPropertyField(role = CtRole.RIGHT_OPERAND)
CtExpression<?> rightHandOperand;

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtBlockImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.Query;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.support.reflect.declaration.CtElementImpl;
import spoon.support.util.EmptyIterator;

Expand All @@ -39,6 +41,7 @@
public class CtBlockImpl<R> extends CtStatementImpl implements CtBlock<R> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.STATEMENT)
private List<CtStatement> statements = emptyList();

public void accept(CtVisitor visitor) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtBreakImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package spoon.support.reflect.code;

import spoon.reflect.code.CtBreak;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;

public class CtBreakImpl extends CtStatementImpl implements CtBreak {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.TARGET_LABEL)
String targetLabel;

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtCaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.support.reflect.declaration.CtElementImpl;

import java.util.ArrayList;
Expand All @@ -32,8 +34,10 @@
public class CtCaseImpl<E> extends CtStatementImpl implements CtCase<E> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.CASE)
CtExpression<E> caseExpression;

@MetamodelPropertyField(role = CtRole.STATEMENT)
List<CtStatement> statements = emptyList();

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtCatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtStatement;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;

public class CtCatchImpl extends CtCodeElementImpl implements CtCatch {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.BODY)
CtBlock<?> body;

@MetamodelPropertyField(role = CtRole.PARAMETER)
CtCatchVariable<? extends Throwable> parameter;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtCatchVariableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.DerivedProperty;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.support.UnsettableProperty;
import spoon.support.reflect.declaration.CtElementImpl;

Expand All @@ -41,12 +43,16 @@
public class CtCatchVariableImpl<T> extends CtCodeElementImpl implements CtCatchVariable<T> {
private static final long serialVersionUID = 1L;

@MetamodelPropertyField(role = CtRole.NAME)
String name = "";

@MetamodelPropertyField(role = CtRole.TYPE)
CtTypeReference<T> type;

@MetamodelPropertyField(role = CtRole.TYPE)
List<CtTypeReference<?>> types = emptyList();

@MetamodelPropertyField(role = CtRole.MODIFIER)
Set<ModifierKind> modifiers = CtElementImpl.emptySet();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import spoon.reflect.code.CtCodeSnippetExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.declaration.CtCodeSnippet;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.support.compiler.SnippetCompilationError;
import spoon.support.compiler.SnippetCompilationHelper;

Expand All @@ -31,6 +33,7 @@ public void accept(CtVisitor visitor) {
visitor.visitCtCodeSnippetExpression(this);
}

@MetamodelPropertyField(role = CtRole.EXPRESSION)
String value;

public String getValue() {
Expand Down
Loading

0 comments on commit e738a1b

Please sign in to comment.