use of spoon.reflect.declaration.CtType 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.declaration.CtType in project spoon by INRIA.
the class ImportScannerImpl method computeImports.
@Override
public void computeImports(CtElement element) {
// look for top declaring type of that simpleType
if (element instanceof CtType) {
CtType simpleType = (CtType) element;
targetType = simpleType.getReference().getTopLevelType();
addClassImport(simpleType.getReference());
scan(simpleType);
} else {
CtType<?> type = element.getParent(CtType.class);
targetType = type == null ? null : type.getReference().getTopLevelType();
scan(element);
}
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class ImportScannerImpl method visitCtClass.
@Override
public <T> void visitCtClass(CtClass<T> ctClass) {
addClassImport(ctClass.getReference());
for (CtTypeMember t : ctClass.getTypeMembers()) {
if (!(t instanceof CtType)) {
continue;
}
addClassImport(((CtType) t).getReference());
}
super.visitCtClass(ctClass);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class PotentialVariableDeclarationFunction method apply.
@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
isTypeOnTheWay = false;
isInStaticScope = false;
// Search previous siblings for element which may represents the declaration of this local variable
CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
if (variableName != null) {
// variable name is defined so we have to search only for variables with that name
siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
}
CtElement scopeElement = input;
// Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
CtElement parent = scopeElement.getParent();
if (parent instanceof CtType<?>) {
isTypeOnTheWay = true;
// visit each CtField of `parent` CtType
CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
q.forEach((CtField<?> field) -> {
if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
/*
* the variable reference is used in static scope,
* but the field is not static - ignore it
*/
return;
}
// else send field as potential variable declaration
if (sendToOutput(field, outputConsumer)) {
// and terminate the internal query q if outer query is already terminated
q.terminate();
}
});
if (query.isTerminated()) {
return;
}
} else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
// visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
if (query.isTerminated()) {
return;
}
// visit parameters of CtCatch and CtExecutable (method, lambda)
if (parent instanceof CtCatch) {
CtCatch ctCatch = (CtCatch) parent;
if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
return;
}
} else if (parent instanceof CtExecutable) {
CtExecutable<?> exec = (CtExecutable<?>) parent;
for (CtParameter<?> param : exec.getParameters()) {
if (sendToOutput(param, outputConsumer)) {
return;
}
}
}
}
if (parent instanceof CtModifiable) {
isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
}
scopeElement = parent;
}
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class AllTypeMembersFunction method apply.
@Override
public void apply(CtTypeInformation input, final CtConsumer<Object> outputConsumer) {
final CtQuery q = ((CtQueryable) input).map(new SuperInheritanceHierarchyFunction(distinctSet == null ? new HashSet<String>() : distinctSet).includingSelf(true));
q.forEach(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> type) {
for (CtTypeMember typeMember : type.getTypeMembers()) {
if (memberClass == null || memberClass.isInstance(typeMember)) {
outputConsumer.accept(typeMember);
}
if (query.isTerminated()) {
q.terminate();
}
}
}
});
}
Aggregations