Search in sources :

Example 6 with CtCatchVariable

use of spoon.reflect.code.CtCatchVariable in project spoon by INRIA.

the class JDTTreeBuilderHelper method createCatchVariable.

/**
 * Creates a catch variable from a type reference.
 *
 * @param typeReference
 * 		Correspond to the exception type declared in the catch.
 * @return a catch variable.
 */
CtCatchVariable<Throwable> createCatchVariable(TypeReference typeReference) {
    final Argument jdtCatch = (Argument) jdtTreeBuilder.getContextBuilder().stack.peekFirst().node;
    final Set<CtExtendedModifier> modifiers = getModifiers(jdtCatch.modifiers, false, false);
    CtCatchVariable<Throwable> result = jdtTreeBuilder.getFactory().Core().createCatchVariable();
    result.<CtCatchVariable>setSimpleName(CharOperation.charToString(jdtCatch.name)).setExtendedModifiers(modifiers);
    if (typeReference instanceof UnionTypeReference) {
        // do not set type of variable yet. It will be initialized later by visit of multiple types. Each call then ADDs one type
        return result;
    } else {
        CtTypeReference ctTypeReference = jdtTreeBuilder.getReferencesBuilder().<Throwable>getTypeReference(typeReference.resolvedType);
        return result.<CtCatchVariable>setType(ctTypeReference);
    }
}
Also used : Argument(org.eclipse.jdt.internal.compiler.ast.Argument) CtTypeReference(spoon.reflect.reference.CtTypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier)

Example 7 with CtCatchVariable

use of spoon.reflect.code.CtCatchVariable in project Ex2Amplifier by STAMP-project.

the class MainGenerator method addCatchGivenExceptionToTry.

private static void addCatchGivenExceptionToTry(CtTypeReference exceptionType, Factory factory, CtTry aTry, String suffixNameException) {
    final CtCatch aCatch = factory.createCatch();
    final CtCatchVariable catchVariable = factory.createCatchVariable();
    catchVariable.setSimpleName("__exceptionEx2Amplifier" + suffixNameException);
    catchVariable.setType(exceptionType);
    aCatch.setParameter(catchVariable);
    final CtThrow aThrow = factory.createThrow();
    aThrow.setThrownExpression(factory.createConstructorCall(factory.Type().createReference("java.lang.RuntimeException"), factory.createVariableRead(factory.createCatchVariableReference(catchVariable), false)));
    aCatch.setBody(aThrow);
    aTry.addCatcher(aCatch);
}
Also used : CtThrow(spoon.reflect.code.CtThrow) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

Aggregations

CtCatchVariable (spoon.reflect.code.CtCatchVariable)7 CtElement (spoon.reflect.declaration.CtElement)4 CtCatch (spoon.reflect.code.CtCatch)3 Test (org.junit.Test)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtField (spoon.reflect.declaration.CtField)2 CtParameter (spoon.reflect.declaration.CtParameter)2 LocalVariableScopeFunction (spoon.reflect.visitor.filter.LocalVariableScopeFunction)2 List (java.util.List)1 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 UnionTypeReference (org.eclipse.jdt.internal.compiler.ast.UnionTypeReference)1 Launcher (spoon.Launcher)1 SpoonException (spoon.SpoonException)1 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtExpression (spoon.reflect.code.CtExpression)1 CtStatement (spoon.reflect.code.CtStatement)1 CtThrow (spoon.reflect.code.CtThrow)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtType (spoon.reflect.declaration.CtType)1