use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtThisAccess.
@Override
public <T> void visitCtThisAccess(CtThisAccess<T> thisAccess) {
try {
enterCtExpression(thisAccess);
// we only write qualified this when this is required
// this is good both in fully-qualified mode and in readable (with-imports) mode
// the implicit information is used for analysis (eg are visibility caused by implicit bugs?) but
// not for pretty-printing
CtTypeAccess target = (CtTypeAccess) thisAccess.getTarget();
CtTypeReference targetType = target.getAccessedType();
// readable mode as close as possible to the original code
if (thisAccess.isImplicit()) {
// what the good target is in JDTTreeBuilder
return;
}
// this is shorter (no qualified this), explicit, and less fragile wrt transformation
if (targetType == null || (thisAccess.getParent(CtType.class) != null && thisAccess.getParent(CtType.class).isTopLevel())) {
printer.writeKeyword("this");
// still go through finally block below
return;
}
// this has to come after the implicit test just before
if (targetType.isAnonymous()) {
printer.writeKeyword("this");
return;
}
// complex case of qualifed this
if (!context.currentThis.isEmpty()) {
CtType lastType = context.currentThis.peekFirst().type;
String lastTypeQualifiedName = lastType.getQualifiedName();
String targetTypeQualifiedName = targetType.getQualifiedName();
if (!lastTypeQualifiedName.equals(targetTypeQualifiedName)) {
if (!targetType.isImplicit()) {
visitCtTypeReferenceWithoutGenerics(targetType);
printer.writeSeparator(".");
}
printer.writeKeyword("this");
return;
}
}
// the default super simple case only comes at the end
printer.writeKeyword("this");
} finally {
exitCtExpression(thisAccess);
}
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ElementPrinterHelper method writeImports.
/**
* writes the imports in a specific order (eg all static imports together
*/
public void writeImports(Collection<CtImport> imports) {
Set<String> setImports = new HashSet<>();
Set<String> setStaticImports = new HashSet<>();
for (CtImport ctImport : imports) {
String importTypeStr;
switch(ctImport.getImportKind()) {
case TYPE:
CtTypeReference typeRef = (CtTypeReference) ctImport.getReference();
importTypeStr = typeRef.getQualifiedName();
if (!isJavaLangClasses(importTypeStr)) {
setImports.add(importTypeStr);
}
break;
case ALL_TYPES:
CtPackageReference packageRef = (CtPackageReference) ctImport.getReference();
importTypeStr = packageRef.getQualifiedName() + ".*";
if (!isJavaLangClasses(importTypeStr)) {
setImports.add(importTypeStr);
}
break;
case METHOD:
CtExecutableReference execRef = (CtExecutableReference) ctImport.getReference();
if (execRef.getDeclaringType() != null) {
setStaticImports.add(this.removeInnerTypeSeparator(execRef.getDeclaringType().getQualifiedName()) + "." + execRef.getSimpleName());
}
break;
case FIELD:
CtFieldReference fieldRef = (CtFieldReference) ctImport.getReference();
setStaticImports.add(this.removeInnerTypeSeparator(fieldRef.getDeclaringType().getQualifiedName()) + "." + fieldRef.getSimpleName());
break;
case ALL_STATIC_MEMBERS:
CtTypeReference typeStarRef = (CtTypeReference) ctImport.getReference();
importTypeStr = typeStarRef.getQualifiedName();
if (!isJavaLangClasses(importTypeStr)) {
setStaticImports.add(importTypeStr);
}
break;
}
}
List<String> sortedImports = new ArrayList<>(setImports);
Collections.sort(sortedImports);
boolean isFirst = true;
for (String importLine : sortedImports) {
if (isFirst) {
printer.writeln();
printer.writeln();
isFirst = false;
}
printer.writeKeyword("import").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
if (setStaticImports.size() > 0) {
if (isFirst) {
printer.writeln();
}
printer.writeln();
List<String> sortedStaticImports = new ArrayList<>(setStaticImports);
Collections.sort(sortedStaticImports);
for (String importLine : sortedStaticImports) {
printer.writeKeyword("import").writeSpace().writeKeyword("static").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
}
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class AccessibleVariablesFinder method getVariable.
private List<CtVariable> getVariable(final CtElement parent) {
final List<CtVariable> variables = new ArrayList<>();
if (parent == null) {
return variables;
}
class VariableScanner extends CtInheritanceScanner {
@Override
public void visitCtStatementList(CtStatementList e) {
for (int i = 0; i < e.getStatements().size(); i++) {
CtStatement ctStatement = e.getStatements().get(i);
if (ctStatement.getPosition() == null) {
}
if (ctStatement.getPosition() != null && ctStatement.getPosition().getSourceStart() > expression.getPosition().getSourceEnd()) {
break;
}
if (ctStatement instanceof CtVariable) {
variables.add((CtVariable) ctStatement);
}
}
super.visitCtStatementList(e);
}
@Override
public <T> void scanCtType(CtType<T> type) {
List<CtField<?>> fields = type.getFields();
for (int i = 0; i < fields.size(); i++) {
CtField<?> ctField = fields.get(i);
if (ctField.hasModifier(ModifierKind.PUBLIC) || ctField.hasModifier(ModifierKind.PROTECTED)) {
variables.add(ctField);
} else if (ctField.hasModifier(ModifierKind.PRIVATE)) {
if (expression.hasParent(type)) {
variables.add(ctField);
}
} else if (expression.getParent(CtPackage.class).equals(type.getParent(CtPackage.class))) {
// default visibility
variables.add(ctField);
}
}
CtTypeReference<?> superclass = type.getSuperclass();
if (superclass != null) {
variables.addAll(getVariable(superclass.getTypeDeclaration()));
}
Set<CtTypeReference<?>> superInterfaces = type.getSuperInterfaces();
for (Iterator<CtTypeReference<?>> iterator = superInterfaces.iterator(); iterator.hasNext(); ) {
CtTypeReference<?> typeReference = iterator.next();
variables.addAll(getVariable(typeReference.getTypeDeclaration()));
}
super.scanCtType(type);
}
@Override
public void visitCtTryWithResource(CtTryWithResource e) {
variables.addAll(e.getResources());
super.visitCtTryWithResource(e);
}
@Override
public void scanCtExecutable(CtExecutable e) {
variables.addAll(e.getParameters());
super.scanCtExecutable(e);
}
@Override
public void visitCtFor(CtFor e) {
for (CtStatement ctStatement : e.getForInit()) {
this.scan(ctStatement);
}
super.visitCtFor(e);
}
@Override
public void visitCtForEach(CtForEach e) {
variables.add(e.getVariable());
super.visitCtForEach(e);
}
@Override
public void visitCtMethod(CtMethod e) {
this.scan(e.getBody());
super.visitCtMethod(e);
}
@Override
public void visitCtLocalVariable(CtLocalVariable e) {
variables.add(e);
super.visitCtLocalVariable(e);
}
@Override
public void visitCtCatch(CtCatch e) {
variables.add(e.getParameter());
super.visitCtCatch(e);
}
}
new VariableScanner().scan(parent);
return variables;
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class GenericsTest method testGetDeclarationOfTypeParameterReference.
@Test
public void testGetDeclarationOfTypeParameterReference() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/ExtendedPaella.java");
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/Paella.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtClass extendedPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "ExtendedPaella")).get(0);
List<CtTypeParameter> typeParameterList = extendedPaella.getFormalCtTypeParameters();
assertEquals(1, typeParameterList.size());
CtMethod totoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "toto")).get(0);
CtTypeReference returnTypeToto = totoMethod.getType();
CtTypeReference paramToto = ((CtParameter) totoMethod.getParameters().get(0)).getType();
CtType declaration = returnTypeToto.getDeclaration();
assertSame(typeParameterList.get(0), declaration);
assertSame(typeParameterList.get(0), paramToto.getDeclaration());
CtMethod machinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "machin")).get(0);
CtTypeReference returnTypeMachin = machinMethod.getType();
List<CtTypeParameter> formalCtTypeParameters = machinMethod.getFormalCtTypeParameters();
assertEquals(1, formalCtTypeParameters.size());
CtType declarationMachin = returnTypeMachin.getDeclaration();
assertNotSame(typeParameterList.get(0), declarationMachin);
assertSame(formalCtTypeParameters.get(0), declarationMachin);
CtClass innerPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "InnerPaella")).get(0);
List<CtTypeParameter> innerTypeParametersList = innerPaella.getFormalCtTypeParameters();
assertEquals(typeParameterList.get(0), innerTypeParametersList.get(0).getSuperclass().getDeclaration());
CtMethod innerMachinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerMachin")).get(0);
CtTypeReference returnTypeInnerMachin = innerMachinMethod.getType();
CtTypeReference paramInnerMachinType = ((CtParameter) innerMachinMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerMachinFormalCtType = innerMachinMethod.getFormalCtTypeParameters();
assertSame(typeParameterList.get(0), returnTypeInnerMachin.getDeclaration());
assertSame(innerMachinFormalCtType.get(0), paramInnerMachinType.getDeclaration());
CtMethod innerTotoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerToto")).get(0);
CtTypeReference returnInnerToto = innerTotoMethod.getType();
CtTypeReference paramInnerToto = ((CtParameter) innerTotoMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerTotoFormatCtType = innerTotoMethod.getFormalCtTypeParameters();
assertSame(innerTotoFormatCtType.get(0), paramInnerToto.getDeclaration());
assertSame(innerTypeParametersList.get(0), returnInnerToto.getDeclaration());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class GenericsTest method testConstructorCallGenerics.
@Test
public void testConstructorCallGenerics() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
assertEquals(2, aTacos.getFormalCtTypeParameters().size());
final CtTypeReference interfaces = aTacos.getSuperInterfaces().toArray(new CtTypeReference[0])[0];
assertEquals(1, interfaces.getActualTypeArguments().size());
final CtMethod<?> m = aTacos.getMethodsByName("m").get(0);
final CtElement local1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal1 = (CtTypeReference<Object>) local1.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtConstructorCall<Object> rightSideLocal1 = (CtConstructorCall<Object>) local1.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(1, leftSideLocal1.getActualTypeArguments().size());
assertEquals(1, rightSideLocal1.getType().getActualTypeArguments().size());
assertEquals("java.util.List<java.lang.String> l = new java.util.ArrayList<>()", local1.toString());
final CtElement local2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal2 = (CtTypeReference<Object>) local2.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
assertEquals(0, leftSideLocal2.getActualTypeArguments().size());
assertEquals("java.util.List l2", local2.toString());
final CtElement local3 = m.getBody().getStatement(2).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal3 = (CtTypeReference<Object>) local3.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtConstructorCall<Object> rightSideLocal3 = (CtConstructorCall<Object>) local3.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(2, leftSideLocal3.getActualTypeArguments().size());
assertEquals(2, rightSideLocal3.getType().getActualTypeArguments().size());
assertEquals("spoon.test.generics.testclasses.IBurritos<?, ?> burritos = new Burritos<>()", local3.toString());
final CtElement local4 = m.getBody().getStatement(3).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
final CtTypeReference<Object> leftSideLocal4 = (CtTypeReference<Object>) local4.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
final CtConstructorCall<Object> rightSideLocal4 = (CtConstructorCall<Object>) local4.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(1, leftSideLocal4.getActualTypeArguments().size());
assertEquals(1, rightSideLocal4.getType().getActualTypeArguments().size());
assertEquals("java.util.List<?> l3 = new java.util.ArrayList<java.lang.Object>()", local4.toString());
final CtConstructorCall constructorCall1 = (CtConstructorCall) m.getBody().getStatement(4).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(1, constructorCall1.getActualTypeArguments().size());
assertEquals(2, constructorCall1.getType().getActualTypeArguments().size());
assertEquals("new <java.lang.Integer>spoon.test.generics.testclasses.Tacos<java.lang.Object, java.lang.String>()", constructorCall1.toString());
final CtConstructorCall constructorCall2 = (CtConstructorCall) m.getBody().getStatement(5).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
assertEquals(0, constructorCall2.getActualTypeArguments().size());
assertEquals(2, constructorCall2.getType().getActualTypeArguments().size());
assertEquals("new spoon.test.generics.testclasses.Tacos<>()", constructorCall2.toString());
canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
Aggregations