Search in sources :

Example 6 with CtExtendedModifier

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);
    }
}
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 CtExtendedModifier

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;
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier)

Example 8 with CtExtendedModifier

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)"));
}
Also used : Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCatch(spoon.reflect.code.CtCatch) CtTry(spoon.reflect.code.CtTry) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier) File(java.io.File) Test(org.junit.Test)

Example 9 with CtExtendedModifier

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)));
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtExtendedModifier (spoon.support.reflect.CtExtendedModifier)9 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 Receiver (org.eclipse.jdt.internal.compiler.ast.Receiver)1 UnionTypeReference (org.eclipse.jdt.internal.compiler.ast.UnionTypeReference)1 BaseTypeBinding (org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding)1 BinaryTypeBinding (org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding)1 LocalTypeBinding (org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding)1 MissingTypeBinding (org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding)1 ParameterizedTypeBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding)1 PolyTypeBinding (org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding)1 RawTypeBinding (org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding)1 SourceTypeBinding (org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding)1 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)1 CtCatch (spoon.reflect.code.CtCatch)1