Search in sources :

Example 11 with CtReference

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

the class ReferenceBuilder method getTypeReference.

/**
 * Try to build a CtTypeReference from a simple name with specified generic types but
 * returns null if the name doesn't correspond to a type (not start by an upper case).
 */
private <T> CtTypeReference<T> getTypeReference(String name) {
    CtTypeReference<T> main = null;
    if (name.matches(".*(<.+>)")) {
        Pattern pattern = Pattern.compile("([^<]+)<(.+)>");
        Matcher m = pattern.matcher(name);
        if (name.startsWith("?")) {
            main = (CtTypeReference) this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
        } else {
            main = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        }
        if (m.find()) {
            main.setSimpleName(m.group(1));
            final String[] split = m.group(2).split(",");
            for (String parameter : split) {
                ((CtTypeReference) main).addActualTypeArgument(getTypeParameterReference(parameter.trim()));
            }
        }
    } else if (Character.isUpperCase(name.charAt(0))) {
        main = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        main.setSimpleName(name);
        final CtReference declaring = this.getDeclaringReferenceFromImports(name.toCharArray());
        setPackageOrDeclaringType(main, declaring);
    } else if (name.startsWith("?")) {
        return (CtTypeReference) this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
    }
    return main;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CtReference(spoon.reflect.reference.CtReference) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 12 with CtReference

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

the class EqualsChecker method scanCtReference.

@Override
public void scanCtReference(CtReference reference) {
    final CtReference peek = (CtReference) this.other;
    if (!reference.getSimpleName().equals(peek.getSimpleName())) {
        isNotEqual = true;
        return;
    }
    super.scanCtReference(reference);
}
Also used : CtReference(spoon.reflect.reference.CtReference)

Example 13 with CtReference

use of spoon.reflect.reference.CtReference 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 14 with CtReference

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

the class TemplateMatcher method invokeCallBack.

/**
 * invokes {@link ParameterMatcher} associated to the `template` (= template parameter)
 * @param target a potentially matching element
 * @param template a matching parameter, which may define extra {@link ParameterMatcher}
 * @return true if {@link ParameterMatcher} of `template` matches on `target`
 */
private boolean invokeCallBack(Object target, Object template) {
    try {
        if (template instanceof CtInvocation) {
            CtFieldAccess<?> param = (CtFieldAccess<?>) ((CtInvocation<?>) template).getTarget();
            ParameterMatcher instance = getParameterInstance(param.getVariable());
            return instance.match(this, (CtInvocation<?>) template, (CtElement) target);
        } else if (template instanceof CtReference) {
            // Get parameter
            CtReference ref = (CtReference) template;
            ParameterMatcher instance;
            if (ref.getDeclaration() == null || ref.getDeclaration().getAnnotation(Parameter.class) == null) {
                instance = new DefaultParameterMatcher();
            } else {
                Parameter param = ref.getDeclaration().getAnnotation(Parameter.class);
                instance = param.match().newInstance();
            }
            return instance.match(this, (CtReference) template, (CtReference) target);
        } else if (template instanceof CtNamedElement) {
            CtNamedElement named = (CtNamedElement) template;
            ParameterMatcher instance = findParameterMatcher(named);
            return instance.match(this, (CtElement) template, (CtElement) target);
        } else {
            // Should not happen
            throw new RuntimeException();
        }
    } catch (InstantiationException e) {
        Launcher.LOGGER.error(e.getMessage(), e);
        return true;
    } catch (IllegalAccessException e) {
        Launcher.LOGGER.error(e.getMessage(), e);
        return true;
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtInvocation(spoon.reflect.code.CtInvocation) DefaultParameterMatcher(spoon.support.template.DefaultParameterMatcher) CtReference(spoon.reflect.reference.CtReference) CtParameter(spoon.reflect.declaration.CtParameter) DefaultParameterMatcher(spoon.support.template.DefaultParameterMatcher) ParameterMatcher(spoon.support.template.ParameterMatcher) CtNamedElement(spoon.reflect.declaration.CtNamedElement)

Aggregations

CtReference (spoon.reflect.reference.CtReference)14 CtTypeReference (spoon.reflect.reference.CtTypeReference)9 CtElement (spoon.reflect.declaration.CtElement)5 CtInvocation (spoon.reflect.code.CtInvocation)4 List (java.util.List)3 CtField (spoon.reflect.declaration.CtField)3 CtNamedElement (spoon.reflect.declaration.CtNamedElement)3 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)3 CtPackageReference (spoon.reflect.reference.CtPackageReference)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 BaseTypeBinding (org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding)2 BinaryTypeBinding (org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding)2 LocalTypeBinding (org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding)2 MissingTypeBinding (org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding)2 ParameterizedTypeBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding)2 PolyTypeBinding (org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding)2 RawTypeBinding (org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding)2 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)2 SourceTypeBinding (org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding)2