use of spoon.support.reflect.CtExtendedModifier 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);
}
}
use of spoon.support.reflect.CtExtendedModifier in project spoon by INRIA.
the class ReferenceBuilder method getQualifiedTypeReference.
/**
* Builds a type reference from a qualified name when a type specified in the name isn't available.
*
* @param tokens Qualified name.
* @param receiverType Last type in the qualified name.
* @param enclosingType Enclosing type of the type name.
* @param listener Listener to know if we must build the type reference.
* @return a type reference.
*/
<T> CtTypeReference<T> getQualifiedTypeReference(char[][] tokens, TypeBinding receiverType, ReferenceBinding enclosingType, JDTTreeBuilder.OnAccessListener listener) {
final List<CtExtendedModifier> listPublicProtected = Arrays.asList(new CtExtendedModifier(ModifierKind.PUBLIC), new CtExtendedModifier(ModifierKind.PROTECTED));
if (enclosingType != null && Collections.disjoint(listPublicProtected, JDTTreeBuilderQuery.getModifiers(enclosingType.modifiers, false, false))) {
String access = "";
int i = 0;
final CompilationUnitDeclaration[] units = ((TreeBuilderCompiler) this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.environment.typeRequestor).unitsToProcess;
for (; i < tokens.length; i++) {
final char[][] qualified = Arrays.copyOfRange(tokens, 0, i + 1);
if (searchPackage(qualified, units) == null) {
access = CharOperation.toString(qualified);
break;
}
}
if (!access.contains(CtPackage.PACKAGE_SEPARATOR)) {
access = searchType(access, this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.imports);
}
final TypeBinding accessBinding = searchTypeBinding(access, units);
if (accessBinding != null && listener.onAccess(tokens, i)) {
final TypeBinding superClassBinding = searchTypeBinding(accessBinding.superclass(), CharOperation.charToString(tokens[i + 1]));
if (superClassBinding != null) {
return this.getTypeReference(superClassBinding.clone(accessBinding));
} else {
return this.getTypeReference(receiverType);
}
} else {
return this.getTypeReference(receiverType);
}
}
return null;
}
use of spoon.support.reflect.CtExtendedModifier in project spoon by INRIA.
the class TryCatchTest method testCatchWithExplicitFinalVariable.
@Test
public void testCatchWithExplicitFinalVariable() throws IOException {
// contract: explicit final modifier are possible in catch variables and should be kept when pretty-printing
String inputResource = "./src/test/java/spoon/test/trycatch/testclasses/Bar.java";
Launcher launcher = new Launcher();
launcher.addInputResource(inputResource);
launcher.getEnvironment().setComplianceLevel(5);
launcher.buildModel();
CtTry tryStmt = launcher.getModel().getElements(new TypeFilter<>(CtTry.class)).get(0);
List<CtCatch> catchers = tryStmt.getCatchers();
assertEquals(1, catchers.size());
CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();
assertTrue(catchVariable.hasModifier(ModifierKind.FINAL));
Set<CtExtendedModifier> extendedModifierSet = catchVariable.getExtendedModifiers();
assertEquals(1, extendedModifierSet.size());
assertEquals(new CtExtendedModifier(ModifierKind.FINAL, false), extendedModifierSet.iterator().next());
launcher = new Launcher();
launcher.addInputResource(inputResource);
launcher.setSourceOutputDirectory("./target/spoon-trycatch");
launcher.getEnvironment().setShouldCompile(true);
launcher.getEnvironment().setComplianceLevel(5);
launcher.run();
File f = new File("target/spoon-trycatch/spoon/test/trycatch/testclasses/Bar.java");
String content = StringUtils.join(Files.readAllLines(f.toPath()), "\n");
assertTrue(content.contains("catch (final java.lang.Exception e)"));
}
use of spoon.support.reflect.CtExtendedModifier in project spoon by INRIA.
the class TestInterfaceWithoutSetup method testModifierFromInterfaceFieldAndMethod.
@Test
public void testModifierFromInterfaceFieldAndMethod() {
// contract: methods defined in interface are all public and fields are all public and static
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/spoon/test/itf/DumbItf.java");
spoon.getEnvironment().setNoClasspath(false);
spoon.buildModel();
CtType dumbType = spoon.getFactory().Type().get("toto.DumbItf");
assertEquals(2, dumbType.getFields().size());
CtField fieldImplicit = dumbType.getField("CONSTANT_INT");
Set<CtExtendedModifier> extendedModifierSet = fieldImplicit.getExtendedModifiers();
assertEquals(3, extendedModifierSet.size());
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.FINAL, true)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.STATIC, true)));
for (CtExtendedModifier extendedModifier : extendedModifierSet) {
assertTrue(extendedModifier.isImplicit());
}
assertEquals(ModifierKind.PUBLIC, fieldImplicit.getVisibility());
assertTrue(fieldImplicit.hasModifier(ModifierKind.STATIC));
assertTrue(fieldImplicit.hasModifier(ModifierKind.PUBLIC));
assertTrue(fieldImplicit.hasModifier(ModifierKind.FINAL));
CtField fieldExplicit = dumbType.getField("ANOTHER_INT");
extendedModifierSet = fieldExplicit.getExtendedModifiers();
assertEquals(3, extendedModifierSet.size());
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.FINAL, true)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, false)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.STATIC, false)));
int counter = 0;
for (CtExtendedModifier extendedModifier : extendedModifierSet) {
if (extendedModifier.getKind() == ModifierKind.FINAL) {
assertTrue(extendedModifier.isImplicit());
counter++;
} else {
assertFalse(extendedModifier.isImplicit());
counter++;
}
}
assertEquals(3, counter);
assertEquals(ModifierKind.PUBLIC, fieldExplicit.getVisibility());
assertTrue(fieldExplicit.hasModifier(ModifierKind.STATIC));
assertTrue(fieldExplicit.hasModifier(ModifierKind.PUBLIC));
assertTrue(fieldExplicit.hasModifier(ModifierKind.FINAL));
assertEquals(4, dumbType.getMethods().size());
CtMethod staticMethod = (CtMethod) dumbType.getMethodsByName("foo").get(0);
assertTrue(staticMethod.hasModifier(ModifierKind.PUBLIC));
assertTrue(staticMethod.hasModifier(ModifierKind.STATIC));
extendedModifierSet = staticMethod.getExtendedModifiers();
assertEquals(2, extendedModifierSet.size());
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.STATIC, false)));
CtMethod publicMethod = (CtMethod) dumbType.getMethodsByName("machin").get(0);
assertTrue(publicMethod.hasModifier(ModifierKind.PUBLIC));
assertFalse(publicMethod.hasModifier(ModifierKind.STATIC));
extendedModifierSet = publicMethod.getExtendedModifiers();
assertEquals(2, extendedModifierSet.size());
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.ABSTRACT, true)));
CtMethod defaultMethod = (CtMethod) dumbType.getMethodsByName("bla").get(0);
assertTrue(defaultMethod.hasModifier(ModifierKind.PUBLIC));
assertTrue(defaultMethod.isDefaultMethod());
assertFalse(defaultMethod.hasModifier(ModifierKind.STATIC));
extendedModifierSet = defaultMethod.getExtendedModifiers();
assertEquals(1, extendedModifierSet.size());
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
CtMethod explicitDefaultMethod = (CtMethod) dumbType.getMethodsByName("anotherOne").get(0);
assertTrue(explicitDefaultMethod.hasModifier(ModifierKind.PUBLIC));
extendedModifierSet = explicitDefaultMethod.getExtendedModifiers();
assertEquals(2, extendedModifierSet.size());
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, false)));
assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.ABSTRACT, true)));
}
Aggregations