Search in sources :

Example 16 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class Substitution method redirectTypeReferences.

/**
 * A helper method that recursively redirects all the type references from a
 * source type to a target type in the given element.
 */
public static void redirectTypeReferences(CtElement element, CtTypeReference<?> source, CtTypeReference<?> target) {
    List<CtTypeReference<?>> refs = Query.getReferences(element, new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class));
    String srcName = source.getQualifiedName();
    String targetName = target.getSimpleName();
    CtPackageReference targetPackage = target.getPackage();
    for (CtTypeReference<?> ref : refs) {
        if (ref.getQualifiedName().equals(srcName)) {
            ref.setSimpleName(targetName);
            ref.setPackage(targetPackage);
        }
    }
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 17 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class TemplateMatcher method helperMatch.

/**
 * Detects whether `template` AST node and `target` AST node are matching.
 * This method is called for each node of to be matched template
 * and for appropriate node of `target`
 *
 * @param target actually checked AST node from target model
 * @param template actually checked AST node from template
 *
 * @return true if template matches this node, false if it does not matches
 *
 * note: Made private to hide the Objects.
 */
private boolean helperMatch(Object target, Object template) {
    if ((target == null) && (template == null)) {
        return true;
    }
    if ((target == null) || (template == null)) {
        return false;
    }
    if (containsSame(variables, template) || containsSame(typeVariables, template)) {
        /*
			 * we are just matching a template parameter.
			 * Check that defined ParameterMatcher matches the target too
			 */
        boolean add = invokeCallBack(target, template);
        if (add) {
            // ParameterMatcher matches the target too, add that match
            return addMatch(template, target);
        }
        return false;
    }
    if (target.getClass() != template.getClass()) {
        return false;
    }
    if ((template instanceof CtTypeReference) && template.equals(templateType.getReference())) {
        return true;
    }
    if ((template instanceof CtPackageReference) && template.equals(templateType.getPackage())) {
        return true;
    }
    if (template instanceof CtReference) {
        CtReference tRef = (CtReference) template;
        /*
			 * Check whether name of a template reference matches with name of target reference
			 * after replacing of variables in template name
			 */
        boolean ok = matchNames(tRef.getSimpleName(), ((CtReference) target).getSimpleName());
        if (ok && !template.equals(target)) {
            boolean remove = !invokeCallBack(target, template);
            if (remove) {
                matches.remove(tRef.getSimpleName());
                return false;
            }
            return true;
        }
    }
    if (template instanceof CtNamedElement) {
        CtNamedElement named = (CtNamedElement) template;
        boolean ok = matchNames(named.getSimpleName(), ((CtNamedElement) target).getSimpleName());
        if (ok && !template.equals(target)) {
            boolean remove = !invokeCallBack(target, template);
            if (remove) {
                matches.remove(named.getSimpleName());
                return false;
            }
        }
    }
    if (template instanceof Collection) {
        return matchCollections((Collection<?>) target, (Collection<?>) template);
    }
    if (template instanceof Map) {
        if (template.equals(target)) {
            return true;
        }
        Map<?, ?> temMap = (Map<?, ?>) template;
        Map<?, ?> tarMap = (Map<?, ?>) target;
        if (!temMap.keySet().equals(tarMap.keySet())) {
            return false;
        }
        return matchCollections(tarMap.values(), temMap.values());
    }
    if (template instanceof CtBlock<?>) {
        final List<CtStatement> statements = ((CtBlock) template).getStatements();
        if (statements.size() == 1 && statements.get(0) instanceof CtInvocation) {
            final CtInvocation ctStatement = (CtInvocation) statements.get(0);
            if ("S".equals(ctStatement.getExecutable().getSimpleName()) && CtBlock.class.equals(ctStatement.getType().getActualClass())) {
                return true;
            }
        }
    }
    if (target instanceof CtElement) {
        for (Field f : RtHelper.getAllFields(target.getClass())) {
            f.setAccessible(true);
            if (Modifier.isStatic(f.getModifiers())) {
                continue;
            }
            if (f.getName().equals("parent")) {
                continue;
            }
            if (f.getName().equals("position")) {
                continue;
            }
            if (f.getName().equals("docComment")) {
                continue;
            }
            if (f.getName().equals("factory")) {
                continue;
            }
            if (f.getName().equals("comments")) {
                continue;
            }
            if (f.getName().equals("metadata")) {
                continue;
            }
            try {
                if (!helperMatch(f.get(target), f.get(template))) {
                    return false;
                }
            } catch (IllegalAccessException ignore) {
            }
        }
        return true;
    } else if (target instanceof String) {
        return matchNames((String) template, (String) target);
    } else {
        return target.equals(template);
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtField(spoon.reflect.declaration.CtField) Field(java.lang.reflect.Field) CtInvocation(spoon.reflect.code.CtInvocation) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtBlock(spoon.reflect.code.CtBlock) CtReference(spoon.reflect.reference.CtReference) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) Collection(java.util.Collection) CtNamedElement(spoon.reflect.declaration.CtNamedElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class PackageTest method testGetFQNSimple.

@Test
public void testGetFQNSimple() {
    // contract: CtPackageReference simple name is also the fully qualified name of its referenced package
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.buildModel();
    CtClass fooClass = spoon.getFactory().Class().get(Foo.class);
    CtField field = fooClass.getField("fieldList");
    CtPackageReference fieldPkg = field.getType().getPackage();
    assertEquals("java.util", fieldPkg.getSimpleName());
    assertEquals("java.util", fieldPkg.getQualifiedName());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtPackageReference (spoon.reflect.reference.CtPackageReference)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)4 CtClass (spoon.reflect.declaration.CtClass)3 CtField (spoon.reflect.declaration.CtField)3 CtReference (spoon.reflect.reference.CtReference)3 ModuleReference (org.eclipse.jdt.internal.compiler.ast.ModuleReference)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtElement (spoon.reflect.declaration.CtElement)2 CtImport (spoon.reflect.declaration.CtImport)2 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)1