Search in sources :

Example 1 with TypeJavaSymbol

use of org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol in project sonar-java by SonarSource.

the class TypeSubstitutionSolver method substitutionFromSuperType.

static TypeSubstitution substitutionFromSuperType(ParametrizedTypeJavaType target, ParametrizedTypeJavaType source) {
    TypeSubstitution result = new TypeSubstitution(target.typeSubstitution);
    if (target.rawType != source.rawType) {
        TypeJavaSymbol targetSymbol = target.symbol;
        Type superClass = targetSymbol.superClass();
        if (superClass != null && ((JavaType) superClass).isParameterized()) {
            TypeSubstitution newSub = substitutionFromSuperType((ParametrizedTypeJavaType) superClass, source);
            result = result.combine(newSub);
        }
        for (Type superInterface : targetSymbol.interfaces()) {
            if (((JavaType) superInterface).isParameterized()) {
                TypeSubstitution newSub = substitutionFromSuperType((ParametrizedTypeJavaType) superInterface, source);
                result = result.combine(newSub);
            }
        }
    } else {
        result = target.typeSubstitution.combine(source.typeSubstitution);
    }
    return result;
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Type(org.sonar.plugins.java.api.semantic.Type)

Example 2 with TypeJavaSymbol

use of org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol in project sonar-java by SonarSource.

the class BytecodeCompleterTest method owning_class_name.

@Test
public void owning_class_name() throws Exception {
    TypeJavaSymbol classSymbolCase1 = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.DistinguishNames$Case1$OWNER$$Child");
    TypeJavaSymbol classSymbolCase2 = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.DistinguishNames$Case2$OWNER$$Child");
    assertThat(classSymbolCase1.owner().name).isEqualTo("OWNER");
    assertThat(classSymbolCase1.name).isEqualTo("$Child");
    assertThat(classSymbolCase2.owner().name).isEqualTo("OWNER$");
    assertThat(classSymbolCase2.name).isEqualTo("Child");
    // No warning about a class not found
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Test(org.junit.Test)

Example 3 with TypeJavaSymbol

use of org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol in project sonar-java by SonarSource.

the class BytecodeCompleterTest method test_loading_java9_class.

@Test
public void test_loading_java9_class() throws Exception {
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(Collections.singletonList(new File("src/test/files/bytecode/java9/bin"))), new ParametrizedTypeCache());
    new Symbols(bytecodeCompleter);
    TypeJavaSymbol classSymbol = (TypeJavaSymbol) bytecodeCompleter.loadClass("org.test.Hello9");
    classSymbol.complete();
    assertThat(classSymbol.getFullyQualifiedName()).isEqualTo("org.test.Hello9");
    assertThat(classSymbol.memberSymbols()).hasSize(2);
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 4 with TypeJavaSymbol

use of org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol in project sonar-java by SonarSource.

the class BytecodeCompleterTest method test_loading_java9_iface.

@Test
public void test_loading_java9_iface() throws Exception {
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(Collections.singletonList(new File("src/test/files/bytecode/java9/bin"))), new ParametrizedTypeCache());
    new Symbols(bytecodeCompleter);
    TypeJavaSymbol iface = (TypeJavaSymbol) bytecodeCompleter.loadClass("org.test.IfaceTest");
    iface.complete();
    assertThat(iface.getFullyQualifiedName()).isEqualTo("org.test.IfaceTest");
    assertThat(iface.memberSymbols()).hasSize(3);
    assertThat(iface.isInterface()).isTrue();
    JavaSymbol.MethodJavaSymbol privateMethod = (JavaSymbol.MethodJavaSymbol) Iterables.getOnlyElement(iface.lookupSymbols("privateMethod"));
    assertThat(privateMethod.flags()).isEqualTo(Flags.PRIVATE);
    JavaSymbol.MethodJavaSymbol defaultMethod = (JavaSymbol.MethodJavaSymbol) Iterables.getOnlyElement(iface.lookupSymbols("defaultMethod"));
    assertThat(defaultMethod.flags()).isEqualTo(Flags.DEFAULT | Flags.PUBLIC);
    JavaSymbol.MethodJavaSymbol staticMethod = (JavaSymbol.MethodJavaSymbol) Iterables.getOnlyElement(iface.lookupSymbols("staticMethod"));
    assertThat(staticMethod.flags()).isEqualTo(Flags.STATIC | Flags.PUBLIC);
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 5 with TypeJavaSymbol

use of org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol in project sonar-java by SonarSource.

the class TypeSubstitutionSolver method getReturnType.

JavaType getReturnType(@Nullable JavaType returnType, JavaType defSite, JavaType callSite, TypeSubstitution substitution, JavaSymbol.MethodJavaSymbol method) {
    JavaType resultType = returnType;
    if (method.isConstructor()) {
        if (constructParametrizedTypeWithoutSubstitution(method, defSite)) {
            resultType = applySubstitution(defSite, substitution);
        } else {
            return defSite;
        }
    }
    // The actual result type [of getClass] is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called.
    if (defSite == symbols.objectType && "getClass".equals(method.name())) {
        TypeJavaSymbol classSymbol = symbols.classType.symbol;
        JavaType wildcardType = parametrizedTypeCache.getWildcardType(callSite.erasure(), WildCardType.BoundType.EXTENDS);
        resultType = parametrizedTypeCache.getParametrizedTypeType(classSymbol, new TypeSubstitution().add(classSymbol.typeVariableTypes.get(0), wildcardType));
    }
    resultType = applySiteSubstitution(resultType, defSite);
    if (callSite != defSite) {
        resultType = applySiteSubstitution(resultType, callSite);
    }
    resultType = applySubstitution(resultType, substitution);
    if (!isReturnTypeCompletelySubstituted(resultType, method.typeVariableTypes) || (method.isConstructor() && !isReturnTypeCompletelySubstituted(resultType, defSite.symbol.typeVariableTypes))) {
        resultType = symbols.deferedType(resultType);
    }
    return resultType;
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol)

Aggregations

TypeJavaSymbol (org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol)7 Test (org.junit.Test)4 File (java.io.File)2 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)2 Type (org.sonar.plugins.java.api.semantic.Type)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1