use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_on_variable.
@Test
public void annotation_on_variable() {
CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { @MyAnnotation Object field; }");
ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
VariableTreeImpl variable = (VariableTreeImpl) clazz.members().get(0);
List<AnnotationInstance> annotations = variable.getSymbol().metadata().annotations();
assertThat(annotations.size()).isEqualTo(1);
assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method identifier_of_variable_symbol.
@Test
public void identifier_of_variable_symbol() {
CompilationUnitTree compilationUnit = treeOf("class A { Object field; }");
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(0);
VariableTree variable = (VariableTree) clazz.members().get(0);
assertThat(variable.symbol().isUnknown()).isFalse();
assertThat(variable.symbol().usages()).isEmpty();
assertThat(variable.simpleName().symbol().isUnknown()).isFalse();
assertThat(variable.simpleName().symbol()).isEqualTo(variable.symbol());
}
use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method newAnnotationType.
public ClassTreeImpl newAnnotationType(InternalSyntaxToken openBraceToken, Optional<List<JavaTree>> annotationTypeElementDeclarations, InternalSyntaxToken closeBraceToken) {
// TODO
ModifiersTreeImpl emptyModifiers = ModifiersTreeImpl.emptyModifiers();
ImmutableList.Builder<Tree> members = ImmutableList.builder();
if (annotationTypeElementDeclarations.isPresent()) {
for (JavaTree annotationTypeElementDeclaration : annotationTypeElementDeclarations.get()) {
if (annotationTypeElementDeclaration.getGrammarRuleKey().equals(JavaLexer.VARIABLE_DECLARATORS)) {
for (VariableTreeImpl variable : (VariableDeclaratorListTreeImpl) annotationTypeElementDeclaration) {
members.add(variable);
}
} else if (!annotationTypeElementDeclaration.is(Kind.TOKEN)) {
members.add(annotationTypeElementDeclaration);
}
}
}
return new ClassTreeImpl(emptyModifiers, openBraceToken, members.build(), closeBraceToken);
}
use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.
the class FirstPass method visitClass.
@Override
public void visitClass(ClassTree tree) {
int flag = 0;
boolean anonymousClass = tree.simpleName() == null;
String name = "";
if (!anonymousClass) {
name = tree.simpleName().name();
flag = computeClassFlags(tree);
}
JavaSymbol.TypeJavaSymbol symbol = new JavaSymbol.TypeJavaSymbol(flag, name, env.scope.owner);
symbol.declaration = tree;
((ClassTreeImpl) tree).setSymbol(symbol);
// TODO : register also based on flags ?
if (!anonymousClass) {
if (env.scope.owner.kind == JavaSymbol.TYP || env.scope.owner.kind == JavaSymbol.PCK) {
resolve.registerClass(symbol);
}
enterSymbol(tree, symbol);
}
symbol.members = new Scope(symbol);
symbol.completer = completer;
uncompleted.add(symbol);
// Define type parameters:
createNewEnvironment(tree.typeParameters());
// Save current environment to be able to complete class later
semanticModel.saveEnv(symbol, env);
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
JavaSymbol.TypeVariableJavaSymbol typeVariableSymbol = new JavaSymbol.TypeVariableJavaSymbol(typeParameterTree.identifier().name(), symbol);
symbol.addTypeParameter((TypeVariableJavaType) typeVariableSymbol.type);
enterSymbol(typeParameterTree, typeVariableSymbol);
}
symbol.typeParameters = env.scope;
Resolve.Env classEnv = env.dup();
classEnv.outer = env;
classEnv.enclosingClass = symbol;
classEnv.scope = symbol.members;
env = classEnv;
semanticModel.associateEnv(tree, env);
scan(tree.modifiers());
// skip type parameters
scan(tree.superClass());
scan(tree.superInterfaces());
scan(tree.members());
if (tree.is(Tree.Kind.ENUM)) {
// implicit methods from enum: JLS8 : 8.9.2
// add 'public static E[] values()'
JavaSymbol.MethodJavaSymbol valuesMethod = new JavaSymbol.MethodJavaSymbol((symbol.flags & Flags.ACCESS_FLAGS) | Flags.STATIC, "values", symbol);
ArrayJavaType enumArrayType = new ArrayJavaType(symbol.type, symbols.arrayClass);
MethodJavaType valuesMethodType = new MethodJavaType(ImmutableList.<JavaType>of(), enumArrayType, ImmutableList.<JavaType>of(), symbol);
valuesMethod.setMethodType(valuesMethodType);
valuesMethod.parameters = new Scope(valuesMethod);
classEnv.scope.enter(valuesMethod);
// add 'public static E valueOf(String name)'
JavaSymbol.MethodJavaSymbol valueOfMethod = new JavaSymbol.MethodJavaSymbol((symbol.flags & Flags.ACCESS_FLAGS) | Flags.STATIC, "valueOf", symbol);
MethodJavaType valueOfMethodType = new MethodJavaType(ImmutableList.<JavaType>of(symbols.stringType), symbol.type, ImmutableList.<JavaType>of(), symbol);
valueOfMethod.setMethodType(valueOfMethodType);
valueOfMethod.parameters = new Scope(valueOfMethod);
valueOfMethod.parameters.enter(new JavaSymbol.VariableJavaSymbol(0, "name", symbols.stringType, valueOfMethod));
classEnv.scope.enter(valueOfMethod);
}
restoreEnvironment(tree);
restoreEnvironment(tree);
}
use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method assertUnary.
private void assertUnary(String input, Type.Primitives expectedPrimitive) {
CompilationUnitTree compilationUnit = treeOf(input);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(0);
Type type = ((ReturnStatementTree) ((MethodTree) clazz.members().get(1)).block().body().get(0)).expression().symbolType();
assertThat(type.isPrimitive(expectedPrimitive)).isTrue();
}
Aggregations