Search in sources :

Example 6 with CtParameter

use of spoon.reflect.declaration.CtParameter in project spoon by INRIA.

the class AnnotationTest method testGetAnnotationFromParameter.

@Test
public void testGetAnnotationFromParameter() {
    // contract: Java 8 receiver parameters are handled
    Launcher spoon = new Launcher();
    spoon.addInputResource("src/test/resources/noclasspath/Initializer.java");
    String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
    spoon.setSourceOutputDirectory(output);
    spoon.getEnvironment().setNoClasspath(true);
    Factory factory = spoon.getFactory();
    spoon.buildModel();
    List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "setField"));
    assertThat(methods.size(), is(1));
    CtMethod methodSet = methods.get(0);
    assertThat(methodSet.getSimpleName(), is("setField"));
    List<CtParameter> parameters = methodSet.getParameters();
    assertThat(parameters.size(), is(1));
    CtParameter thisParameter = parameters.get(0);
    assertThat(thisParameter.getSimpleName(), is("this"));
    CtTypeReference thisParamType = thisParameter.getType();
    assertThat(thisParamType.getSimpleName(), is("Initializer"));
    List<CtAnnotation<?>> annotations = thisParameter.getType().getAnnotations();
    assertThat(annotations.size(), is(2));
    CtAnnotation unknownInit = annotations.get(0);
    CtAnnotation raw = annotations.get(1);
    assertThat(unknownInit.getAnnotationType().getSimpleName(), is("UnknownInitialization"));
    assertThat(raw.getAnnotationType().getSimpleName(), is("Raw"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 7 with CtParameter

use of spoon.reflect.declaration.CtParameter in project spoon by INRIA.

the class AnnotationTest method testAnnotatedElementTypes.

@Test
public void testAnnotatedElementTypes() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    // load package of the test classes
    CtPackage pkg = factory.Package().get("spoon.test.annotation.testclasses");
    // check annotated element type of the package annotation
    List<CtAnnotation<?>> annotations = pkg.getAnnotations();
    assertEquals(2, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(pkg));
    assertEquals(CtAnnotatedElementType.PACKAGE, annotations.get(0).getAnnotatedElementType());
    // load class Main from package and check annotated element type of the class annotation
    CtClass<?> clazz = pkg.getType("Main");
    assertEquals(Main.class, clazz.getActualClass());
    annotations = clazz.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(clazz));
    assertEquals(CtAnnotatedElementType.TYPE, clazz.getAnnotations().get(0).getAnnotatedElementType());
    // load method toString() from class and check annotated element type of the annotation
    List<CtMethod<?>> methods = clazz.getMethodsByName("toString");
    assertEquals(1, methods.size());
    CtMethod<?> method = methods.get(0);
    assertEquals("toString", method.getSimpleName());
    annotations = method.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
    assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
    // load parameter of method m(int) and check annotated element type of the parameter annotation
    methods = clazz.getMethodsByName("m");
    assertEquals(1, methods.size());
    method = methods.get(0);
    assertEquals("m", method.getSimpleName());
    List<CtParameter<?>> parameters = method.getParameters();
    assertEquals(1, parameters.size());
    CtParameter<?> parameter = parameters.get(0);
    annotations = parameter.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(parameter));
    assertEquals(CtAnnotatedElementType.PARAMETER, annotations.get(0).getAnnotatedElementType());
    // load constructor of the clazz and check annotated element type of the constructor annotation
    Set<? extends CtConstructor<?>> constructors = clazz.getConstructors();
    assertEquals(1, constructors.size());
    CtConstructor<?> constructor = constructors.iterator().next();
    annotations = constructor.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(constructor));
    assertEquals(CtAnnotatedElementType.CONSTRUCTOR, annotations.get(0).getAnnotatedElementType());
    // load value ia of the m1() method annotation, which is also an annotation
    // and check the annotated element type of the inner annotation.
    methods = clazz.getMethodsByName("m1");
    assertEquals(1, methods.size());
    method = methods.get(0);
    annotations = method.getAnnotations();
    assertEquals(1, annotations.size());
    CtAnnotation<?> annotation = annotations.get(0);
    assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
    assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
    Object element = annotation.getValues().get("ia");
    assertNotNull(element);
    assertTrue(element instanceof CtAnnotation);
    assertTrue(((CtAnnotation<?>) element).getAnnotatedElement().equals(annotation));
    assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, ((CtAnnotation<?>) element).getAnnotatedElementType());
    // load enum AnnotParamTypeEnum and check the annotated element type of the annotation of the enum and of the fields
    CtEnum<?> enumeration = pkg.getType("AnnotParamTypeEnum");
    assertEquals(AnnotParamTypeEnum.class, enumeration.getActualClass());
    annotations = enumeration.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(enumeration));
    assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
    List<CtEnumValue<?>> fields = enumeration.getEnumValues();
    assertEquals(3, fields.size());
    annotations = fields.get(0).getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(fields.get(0)));
    assertEquals(CtAnnotatedElementType.FIELD, annotations.get(0).getAnnotatedElementType());
    // load interface type TestInterface and check the annotated element type of the annotation
    CtInterface<?> ctInterface = pkg.getType("TestInterface");
    assertEquals(TestInterface.class, ctInterface.getActualClass());
    annotations = ctInterface.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(ctInterface));
    assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
    // load annotation type Bound and check the annotated element type of the annotations
    CtAnnotationType<?> annotationType = pkg.getType("Bound");
    assertEquals(Bound.class, annotationType.getActualClass());
    assertNull(annotationType.getSuperclass());
    assertEquals(1, annotationType.getMethods().size());
    assertEquals(0, annotationType.getSuperInterfaces().size());
    annotations = annotationType.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(annotationType));
    assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, annotations.get(0).getAnnotatedElementType());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtEnumValue(spoon.reflect.declaration.CtEnumValue) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 8 with CtParameter

