use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class Refactoring method copyType.
/**
* See doc in {@link CtType#copyType()}
*/
public static CtType<?> copyType(final CtType<?> type) {
CtType<?> clone = type.clone();
String tentativeTypeName = type.getSimpleName() + "Copy";
while (type.getFactory().Type().get(type.getPackage().getQualifiedName() + "." + tentativeTypeName) != null) {
tentativeTypeName += "X";
}
final String cloneTypeName = tentativeTypeName;
clone.setSimpleName(cloneTypeName);
type.getPackage().addType(clone);
new CtScanner() {
@Override
public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
if (reference.getDeclaration() == null) {
return;
}
if (reference.getDeclaration() == type) {
reference.setSimpleName(cloneTypeName);
}
if (reference.getDeclaration() != clone) {
throw new SpoonException("post condition broken " + reference);
}
super.visitCtTypeReference(reference);
}
@Override
public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) {
CtExecutable<T> declaration = reference.getDeclaration();
if (declaration == null) {
return;
}
if (declaration.hasParent(type)) {
reference.getDeclaringType().setSimpleName(cloneTypeName);
}
if (!reference.getDeclaration().hasParent(clone)) {
throw new SpoonException("post condition broken " + reference);
}
super.visitCtExecutableReference(reference);
}
@Override
public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
CtField<T> declaration = reference.getDeclaration();
if (declaration == null) {
return;
}
if (declaration.hasParent(type)) {
reference.getDeclaringType().setSimpleName(cloneTypeName);
}
if (reference.getDeclaration() == null || !reference.getDeclaration().hasParent(clone)) {
throw new SpoonException("post condition broken " + reference);
}
super.visitCtFieldReference(reference);
}
}.scan(clone);
return clone;
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class StandardEnvironment method report.
@Override
public void report(Processor<?> processor, Level level, CtElement element, String message) {
StringBuffer buffer = new StringBuffer();
prefix(buffer, level);
// Adding message
buffer.append(message);
// Add sourceposition (javac format)
try {
CtType<?> type = (element instanceof CtType) ? (CtType<?>) element : element.getParent(CtType.class);
SourcePosition sp = element.getPosition();
if (sp == null) {
buffer.append(" (Unknown Source)");
} else {
buffer.append(" at " + type.getQualifiedName() + ".");
CtExecutable<?> exe = (element instanceof CtExecutable) ? (CtExecutable<?>) element : element.getParent(CtExecutable.class);
if (exe != null) {
buffer.append(exe.getSimpleName());
}
buffer.append("(" + sp.getFile().getName() + ":" + sp.getLine() + ")");
}
} catch (ParentNotInitializedException e) {
buffer.append(" (invalid parent)");
}
print(buffer.toString(), level);
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class CtTypeParameterTest method checkType.
private void checkType(CtType<?> type) throws NoSuchFieldException, SecurityException {
List<CtTypeParameter> formalTypeParameters = type.getFormalCtTypeParameters();
for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
checkTypeParamErasureOfType(ctTypeParameter, type.getActualClass());
}
for (CtTypeMember member : type.getTypeMembers()) {
if (member instanceof CtFormalTypeDeclarer) {
CtFormalTypeDeclarer ftDecl = (CtFormalTypeDeclarer) member;
formalTypeParameters = ftDecl.getFormalCtTypeParameters();
if (member instanceof CtExecutable<?>) {
CtExecutable<?> exec = (CtExecutable<?>) member;
for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
checkTypeParamErasureOfExecutable(ctTypeParameter);
}
for (CtParameter<?> param : exec.getParameters()) {
checkParameterErasureOfExecutable(param);
}
} else if (member instanceof CtType<?>) {
CtType<?> nestedType = (CtType<?>) member;
// recursive call for nested type
checkType(nestedType);
}
}
}
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class ImportScannerImpl method addClassImport.
/**
* Adds a type to the classImports.
*/
protected boolean addClassImport(CtTypeReference<?> ref) {
this.exploredReferences.add(ref);
if (ref == null) {
return false;
}
if (targetType != null && targetType.getSimpleName().equals(ref.getSimpleName()) && !targetType.equals(ref)) {
return false;
}
if (classImports.containsKey(ref.getSimpleName())) {
return isImportedInClassImports(ref);
}
// don't import unnamed package elements
if (ref.getPackage() == null || ref.getPackage().isUnnamedPackage()) {
return false;
}
if (targetType != null && targetType.canAccess(ref) == false) {
// ref type is not visible in targetType we must not add import for it, java compiler would fail on that.
return false;
}
if (this.isThereAnotherClassWithSameNameInAnotherPackage(ref)) {
return false;
}
if (targetType != null) {
try {
CtElement parent = ref.getParent();
if (parent != null) {
parent = parent.getParent();
if (parent != null) {
if ((parent instanceof CtFieldAccess) || (parent instanceof CtExecutable) || (parent instanceof CtInvocation)) {
CtTypeReference declaringType;
CtReference reference;
CtPackageReference pack = targetType.getPackage();
if (parent instanceof CtFieldAccess) {
CtFieldAccess field = (CtFieldAccess) parent;
CtFieldReference localReference = field.getVariable();
declaringType = localReference.getDeclaringType();
reference = localReference;
} else if (parent instanceof CtExecutable) {
CtExecutable exec = (CtExecutable) parent;
CtExecutableReference localReference = exec.getReference();
declaringType = localReference.getDeclaringType();
reference = localReference;
} else if (parent instanceof CtInvocation) {
CtInvocation invo = (CtInvocation) parent;
CtExecutableReference localReference = invo.getExecutable();
declaringType = localReference.getDeclaringType();
reference = localReference;
} else {
declaringType = null;
reference = null;
}
if (reference != null && isImported(reference)) {
// if we are in the **same** package we do the import for test with method isImported
if (declaringType != null) {
if (declaringType.getPackage() != null && !declaringType.getPackage().isUnnamedPackage()) {
// ignore java.lang package
if (!declaringType.getPackage().getSimpleName().equals("java.lang")) {
// ignore type in same package
if (declaringType.getPackage().getSimpleName().equals(pack.getSimpleName())) {
classImports.put(ref.getSimpleName(), ref);
return true;
}
}
}
}
}
}
}
}
} catch (ParentNotInitializedException e) {
}
CtPackageReference pack = targetType.getPackage();
if (pack != null && ref.getPackage() != null && !ref.getPackage().isUnnamedPackage()) {
// ignore java.lang package
if (!ref.getPackage().getSimpleName().equals("java.lang")) {
// ignore type in same package
if (ref.getPackage().getSimpleName().equals(pack.getSimpleName())) {
return false;
}
}
}
}
classImports.put(ref.getSimpleName(), ref);
return true;
}
use of spoon.reflect.declaration.CtExecutable 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;
}
}
Aggregations