Search in sources :

Example 46 with CtMethod

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

the class EqualsChecker method visitCtMethod.

@Override
public <T> void visitCtMethod(CtMethod<T> e) {
    final CtMethod peek = (CtMethod) this.other;
    if (e.isDefaultMethod() != peek.isDefaultMethod()) {
        isNotEqual = true;
        return;
    }
    super.visitCtMethod(e);
}
Also used : CtMethod(spoon.reflect.declaration.CtMethod)

Example 47 with CtMethod

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

the class JavaReflectionTreeBuilder method visitAnnotation.

@Override
public void visitAnnotation(final Annotation annotation) {
    final CtAnnotation<Annotation> ctAnnotation = factory.Core().createAnnotation();
    enter(new AnnotationRuntimeBuilderContext(ctAnnotation) {

        @Override
        public void addMethod(CtMethod ctMethod) {
            try {
                Object value = annotation.annotationType().getMethod(ctMethod.getSimpleName()).invoke(annotation);
                ctAnnotation.addValue(ctMethod.getSimpleName(), value);
            } catch (Exception ignore) {
                ctAnnotation.addValue(ctMethod.getSimpleName(), "");
            }
        }
    });
    super.visitAnnotation(annotation);
    exit();
    contexts.peek().addAnnotation(ctAnnotation);
}
Also used : AnnotationRuntimeBuilderContext(spoon.support.visitor.java.internal.AnnotationRuntimeBuilderContext) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Annotation(java.lang.annotation.Annotation) CtMethod(spoon.reflect.declaration.CtMethod)

Example 48 with CtMethod

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

the class JavaReflectionTreeBuilder method visitInterface.

@Override
public <T> void visitInterface(Class<T> clazz) {
    final CtInterface<Object> ctInterface = factory.Core().createInterface();
    ctInterface.setSimpleName(clazz.getSimpleName());
    setModifier(ctInterface, clazz.getModifiers());
    enter(new TypeRuntimeBuilderContext(ctInterface) {

        @Override
        public void addMethod(CtMethod ctMethod) {
            super.addMethod(ctMethod);
            ctMethod.setBody(null);
        }
    });
    super.visitInterface(clazz);
    exit();
    contexts.peek().addType(ctInterface);
}
Also used : TypeRuntimeBuilderContext(spoon.support.visitor.java.internal.TypeRuntimeBuilderContext) CtMethod(spoon.reflect.declaration.CtMethod)

Example 49 with CtMethod

use of spoon.reflect.declaration.CtMethod 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 50 with CtMethod

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

the class AnnotationTest method annotationAddValue.

@Test
public void annotationAddValue() {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Bar.java");
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<CtMethod>(CtMethod.class, "bidule"));
    assertThat(methods.size(), is(1));
    CtAnnotation anno1 = factory.Annotation().annotate(methods.get(0), TypeAnnotation.class).addValue("params", new String[] { "test" });
    assertThat(anno1.getValue("params").getType(), is(factory.Type().createReference(String[].class)));
    CtAnnotation anno = factory.Annotation().annotate(methods.get(0), TypeAnnotation.class).addValue("params", new String[0]);
    assertThat(anno.getValue("params").getType(), is(factory.Type().createReference(String[].class)));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16