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;
}
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;
}
}
Aggregations