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