use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class CodeFactoryTest method testThisAccess.
@Test
public void testThisAccess() throws Exception {
final Factory factory = createFactory();
final CtTypeReference<Object> type = factory.Type().createReference("fr.inria.Test");
final CtThisAccess<Object> thisAccess = factory.Code().createThisAccess(type);
assertNotNull(thisAccess.getTarget());
assertTrue(thisAccess.getTarget() instanceof CtTypeAccess);
assertEquals(type, ((CtTypeAccess) thisAccess.getTarget()).getAccessedType());
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtInvocation.
@Override
public <T> void visitCtInvocation(CtInvocation<T> invocation) {
enterCtStatement(invocation);
enterCtExpression(invocation);
if (invocation.getExecutable().isConstructor()) {
// It's a constructor (super or this)
elementPrinterHelper.writeActualTypeArguments(invocation.getExecutable());
CtType<?> parentType;
try {
parentType = invocation.getParent(CtType.class);
} catch (ParentNotInitializedException e) {
parentType = null;
}
if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) {
printer.writeKeyword("this");
} else {
if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
scan(invocation.getTarget());
printer.writeSeparator(".");
}
printer.writeKeyword("super");
}
} else {
// It's a method invocation
boolean isImported = this.isImported(invocation.getExecutable());
if (!isImported) {
try (Writable _context = context.modify()) {
if (invocation.getTarget() instanceof CtTypeAccess) {
_context.ignoreGenerics(true);
}
if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
scan(invocation.getTarget());
printer.writeSeparator(".");
}
}
}
elementPrinterHelper.writeActualTypeArguments(invocation);
if (env.isPreserveLineNumbers()) {
getPrinterHelper().adjustStartPosition(invocation);
}
printer.writeIdentifier(invocation.getExecutable().getSimpleName());
}
try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "(", false, false, ",", true, false, ")")) {
for (CtExpression<?> e : invocation.getArguments()) {
lp.printSeparatorIfAppropriate();
scan(e);
}
}
exitCtExpression(invocation);
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method printCtFieldAccess.
private <T> void printCtFieldAccess(CtFieldAccess<T> f) {
enterCtExpression(f);
try (Writable _context = context.modify()) {
if ((f.getVariable().isStatic() || "class".equals(f.getVariable().getSimpleName())) && f.getTarget() instanceof CtTypeAccess) {
_context.ignoreGenerics(true);
}
CtExpression<?> target = f.getTarget();
if (target != null) {
boolean isInitializeStaticFinalField = isInitializeStaticFinalField(f.getTarget());
boolean isStaticField = f.getVariable().isStatic();
boolean isImportedField = this.isImported(f.getVariable());
if (!isInitializeStaticFinalField && !(isStaticField && isImportedField)) {
if (target.isImplicit() && !(f.getVariable().getFieldDeclaration() == null && this.env.getNoClasspath())) {
/*
* target is implicit, check whether there is no conflict with an local variable, catch variable or parameter
* in case of conflict make it explicit, otherwise the field access is shadowed by that variable.
* Search for potential variable declaration until we found a class which declares or inherits this field
*/
final CtField<?> field = f.getVariable().getFieldDeclaration();
if (field != null) {
final String fieldName = field.getSimpleName();
CtVariable<?> var = f.getVariable().map(new PotentialVariableDeclarationFunction(fieldName)).first();
if (var != field) {
// another variable declaration was found which is hiding the field declaration for this field access. Make the field access expicit
target.setImplicit(false);
}
} else {
// There is a model inconsistency
printer.writeComment(f.getFactory().createComment("ERROR: Missing field \"" + f.getVariable().getSimpleName() + "\", please check your model. The code may not compile.", CommentType.BLOCK)).writeSpace();
}
}
// the implicit drives the separator
if (!target.isImplicit()) {
scan(target);
printer.writeSeparator(".");
}
}
_context.ignoreStaticAccess(true);
}
scan(f.getVariable());
}
exitCtExpression(f);
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class CtAnnotationImpl method convertValueToExpression.
private CtExpression convertValueToExpression(Object value) {
CtExpression res;
if (value.getClass().isArray()) {
// Value should be converted to a CtNewArray.
res = getFactory().Core().createNewArray();
Object[] values = (Object[]) value;
res.setType(getFactory().Type().createArrayReference(getFactory().Type().createReference(value.getClass().getComponentType())));
for (Object o : values) {
((CtNewArray) res).addElement(convertValueToExpression(o));
}
} else if (value instanceof Collection) {
// Value should be converted to a CtNewArray.
res = getFactory().Core().createNewArray();
Collection values = (Collection) value;
res.setType(getFactory().Type().createArrayReference(getFactory().Type().createReference(values.toArray()[0].getClass())));
for (Object o : values) {
((CtNewArray) res).addElement(convertValueToExpression(o));
}
} else if (value instanceof Class) {
// Value should be a field access to a .class.
res = getFactory().Code().createClassAccess(getFactory().Type().createReference((Class) value));
} else if (value instanceof Field) {
// Value should be a field access to a field.
CtFieldReference<Object> variable = getFactory().Field().createReference((Field) value);
variable.setStatic(true);
CtTypeAccess target = getFactory().Code().createTypeAccess(getFactory().Type().createReference(((Field) value).getDeclaringClass()));
CtFieldRead fieldRead = getFactory().Core().createFieldRead();
fieldRead.setVariable(variable);
fieldRead.setTarget(target);
fieldRead.setType(target.getAccessedType());
res = fieldRead;
} else if (isPrimitive(value.getClass()) || value instanceof String) {
// Value should be a literal.
res = getFactory().Code().createLiteral(value);
} else if (value.getClass().isEnum()) {
final CtTypeReference declaringClass = getFactory().Type().createReference(((Enum) value).getDeclaringClass());
final CtFieldReference variableRef = getFactory().Field().createReference(declaringClass, declaringClass, ((Enum) value).name());
CtTypeAccess target = getFactory().Code().createTypeAccess(declaringClass);
CtFieldRead fieldRead = getFactory().Core().createFieldRead();
fieldRead.setVariable(variableRef);
fieldRead.setTarget(target);
fieldRead.setType(declaringClass);
res = fieldRead;
} else {
throw new SpoonException("Please, submit a valid value.");
}
return res;
}
use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.
the class InvocationTest method testTypeOfStaticInvocation.
@Test
public void testTypeOfStaticInvocation() throws Exception {
SpoonAPI launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/invocations/testclasses/", "-o", "./target/spooned/" });
Factory factory = launcher.getFactory();
CtClass<?> aClass = factory.Class().get(Foo.class);
final List<CtInvocation<?>> elements = aClass.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return element.getTarget() != null;
}
});
assertEquals(2, elements.size());
assertTrue(elements.get(0).getTarget() instanceof CtTypeAccess);
assertTrue(elements.get(1).getTarget() instanceof CtTypeAccess);
}
Aggregations