Search in sources :

Example 1 with MethodTreeImpl

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);
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl)

Example 2 with MethodTreeImpl

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);
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 3 with MethodTreeImpl

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);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl)

Example 4 with MethodTreeImpl

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();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 5 with MethodTreeImpl

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();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Aggregations

MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)6 Test (org.junit.Test)2 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)2 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)2 AnnotationInstance (org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance)2 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)2 ArrayTypeTreeImpl (org.sonar.java.model.JavaTree.ArrayTypeTreeImpl)1 VariableTreeImpl (org.sonar.java.model.declaration.VariableTreeImpl)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1 BlockTreeImpl (org.sonar.java.model.statement.BlockTreeImpl)1 JavaSymbol (org.sonar.java.resolve.JavaSymbol)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1 TypeParameterTree (org.sonar.plugins.java.api.tree.TypeParameterTree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1