Search in sources :

Example 1 with DefaultParameterMatcher

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

the class TemplateMatcher method findParameterMatcher.

/**
 * returns an appropriate ParameterMatcher defined in a template parameter, or else a default one
 *
 * if a template parameter (field annotated with @Parameter) whose name (field name) is a substring of the template name, it also works
 */
private ParameterMatcher findParameterMatcher(CtNamedElement templateDeclaration) throws InstantiationException, IllegalAccessException {
    if (templateDeclaration == null) {
        return new DefaultParameterMatcher();
    }
    String name = templateDeclaration.getSimpleName();
    CtClass<?> clazz = null;
    try {
        clazz = templateDeclaration.getParent(CtClass.class);
    } catch (ParentNotInitializedException e) {
        Launcher.LOGGER.error(e.getMessage(), e);
    }
    if (clazz == null) {
        return new DefaultParameterMatcher();
    }
    Collection<CtFieldReference<?>> fields = clazz.getAllFields();
    CtFieldReference<?> param = null;
    for (CtFieldReference<?> fieldRef : fields) {
        Parameter p = fieldRef.getDeclaration().getAnnotation(Parameter.class);
        if (p == null) {
            // not a parameter.
            continue;
        }
        String proxy = p.value();
        if (!"".equals(proxy)) {
            if (name.contains(proxy)) {
                param = fieldRef;
                break;
            }
        }
        if (name.contains(fieldRef.getSimpleName())) {
            param = fieldRef;
            break;
        }
    // todo: check for field hack.
    }
    return getParameterInstance(param);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) DefaultParameterMatcher(spoon.support.template.DefaultParameterMatcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtParameter(spoon.reflect.declaration.CtParameter)

Example 2 with DefaultParameterMatcher

use of spoon.support.template.DefaultParameterMatcher 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 3 with DefaultParameterMatcher

use of spoon.support.template.DefaultParameterMatcher 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)3 DefaultParameterMatcher (spoon.support.template.DefaultParameterMatcher)3 ParameterMatcher (spoon.support.template.ParameterMatcher)2 CtFieldAccess (spoon.reflect.code.CtFieldAccess)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtClass (spoon.reflect.declaration.CtClass)1 CtNamedElement (spoon.reflect.declaration.CtNamedElement)1 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)1 CtFieldReference (spoon.reflect.reference.CtFieldReference)1 CtReference (spoon.reflect.reference.CtReference)1