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