use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class BadLocalVariableNameCheck method visitCatch.
@Override
public void visitCatch(CatchTree tree) {
VariableTree parameter = tree.parameter();
if (parameter.simpleName().name().length() > 1) {
scan(parameter);
}
scan(tree.block());
}
use of org.sonar.plugins.java.api.tree.VariableTree 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.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SecondPass method complete.
private void complete(JavaSymbol.VariableJavaSymbol symbol) {
VariableTree variableTree = symbol.declaration;
Resolve.Env env = semanticModel.getEnv(symbol);
if (variableTree.is(Tree.Kind.ENUM_CONSTANT)) {
symbol.type = env.enclosingClass.type;
} else {
symbol.type = resolveType(env, variableTree.type());
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method outside_method.
@Test
public void outside_method() throws Exception {
String code = newCode("int b;", "int foo() {", " return b;", "}");
ClassTree classTree = classTree(code);
List<StatementTree> statements = ((MethodTree) classTree.members().get(1)).block().body();
ExpressionTree variableDeclaration = ((VariableTree) (classTree.members().get(0))).initializer();
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, variableDeclaration);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_between_two_trees.
@Test
public void report_issue_between_two_trees() {
ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
VariableTree firstMember = (VariableTree) tree.members().get(0);
VariableTree secondMember = (VariableTree) tree.members().get(1);
context.reportIssue(CHECK, firstMember.simpleName(), secondMember.equalToken(), "msg");
assertThat(reportedMessage.getMessage()).isEqualTo("msg");
assertThat(reportedMessage.getCost()).isNull();
assertThat(reportedMessage.flows).isEmpty();
assertMessagePosition(reportedMessage, 2, 6, 3, 10);
}
Aggregations