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;
}
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());
}
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;
}
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);
}
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();
}
}
}
Aggregations