use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class ElementPrinterHelper method writeAnnotationElement.
/**
* Writes an annotation element.
*/
public void writeAnnotationElement(Factory factory, Object value) {
if (value instanceof CtTypeAccess) {
prettyPrinter.scan((CtTypeAccess) value);
printer.writeSeparator(".").writeKeyword("class");
} else if (value instanceof CtFieldReference) {
prettyPrinter.scan(((CtFieldReference<?>) value).getDeclaringType());
printer.writeSeparator(".").writeIdentifier(((CtFieldReference<?>) value).getSimpleName());
} else if (value instanceof CtElement) {
prettyPrinter.scan((CtElement) value);
} else if (value instanceof String) {
printer.writeLiteral("\"" + LiteralHelper.getStringLiteral((String) value, true) + "\"");
} else if (value instanceof Collection) {
try (ListPrinter lp = createListPrinter(false, "{", false, true, ",", false, false, "}")) {
for (Object obj : (Collection<?>) value) {
lp.printSeparatorIfAppropriate();
writeAnnotationElement(factory, obj);
}
}
} else if (value instanceof Object[]) {
try (ListPrinter lp = createListPrinter(false, "{", false, true, ",", false, false, "}")) {
for (Object obj : (Object[]) value) {
lp.printSeparatorIfAppropriate();
writeAnnotationElement(factory, obj);
}
}
} else if (value instanceof Enum) {
try (Writable c = prettyPrinter.getContext().modify().ignoreGenerics(true)) {
prettyPrinter.scan(factory.Type().createReference(((Enum<?>) value).getDeclaringClass()));
}
printer.writeSeparator(".");
printer.writeIdentifier(value.toString());
} else {
// it probably prints, boolean, number, ...
printer.writeLiteral(value.toString());
}
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class JDTTreeBuilder method createParameterizedType.
private boolean createParameterizedType(TypeReference parameterizedTypeReference) {
if (skipTypeInAnnotation) {
return true;
}
CtTypeReference typeReference = references.buildTypeReference(parameterizedTypeReference, null);
CtTypeAccess typeAccess = factory.Code().createTypeAccessWithoutCloningReference(typeReference);
context.enter(typeAccess, parameterizedTypeReference);
return true;
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class ParentExiter method visitCtNewArray.
@Override
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) {
final ArrayAllocationExpression arrayAlloc = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node;
newArray.setType((CtArrayTypeReference) jdtTreeBuilder.getFactory().Type().createArrayReference(((CtTypeAccess) child).getAccessedType(), arrayAlloc.dimensions.length));
} else if (child instanceof CtExpression) {
if (isContainedInDimensionExpression()) {
newArray.addDimensionExpression((CtExpression<Integer>) child);
} else if (child instanceof CtNewArray && childJDT instanceof ArrayInitializer && jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ArrayAllocationExpression) {
newArray.setElements(((CtNewArray) child).getElements());
} else {
newArray.addElement((CtExpression) child);
}
}
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class TypeTest method testTypeAccessForTypeAccessInInstanceOf.
@Test
public void testTypeAccessForTypeAccessInInstanceOf() throws Exception {
// contract: the right hand operator must be a CtTypeAccess.
final String target = "./target/type";
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
launcher.setSourceOutputDirectory(target);
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
final CtMethod<?> eat = aPozole.getMethodsByName("eat").get(0);
final List<CtTypeAccess<?>> typeAccesses = eat.getElements(new TypeFilter<CtTypeAccess<?>>(CtTypeAccess.class));
assertEquals(2, typeAccesses.size());
assertTrue(typeAccesses.get(0).getParent() instanceof CtBinaryOperator);
assertEquals(BinaryOperatorKind.INSTANCEOF, ((CtBinaryOperator) typeAccesses.get(0).getParent()).getKind());
assertEquals("a instanceof java.lang.String", typeAccesses.get(0).getParent().toString());
assertTrue(typeAccesses.get(1).getParent() instanceof CtBinaryOperator);
assertEquals(BinaryOperatorKind.INSTANCEOF, ((CtBinaryOperator) typeAccesses.get(1).getParent()).getKind());
assertEquals("a instanceof java.util.Collection<?>", typeAccesses.get(1).getParent().toString());
}
Aggregations