use of spoon.reflect.declaration.ParentNotInitializedException 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.reflect.declaration.ParentNotInitializedException in project spoon by INRIA.
the class CtLocalVariableReferenceImpl method getDeclaration.
@SuppressWarnings("unchecked")
@Override
public CtLocalVariable<T> getDeclaration() {
// without a factory, we are not able to filter for local variables
final Factory factory = getFactory();
if (factory == null) {
return null;
}
final String simpleName = getSimpleName();
// handle the CtLocalVariableReference which were created by CtLocalVariable#getReference() and which are not yet part of model, so we cannot found them using standard rules
if (parent instanceof CtLocalVariable) {
CtLocalVariable<T> var = (CtLocalVariable<T>) parent;
if (simpleName.equals(var.getSimpleName())) {
return var;
}
}
try {
// successively iterate through all parents of this reference and
// return first result (which must be the closest declaration
// respecting visible scope)
CtVariable<?> var = map(new PotentialVariableDeclarationFunction(simpleName)).first();
if (var instanceof CtLocalVariable) {
return (CtLocalVariable<T>) var;
}
if (var != null) {
// handle it as not found
return null;
}
} catch (ParentNotInitializedException e) {
// handle this case as 'not found'
}
return null;
}
Aggregations