Search in sources :

Example 6 with CtConstructor

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

the class CtClassImpl method getConstructor.

@Override
public CtConstructor<T> getConstructor(CtTypeReference<?>... parameterTypes) {
    for (CtTypeMember typeMember : getTypeMembers()) {
        if (!(typeMember instanceof CtConstructor)) {
            continue;
        }
        CtConstructor<T> c = (CtConstructor<T>) typeMember;
        boolean cont = c.getParameters().size() == parameterTypes.length;
        for (int i = 0; cont && (i < c.getParameters().size()) && (i < parameterTypes.length); i++) {
            if (!parameterTypes[i].getQualifiedName().equals(c.getParameters().get(i).getType().getQualifiedName())) {
                cont = false;
            }
        }
        if (cont) {
            return c;
        }
    }
    return null;
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 7 with CtConstructor

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

the class AnnotationTest method testRepeatSameAnnotationOnConstructor.

@Test
public void testRepeatSameAnnotationOnConstructor() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsRepeated.class);
    final CtConstructor<?> ctConstructor = ctClass.getConstructors().toArray(new CtConstructor<?>[0])[0];
    final List<CtAnnotation<? extends Annotation>> annotations = ctConstructor.getAnnotations();
    assertEquals("Constructor must to have multi annotation of the same type", 2, annotations.size());
    assertEquals("Type of the first annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(0).getAnnotationType().getActualClass());
    assertEquals("Type of the second annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(1).getAnnotationType().getActualClass());
    assertEquals("Argument of the first annotation is \"Constructor 1\"", "Constructor 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Constructor 2\"", "Constructor 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AnnotationsRepeated(spoon.test.annotation.testclasses.AnnotationsRepeated) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 8 with CtConstructor

use of spoon.reflect.declaration.CtConstructor 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 CtConstructor

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

the class IntercessionTest method testEqualConstructor.

@Test
public void testEqualConstructor() {
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X { public X() {} };").compile();
    CtConstructor<?> foo = (CtConstructor<?>) clazz.getConstructors().toArray()[0];
    CtConstructor<?> fooClone = foo.clone();
    Assert.assertEquals(foo, fooClone);
    CtBlock<?> body = foo.getBody();
    // there is an implicit call to super()
    assertEquals(1, body.getStatements().size());
    assertEquals("super()", body.getStatements().get(0).toString());
    // adding a new statement;
    CtStatement stmt = factory.Core().createCodeSnippetStatement();
    body.insertEnd(stmt);
    assertEquals(2, body.getStatements().size());
    // constructor are not equals anymore
    Assert.assertNotEquals(foo, fooClone);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 10 with CtConstructor

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

the class ElementPrinterHelper method writeElementList.

public void writeElementList(List<CtTypeMember> elements) {
    for (CtTypeMember element : elements) {
        if (element instanceof CtConstructor && element.isImplicit()) {
            continue;
        }
        printer.writeln();
        prettyPrinter.scan(element);
        if (!env.isPreserveLineNumbers()) {
            printer.writeln();
        }
    }
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtConstructor(spoon.reflect.declaration.CtConstructor)

Aggregations

CtConstructor (spoon.reflect.declaration.CtConstructor)21 CtMethod (spoon.reflect.declaration.CtMethod)10 CtClass (spoon.reflect.declaration.CtClass)9 Test (org.junit.Test)7 Factory (spoon.reflect.factory.Factory)6 CtAnonymousExecutable (spoon.reflect.declaration.CtAnonymousExecutable)5 CtParameter (spoon.reflect.declaration.CtParameter)5 CtTypeMember (spoon.reflect.declaration.CtTypeMember)5 ArrayList (java.util.ArrayList)4 CtType (spoon.reflect.declaration.CtType)4 Annotation (java.lang.annotation.Annotation)3 CtBlock (spoon.reflect.code.CtBlock)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtIf (spoon.reflect.code.CtIf)3 CtSwitch (spoon.reflect.code.CtSwitch)3 CtAnnotation (spoon.reflect.declaration.CtAnnotation)3 CtElement (spoon.reflect.declaration.CtElement)3 CtExecutable (spoon.reflect.declaration.CtExecutable)3 CtField (spoon.reflect.declaration.CtField)3 Executable (java.lang.reflect.Executable)2