use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class AnnotationValuesTest method testValuesOnJava8Annotation.
@Test
public void testValuesOnJava8Annotation() throws Exception {
CtType<AnnotationValues> aClass = buildClass(AnnotationValues.class);
CtConstructorCall aConstructorCall = aClass.getMethod("method").getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
CtAnnotation<?> ctAnnotation = on(aConstructorCall.getType()).giveMeAnnotation(AnnotationValues.Annotation.class);
on(ctAnnotation).giveMeAnnotationValue("integer").isTypedBy(CtLiteral.class);
on(ctAnnotation).giveMeAnnotationValue("integers").isTypedBy(CtNewArray.class);
on(ctAnnotation).giveMeAnnotationValue("string").isTypedBy(CtLiteral.class);
on(ctAnnotation).giveMeAnnotationValue("strings").isTypedBy(CtLiteral.class);
on(ctAnnotation).giveMeAnnotationValue("clazz").isTypedBy(CtFieldAccess.class);
on(ctAnnotation).giveMeAnnotationValue("classes").isTypedBy(CtNewArray.class);
on(ctAnnotation).giveMeAnnotationValue("b").isTypedBy(CtLiteral.class);
on(ctAnnotation).giveMeAnnotationValue("e").isTypedBy(CtFieldAccess.class);
on(ctAnnotation).giveMeAnnotationValue("ia").isTypedBy(CtAnnotation.class);
on(ctAnnotation).giveMeAnnotationValue("ias").isTypedBy(CtNewArray.class);
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testSrcMainJava.
// this test contains all the architectural rules that are valid for the whole src/main/java
// we put them in the same test in order to only build the full model once
@Test
public void testSrcMainJava() throws Exception {
Launcher spoon = new Launcher();
spoon.getEnvironment().setCommentEnabled(true);
spoon.addInputResource("src/main/java/");
// contract: all non-trivial public methods should be documented with proper API Javadoc
spoon.buildModel();
List<String> notDocumented = new ArrayList<>();
for (CtMethod method : spoon.getModel().getElements(new TypeFilter<>(CtMethod.class))) {
// now we see whether this should be documented
if (// public methods should be documented
method.hasModifier(ModifierKind.PUBLIC) && // all kinds of setters can be undocumented
!method.getSimpleName().startsWith("get") && !method.getSimpleName().startsWith("set") && !method.getSimpleName().startsWith("is") && !method.getSimpleName().startsWith("add") && !method.getSimpleName().startsWith("remove") && // only the top declarations should be documented (not the overriding methods which are lower in the hierarchy)
method.getTopDefinitions().size() == 0 && (// all interface methods and abstract class methods must be documented
method.hasModifier(ModifierKind.ABSTRACT) || // 4) you commit your changes and create the corresponding pull requests
method.filterChildren(new TypeFilter<>(CtCodeElement.class)).list().size() > // means that only large methods must be documented
35)) {
// is it really well documented?
if (method.getDocComment().length() <= 15) {
// the Javadoc must be at least at least 15 characters (still pretty short...)
notDocumented.add(method.getParent(CtType.class).getQualifiedName() + "#" + method.getSignature());
}
}
}
if (notDocumented.size() > 0) {
fail(notDocumented.size() + " public methods should be documented with proper API documentation: \n" + StringUtils.join(notDocumented, "\n"));
}
// contract: Spoon's code never uses TreeSet constructor, because they implicitly depend on Comparable (no static check, only dynamic checks)
List<CtConstructorCall> treeSetWithoutComparators = spoon.getFactory().Package().getRootPackage().filterChildren(new AbstractFilter<CtConstructorCall>() {
@Override
public boolean matches(CtConstructorCall element) {
return element.getType().getActualClass().equals(TreeSet.class) && element.getArguments().size() == 0;
}
}).list();
assertEquals(0, treeSetWithoutComparators.size());
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class InitializerTest method testModelBuildingInitializer.
@Test
public void testModelBuildingInitializer() throws Exception {
CtClass<?> type = build("spoon.test.initializers", "InstanceInitializers");
assertEquals("InstanceInitializers", type.getSimpleName());
CtField<?> k = type.getElements(new NamedElementFilter<>(CtField.class, "k")).get(0);
assertTrue(k.getDefaultExpression() instanceof CtConstructorCall);
CtField<?> l = type.getElements(new NamedElementFilter<>(CtField.class, "l")).get(0);
assertTrue(l.getDefaultExpression() instanceof CtConstructorCall);
CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
assertTrue(x.getDefaultExpression() == null);
CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
assertTrue(y.getDefaultExpression() instanceof CtLiteral);
CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
assertTrue(z.getDefaultExpression().toString().equals("5"));
// static initializer
CtAnonymousExecutable ex = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
assertEquals("x = 3", ex.getBody().getStatements().get(0).toString());
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class TypeReferenceTest method testIgnoreEnclosingClassInActualTypes.
@Test
public void testIgnoreEnclosingClassInActualTypes() throws Exception {
final CtType<Panini> aPanini = buildClass(Panini.class);
final CtStatement ctReturn = aPanini.getMethod("entryIterator").getBody().getStatement(0);
assertTrue(ctReturn instanceof CtReturn);
final CtExpression ctConstructorCall = ((CtReturn) ctReturn).getReturnedExpression();
assertTrue(ctConstructorCall instanceof CtConstructorCall);
assertEquals("spoon.test.reference.testclasses.Panini<K, V>.Itr<java.util.Map.Entry<K, V>>", ctConstructorCall.getType().toString());
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class TypeTest method testPolyTypBindingInTernaryExpression.
@Test
public void testPolyTypBindingInTernaryExpression() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/ternary-bug");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
CtType<Object> aType = launcher.getFactory().Type().get("de.uni_bremen.st.quide.persistence.transformators.IssueTransformator");
CtConstructorCall ctConstructorCall = aType.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class) {
@Override
public boolean matches(CtConstructorCall element) {
return "TOIssue".equals(element.getExecutable().getType().getSimpleName()) && super.matches(element);
}
}).get(0);
assertEquals(launcher.getFactory().Type().objectType(), ctConstructorCall.getExecutable().getParameters().get(9));
}
Aggregations