use of org.sonar.java.model.declaration.MethodTreeImpl in project sonar-java by SonarSource.
the class FirstPass method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
String name = tree.returnType() == null ? CONSTRUCTOR_NAME : tree.simpleName().name();
JavaSymbol.MethodJavaSymbol symbol = new JavaSymbol.MethodJavaSymbol(computeFlags(tree.modifiers(), tree), name, env.scope.owner);
symbol.declaration = tree;
if (Flags.isFlagged(env.scope.owner.flags, Flags.ENUM) && tree.returnType() == null) {
// enum constructors are private.
symbol.flags |= Flags.PRIVATE;
}
enterSymbol(tree, symbol);
symbol.parameters = new Scope(symbol);
symbol.completer = completer;
uncompleted.add(symbol);
((MethodTreeImpl) tree).setSymbol(symbol);
createNewEnvironment(tree.typeParameters());
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
JavaSymbol.TypeVariableJavaSymbol typeVariableSymbol = new JavaSymbol.TypeVariableJavaSymbol(typeParameterTree.identifier().name(), symbol);
symbol.addTypeParameter((TypeVariableJavaType) typeVariableSymbol.type);
enterSymbol(typeParameterTree, typeVariableSymbol);
}
// Save current environment to be able to complete method later
semanticModel.saveEnv(symbol, env);
symbol.typeParameters = env.scope;
// Create new environment - this is required, because new scope is created
Resolve.Env methodEnv = env.dup();
methodEnv.scope = symbol.parameters;
methodEnv.outer = env;
env = methodEnv;
scan(tree.modifiers());
// skip type parameters.
scan(tree.returnType());
scan(tree.parameters());
scan(tree.throwsClauses());
scan(tree.defaultValue());
symbol.defaultValue = getDefaultValueFromTree(tree.defaultValue());
scan(tree.block());
restoreEnvironment(tree);
restoreEnvironment(tree);
}
use of org.sonar.java.model.declaration.MethodTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method newAnnotationTypeMethod.
public MethodTreeImpl newAnnotationTypeMethod(InternalSyntaxToken openParenToken, InternalSyntaxToken closeParenToken, Optional<Tuple<InternalSyntaxToken, ExpressionTree>> defaultValue) {
FormalParametersListTreeImpl parameters = new FormalParametersListTreeImpl(openParenToken, closeParenToken);
InternalSyntaxToken defaultToken = null;
ExpressionTree defaultExpression = null;
if (defaultValue.isPresent()) {
defaultToken = defaultValue.get().first();
defaultExpression = defaultValue.get().second();
}
return new MethodTreeImpl(parameters, defaultToken, defaultExpression);
}
use of org.sonar.java.model.declaration.MethodTreeImpl in project sonar-java by SonarSource.
the class ChangeMethodContractCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTreeImpl methodTree = (MethodTreeImpl) tree;
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
Symbol.MethodSymbol overridee = methodSymbol.overriddenSymbol();
if (overridee != null && overridee.isMethodSymbol()) {
checkContractChange(methodTree, overridee);
}
}
use of org.sonar.java.model.declaration.MethodTreeImpl in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_on_method.
@Test
public void annotation_on_method() {
CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { @MyAnnotation void method() { } }");
ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
List<AnnotationInstance> annotations = ((JavaSymbol.MethodJavaSymbol) method.symbol()).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.MethodTreeImpl in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_on_method_parameter.
@Test
public void annotation_on_method_parameter() {
CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { void method(@MyAnnotation int a) { } }");
ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
VariableTreeImpl parameter = (VariableTreeImpl) method.parameters().get(0);
List<AnnotationInstance> annotations = parameter.getSymbol().metadata().annotations();
assertThat(annotations.size()).isEqualTo(1);
assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
Aggregations