use of org.sonar.java.model.declaration.VariableTreeImpl 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.VariableTreeImpl in project sonar-java by SonarSource.
the class SecondPass method complete.
private void complete(JavaSymbol.MethodJavaSymbol symbol) {
MethodTree methodTree = symbol.declaration;
Resolve.Env env = semanticModel.getEnv(symbol);
completeTypeParameters(methodTree.typeParameters(), env);
ImmutableList.Builder<JavaType> thrownTypes = ImmutableList.builder();
for (TypeTree throwClause : methodTree.throwsClauses()) {
JavaType thrownType = resolveType(env, throwClause);
if (thrownType != null) {
thrownTypes.add(thrownType);
}
}
JavaType returnType = null;
List<JavaType> argTypes = Lists.newArrayList();
// no return type for constructor
if (!CONSTRUCTOR_NAME.equals(symbol.name)) {
returnType = resolveType(env, methodTree.returnType());
if (returnType != null) {
symbol.returnType = returnType.symbol;
}
} else if (!symbol.enclosingClass().isStatic()) {
JavaSymbol owner = symbol.enclosingClass().owner();
if (!owner.isPackageSymbol()) {
// JLS8 - 8.8.1 & 8.8.9 : constructors of inner class have an implicit first arg of its directly enclosing class type
argTypes.add(owner.enclosingClass().type);
}
}
List<VariableTree> parametersTree = methodTree.parameters();
List<JavaSymbol> scopeSymbols = symbol.parameters.scopeSymbols();
for (int i = 0; i < parametersTree.size(); i += 1) {
VariableTree variableTree = parametersTree.get(i);
JavaSymbol param = scopeSymbols.get(i);
if (variableTree.simpleName().name().equals(param.getName())) {
param.complete();
argTypes.add(param.getType());
}
if (((VariableTreeImpl) variableTree).isVararg()) {
symbol.flags |= Flags.VARARGS;
}
}
MethodJavaType methodType = new MethodJavaType(argTypes, returnType, thrownTypes.build(), (JavaSymbol.TypeJavaSymbol) symbol.owner);
symbol.setMethodType(methodType);
}
use of org.sonar.java.model.declaration.VariableTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method newVariableDeclaratorId.
public VariableTreeImpl newVariableDeclaratorId(InternalSyntaxToken identifierToken, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dims) {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
ArrayTypeTreeImpl nestedDimensions = newArrayTypeTreeWithAnnotations(dims);
return new VariableTreeImpl(identifier, nestedDimensions);
}
use of org.sonar.java.model.declaration.VariableTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method completeVariableDeclarator.
public VariableTreeImpl completeVariableDeclarator(InternalSyntaxToken identifierToken, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dimensions, Optional<VariableTreeImpl> partial) {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
ArrayTypeTreeImpl nestedDimensions = newArrayTypeTreeWithAnnotations(dimensions);
if (partial.isPresent()) {
return partial.get().completeIdentifierAndDims(identifier, nestedDimensions);
} else {
return new VariableTreeImpl(identifier, nestedDimensions);
}
}
use of org.sonar.java.model.declaration.VariableTreeImpl 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);
}
Aggregations