Search in sources :

Example 1 with ModifierKind

use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.

the class SnippetCompilationHelper method compileAndReplaceSnippetsIn.

public static void compileAndReplaceSnippetsIn(CtType<?> c) {
    Factory f = c.getFactory();
    CtType<?> workCopy = c;
    Set<ModifierKind> backup = EnumSet.noneOf(ModifierKind.class);
    backup.addAll(workCopy.getModifiers());
    workCopy.removeModifier(ModifierKind.PUBLIC);
    try {
        build(f, workCopy.toString());
    } finally {
        // restore modifiers
        c.setModifiers(backup);
    }
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind) Factory(spoon.reflect.factory.Factory)

Example 2 with ModifierKind

use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.

the class SnippetCompilationHelper method createWrapperContent.

private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) {
    CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);
    CtBlock body = f.Core().createBlock();
    if (element instanceof CtStatement) {
        body.addStatement((CtStatement) element);
    } else if (element instanceof CtExpression) {
        CtReturn ret = f.Core().createReturn();
        ret.setReturnedExpression((CtExpression) element);
        body.addStatement(ret);
    }
    Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC);
    Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>();
    thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference());
    f.Method().create(w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body);
    String contents = w.toString();
    // Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly
    w.getPackage().removeType(w);
    return contents;
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) CtReturn(spoon.reflect.code.CtReturn) CtTypeReference(spoon.reflect.reference.CtTypeReference) ModifierKind(spoon.reflect.declaration.ModifierKind) HashSet(java.util.HashSet)

Example 3 with ModifierKind

use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.

the class CtModifierHandler method setModifiers.

public CtModifierHandler setModifiers(Set<ModifierKind> modifiers) {
    if (modifiers != null && modifiers.size() > 0) {
        getFactory().getEnvironment().getModelChangeListener().onSetDeleteAll(element, MODIFIER, this.modifiers, new HashSet<>(this.modifiers));
        this.modifiers.clear();
        for (ModifierKind modifier : modifiers) {
            addModifier(modifier);
        }
    }
    return this;
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind)

Example 4 with ModifierKind

use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.

the class ElementPrinterHelper method writeModifiers.

/**
 *  writes the modifiers of this modifiable in a specific order
 */
public void writeModifiers(CtModifiable modifiable) {
    // write the modifiers according to the convention on order
    // visibility: public, private, protected
    List<String> firstPosition = new ArrayList<>();
    // keywords: static, abstract
    List<String> secondPosition = new ArrayList<>();
    // all other things
    List<String> thirdPosition = new ArrayList<>();
    for (CtExtendedModifier extendedModifier : modifiable.getExtendedModifiers()) {
        if (!extendedModifier.isImplicit()) {
            ModifierKind modifierKind = extendedModifier.getKind();
            if (modifierKind == ModifierKind.PUBLIC || modifierKind == ModifierKind.PRIVATE || modifierKind == ModifierKind.PROTECTED) {
                firstPosition.add(modifierKind.toString());
            } else if (modifierKind == ModifierKind.ABSTRACT || modifierKind == ModifierKind.STATIC) {
                secondPosition.add(modifierKind.toString());
            } else {
                thirdPosition.add(modifierKind.toString());
            }
        }
    }
    for (String s : firstPosition) {
        printer.writeKeyword(s).writeSpace();
    }
    for (String s : secondPosition) {
        printer.writeKeyword(s).writeSpace();
    }
    for (String s : thirdPosition) {
        printer.writeKeyword(s).writeSpace();
    }
}
Also used : ArrayList(java.util.ArrayList) ModifierKind(spoon.reflect.declaration.ModifierKind) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier)

Example 5 with ModifierKind

use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.

the class TestModifiers method testMethodWithVarargsDoesNotBecomeTransient.

@Test
public void testMethodWithVarargsDoesNotBecomeTransient() {
    // contract: method with varsargs should not become transient
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/MethodVarArgs.java");
    spoon.buildModel();
    CtType<?> myClass = spoon.getFactory().Type().get(MethodVarArgs.class);
    CtMethod methodVarargs = myClass.getMethodsByName("getInitValues").get(0);
    Set<ModifierKind> expectedModifiers = Collections.singleton(ModifierKind.PROTECTED);
    assertEquals(expectedModifiers, methodVarargs.getModifiers());
    spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/MethodVarArgs.java");
    spoon.getEnvironment().setShouldCompile(true);
    spoon.run();
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

ModifierKind (spoon.reflect.declaration.ModifierKind)8 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Factory (spoon.reflect.factory.Factory)3 ArrayList (java.util.ArrayList)2 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 Launcher (spoon.Launcher)1 CtBlock (spoon.reflect.code.CtBlock)1 CtExpression (spoon.reflect.code.CtExpression)1 CtReturn (spoon.reflect.code.CtReturn)1 CtStatement (spoon.reflect.code.CtStatement)1 CtElement (spoon.reflect.declaration.CtElement)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtParameter (spoon.reflect.declaration.CtParameter)1 ClassFactory (spoon.reflect.factory.ClassFactory)1 ConstructorFactory (spoon.reflect.factory.ConstructorFactory)1 CoreFactory (spoon.reflect.factory.CoreFactory)1 FieldFactory (spoon.reflect.factory.FieldFactory)1 TypeFactory (spoon.reflect.factory.TypeFactory)1 DefaultCoreFactory (spoon.support.DefaultCoreFactory)1