Search in sources :

Example 16 with ParentNotInitializedException

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);
}
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 17 with ParentNotInitializedException

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;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) PotentialVariableDeclarationFunction(spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction) Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable)

Aggregations

ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)17 CtElement (spoon.reflect.declaration.CtElement)9 CtInvocation (spoon.reflect.code.CtInvocation)4 CtExecutable (spoon.reflect.declaration.CtExecutable)4 CtStatement (spoon.reflect.code.CtStatement)3 CtMethod (spoon.reflect.declaration.CtMethod)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtType (spoon.reflect.declaration.CtType)3 Factory (spoon.reflect.factory.Factory)3 CtFieldReference (spoon.reflect.reference.CtFieldReference)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Test (org.junit.Test)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtBlock (spoon.reflect.code.CtBlock)2 CtCatch (spoon.reflect.code.CtCatch)2 CtIf (spoon.reflect.code.CtIf)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtStatementList (spoon.reflect.code.CtStatementList)2