Search in sources :

Example 1 with CtExecutable

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;
}
Also used : SpoonException(spoon.SpoonException) CtField(spoon.reflect.declaration.CtField) CtScanner(spoon.reflect.visitor.CtScanner) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 2 with CtExecutable

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);
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtType(spoon.reflect.declaration.CtType) SourcePosition(spoon.reflect.cu.SourcePosition) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 3 with CtExecutable

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);
            }
        }
    }
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtType(spoon.reflect.declaration.CtType) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 4 with CtExecutable

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;
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtInvocation(spoon.reflect.code.CtInvocation) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtReference(spoon.reflect.reference.CtReference) CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 5 with CtExecutable

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;
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) CtExecutable(spoon.reflect.declaration.CtExecutable) CtBodyHolder(spoon.reflect.code.CtBodyHolder) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) CtCatch(spoon.reflect.code.CtCatch) CtModifiable(spoon.reflect.declaration.CtModifiable)

Aggregations

CtExecutable (spoon.reflect.declaration.CtExecutable)26 Test (org.junit.Test)10 CtType (spoon.reflect.declaration.CtType)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)7 Launcher (spoon.Launcher)6 CtElement (spoon.reflect.declaration.CtElement)6 Factory (spoon.reflect.factory.Factory)6 File (java.io.File)5 CtConstructor (spoon.reflect.declaration.CtConstructor)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SpoonException (spoon.SpoonException)4 CtField (spoon.reflect.declaration.CtField)4 CtMethod (spoon.reflect.declaration.CtMethod)4 SpoonModelBuilder (spoon.SpoonModelBuilder)3 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLambda (spoon.reflect.code.CtLambda)3 CtClass (spoon.reflect.declaration.CtClass)3 CtParameter (spoon.reflect.declaration.CtParameter)3