Search in sources :

Example 1 with ParameterMatcher

use of spoon.support.template.ParameterMatcher in project spoon by INRIA.

the class TemplateMatcher method getParameterInstance.

/**
 * returns a specific ParameterMatcher corresponding to the field acting as template parameter
 */
private ParameterMatcher getParameterInstance(CtFieldReference<?> param) throws InstantiationException, IllegalAccessException {
    if (param == null) {
        // return a default impl
        return new DefaultParameterMatcher();
    }
    Parameter anParam = param.getDeclaration().getAnnotation(Parameter.class);
    if (anParam == null) {
        // return a default impl
        return new DefaultParameterMatcher();
    }
    Class<? extends ParameterMatcher> pm = anParam.match();
    ParameterMatcher instance = pm.newInstance();
    return instance;
}
Also used : DefaultParameterMatcher(spoon.support.template.DefaultParameterMatcher) CtParameter(spoon.reflect.declaration.CtParameter) DefaultParameterMatcher(spoon.support.template.DefaultParameterMatcher) ParameterMatcher(spoon.support.template.ParameterMatcher)

Example 2 with ParameterMatcher

use of spoon.support.template.ParameterMatcher 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

CtParameter (spoon.reflect.declaration.CtParameter)2 DefaultParameterMatcher (spoon.support.template.DefaultParameterMatcher)2 ParameterMatcher (spoon.support.template.ParameterMatcher)2 CtFieldAccess (spoon.reflect.code.CtFieldAccess)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtNamedElement (spoon.reflect.declaration.CtNamedElement)1 CtReference (spoon.reflect.reference.CtReference)1