use of spoon.reflect.declaration.CtParameter in project spoon by INRIA.

the class ReplaceScanner method updateConstructor.

private CtParameter<?> updateConstructor(CtClass listener, CtTypeReference type) {
    final CtConstructor ctConstructor = (CtConstructor) listener.getConstructors().toArray(new CtConstructor[listener.getConstructors().size()])[0];
    CtAssignment assign = (CtAssignment) ctConstructor.getBody().getStatement(1);
    CtThisAccess fieldAccess = (CtThisAccess) ((CtFieldAccess) assign.getAssigned()).getTarget();
    ((CtTypeAccess) fieldAccess.getTarget()).getAccessedType().setImplicit(true);
    final CtParameter<?> aParameter = (CtParameter<?>) ctConstructor.getParameters().get(0);
    aParameter.setType(type);
    return aParameter;
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtThisAccess(spoon.reflect.code.CtThisAccess) CtParameter(spoon.reflect.declaration.CtParameter) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 9 with CtParameter

use of spoon.reflect.declaration.CtParameter in project spoon by INRIA.

the class LambdaTest method testEqualsLambdaParameterRef.

@Test
public void testEqualsLambdaParameterRef() throws Exception {
    CtLambda<?> lambda = getLambdaInFooByNumber(8);
    CtParameter<?> param = (CtParameter<?>) lambda.getParameters().get(0);
    CtParameterReference paramRef1 = param.getReference();
    CtParameterReference paramRef2 = lambda.filterChildren(new TypeFilter<>(CtParameterReference.class)).first();
    assertTrue(paramRef1.equals(paramRef2));
}
Also used : CtParameterReference(spoon.reflect.reference.CtParameterReference) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 10 with CtParameter

use of spoon.reflect.declaration.CtParameter in project spoon by INRIA.

the class ParameterTest method testGetParameterReferenceInLambdaNoClasspath.

@Test
public void testGetParameterReferenceInLambdaNoClasspath() throws Exception {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/Tacos.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    CtMethod<?> ctMethod = launcher.getFactory().Type().get("Tacos").getMethodsByName("setStarRatings").get(0);
    CtParameter ctParameter = ctMethod.getBody().getStatement(0).getElements(new TypeFilter<CtParameter>(CtParameter.class) {

        @Override
        public boolean matches(CtParameter element) {
            return "entryPair".equals(element.getSimpleName()) && super.matches(element);
        }
    }).get(0);
    assertNotNull(ctParameter.getReference());
    List<CtParameterReference> elements = ctMethod.getBody().getStatement(0).getElements(new TypeFilter<CtParameterReference>(CtParameterReference.class) {

        @Override
        public boolean matches(CtParameterReference element) {
            return "entryPair".equals(element.getSimpleName()) && super.matches(element);
        }
    });
    assertEquals(2, elements.size());
    for (CtParameterReference element : elements) {
        assertEquals(ctParameter, element.getDeclaration());
        assertEquals(ctParameter.getReference(), element);
    }
}
Also used : CtParameterReference(spoon.reflect.reference.CtParameterReference) Launcher(spoon.Launcher) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

CtParameter (spoon.reflect.declaration.CtParameter)32 Test (org.junit.Test)18 CtMethod (spoon.reflect.declaration.CtMethod)13 Factory (spoon.reflect.factory.Factory)12 CtTypeReference (spoon.reflect.reference.CtTypeReference)10 ArrayList (java.util.ArrayList)8 Launcher (spoon.Launcher)8 CtElement (spoon.reflect.declaration.CtElement)7 List (java.util.List)6 CtIf (spoon.reflect.code.CtIf)6 CtLocalVariable (spoon.reflect.code.CtLocalVariable)6 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)6 CtConstructor (spoon.reflect.declaration.CtConstructor)5 CtField (spoon.reflect.declaration.CtField)5 CtInvocation (spoon.reflect.code.CtInvocation)4 CtClass (spoon.reflect.declaration.CtClass)4 CtType (spoon.reflect.declaration.CtType)4 SpoonException (spoon.SpoonException)3 CtComment (spoon.reflect.code.CtComment)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3