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