当前位置: 首页>>代码示例>>Java>>正文


Java Tree类代码示例

本文整理汇总了Java中org.sonar.plugins.java.api.tree.Tree的典型用法代码示例。如果您正苦于以下问题:Java Tree类的具体用法?Java Tree怎么用?Java Tree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Tree类属于org.sonar.plugins.java.api.tree包,在下文中一共展示了Tree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: buildGraph

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
private void buildGraph(Tree tree) {
  String label = tree.kind() + (tree.firstToken() != null ? (" L#" + tree.firstToken().line()) : "");
  addNode(new ASTDotNode(index, label, tree.kind()));
  if (tree.is(Tree.Kind.TOKEN)) {
    // add an extra node for tokens
    addNode(new ASTDotNode(index, escapeSpecialChars(((SyntaxToken) tree).text())));
  }
  int currentNodeIndex = index;
  if (!((JavaTree) tree).isLeaf()) {
    for (Tree child : ((JavaTree) tree).getChildren()) {
      index++;
      int childIndex = index;
      buildGraph(child);
      addEdge(new ASTDotEdge(currentNodeIndex, childIndex));
    }
  }
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:18,代码来源:ASTDotGraph.java

示例2: visitMethod

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@Override
public void visitMethod(MethodTree tree) {
  if (implementsSpecificInterface) {
    List<AnnotationTree> annotations = tree.modifiers().annotations();

    boolean isHavingMandatoryAnnotation = Boolean.FALSE;

    for (AnnotationTree annotationTree : annotations) {
      if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
        LOGGER.info("Method Name {}", idf.name());

        if (idf.name().equals(name)) {
          isHavingMandatoryAnnotation = Boolean.TRUE;
        }
      }
    }
    if (!isHavingMandatoryAnnotation) {
      context.reportIssue(this, tree, String.format("Mandatory Annotation not set @%s", name));
    }

  }
  // The call to the super implementation allows to continue the visit of the AST.
  // Be careful to always call this method to visit every node of the tree.
  super.visitMethod(tree);
}
 
开发者ID:SonarSource,项目名称:sonar-custom-rules-examples,代码行数:27,代码来源:SecurityAnnotationMandatoryRule.java

示例3: visitNode

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@Override
public void visitNode(Tree tree) {
  // Cast the node to the correct type :
  // in this case we registered only to one kind so we will only receive MethodTree see Tree.Kind enum to know about which type you can
  // cast depending on Kind.
  MethodTree methodTree = (MethodTree) tree;
  // Retrieve symbol of method.
  MethodSymbol methodSymbol = methodTree.symbol();
  Type returnType = methodSymbol.returnType().type();
  // Check method has only one argument.
  if (methodSymbol.parameterTypes().size() == 1) {
    Type argType = methodSymbol.parameterTypes().get(0);
    // Verify argument type is same as return type.
    if (argType.is(returnType.fullyQualifiedName())) {
      // raise an issue on this node of the SyntaxTree
      reportIssue(tree, "message");
    }
  }
}
 
开发者ID:SonarSource,项目名称:sonar-custom-rules-examples,代码行数:20,代码来源:MyCustomSubscriptionRule.java

示例4: visitNode

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@Override
public void visitNode(Tree tree) {
  // Visit CLASS node only => cast could be done
  ClassTree treeClazz = (ClassTree) tree;

  // No extends => stop to visit class
  if (treeClazz.superClass() == null) {
    return;
  }

  // For 'symbolType' usage, jar in dependencies must be on classpath, !unknownSymbol! result otherwise
  String superClassName = treeClazz.superClass().symbolType().fullyQualifiedName();

  // Check if superClass avoid
  if (SUPER_CLASS_AVOID.contains(superClassName)) {
    reportIssue(tree, String.format("The usage of super class %s is forbidden", superClassName));
  }
}
 
开发者ID:SonarSource,项目名称:sonar-custom-rules-examples,代码行数:19,代码来源:AvoidSuperClassRule.java

示例5: visitMethod

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@Override
public void visitMethod(MethodTree tree) {
  List<AnnotationTree> annotations = tree.modifiers().annotations();
  for (AnnotationTree annotationTree : annotations) {
    if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
      IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
      System.out.println(idf.name());

      if (idf.name().equals(name)) {
        context.reportIssue(this, idf, String.format("Avoid using annotation @%s", name));
      }
    }
  }

  // The call to the super implementation allows to continue the visit of the AST.
  // Be careful to always call this method to visit every node of the tree.
  super.visitMethod(tree);
}
 
开发者ID:SonarSource,项目名称:sonar-custom-rules-examples,代码行数:19,代码来源:AvoidAnnotationRule.java

示例6: checkIfLongSession

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
protected boolean checkIfLongSession(MethodTree method) {
	List<AnnotationTree> annotations = method.modifiers().annotations();
	for (AnnotationTree annotationTree : annotations) {
		if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
			IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
			if (idf.name().equals(ACTIVATE)) {
				collectLongSessionOpened(method);
				return true;
			}
			else if (idf.name().equals(DEACTIVATE)) {
				collectLongSessionClosed(method);
				return true;
			}
		}
	}
	return false;
}
 
开发者ID:Cognifide,项目名称:AEM-Rules-for-SonarQube,代码行数:18,代码来源:SessionShouldBeLoggedOut.java

示例7: checkIfLongResourceResolver

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
protected boolean checkIfLongResourceResolver(MethodTree method) {
	List<AnnotationTree> annotations = method.modifiers().annotations();
	for (AnnotationTree annotationTree : annotations) {
		if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
			IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
			if (idf.name().equals(ACTIVATE)) {
				collectLongResourceResolverOpened(method);
				return true;
			}
			else if (idf.name().equals(DEACTIVATE)) {
				collectLongResourceResolverClosed(method);
				return true;
			}
		}
	}
	return false;
}
 
开发者ID:Cognifide,项目名称:AEM-Rules-for-SonarQube,代码行数:18,代码来源:ResourceResolverShouldBeClosed.java

示例8: appendElements

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
private static void appendElements(StringBuilder buffer, CFG.Block block) {
  int i = 0;
  for (Tree tree : block.elements()) {
    buffer.append('\n');
    buffer.append(i);
    buffer.append(":\t");
    appendKind(buffer, tree.kind());
    buffer.append(toString(tree));
    i++;
  }
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:12,代码来源:CFGPrinter.java

示例9: appendTerminator

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
private static void appendTerminator(StringBuilder buffer, CFG.Block block) {
  Tree terminator = block.terminator();
  if (terminator != null) {
    buffer.append("\nT:\t");
    appendKind(buffer, terminator.kind());
    buffer.append(toString(terminator));
  }
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:9,代码来源:CFGPrinter.java

示例10: toString

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
private static String toString(Tree tree) {
  Stream.Builder<String> sb = Stream.builder();
  switch (tree.kind()) {
    case TOKEN:
      sb.add(((SyntaxToken) tree).text());
      break;
    case VARIABLE:
      VariableTree vt = (VariableTree) tree;
      // skip initializer
      addTrees(sb, vt.type(), vt.simpleName());
      break;
    case NEW_CLASS:
      NewClassTree nct = (NewClassTree) tree;
      // skip body for anonymous classes
      addTrees(sb, nct.newKeyword(), nct.identifier(), nct.arguments());
      break;
    case MEMBER_SELECT:
      MemberSelectExpressionTree mset = (MemberSelectExpressionTree) tree;
      if (mset.expression().is(Tree.Kind.METHOD_INVOCATION)) {
        // skip method invocation
        addTrees(sb, mset.identifier());
      } else {
        addChildren(sb, tree);
      }
      break;
    case IF_STATEMENT:
      IfStatementTree ist = (IfStatementTree) tree;
      // skip thenClause and elseClause
      addTrees(sb, ist.ifKeyword(), ist.openParenToken(), ist.condition(), ist.closeParenToken());
      break;
    default:
      addChildren(sb, tree);
      break;
  }
  return sb.build().filter(text -> !text.isEmpty()).collect(Collectors.joining(" "));
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:37,代码来源:CFGPrinter.java

示例11: addChildren

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
private static void addChildren(Stream.Builder<String> sb, Tree tree) {
  if (tree.is(Tree.Kind.INFERED_TYPE)) {
    // do nothing, infered types does not have children
    return;
  }
  addTrees(sb, ((JavaTree) tree).getChildren());
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:8,代码来源:CFGPrinter.java

示例12: programPoint

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
private String programPoint() {
  String tree = "";
  if (pp.i < pp.block.elements().size()) {
    Tree syntaxNode = ((CFG.Block) pp.block).elements().get(pp.i);
    tree = "" + syntaxNode.kind() + " L#" + syntaxNode.firstToken().line();
  }
  return programPointKey() + "  " + tree;
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:9,代码来源:EGDotNode.java

示例13: getMethodBehavior

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@CheckForNull
private static MethodBehavior getMethodBehavior(BehaviorCache bc, @Nullable Tree syntaxTree) {
  if (syntaxTree == null || !syntaxTree.is(Tree.Kind.METHOD_INVOCATION)) {
    return null;
  }
  Symbol symbol = ((MethodInvocationTree) syntaxTree).symbol();
  if (!symbol.isMethodSymbol()) {
    return null;
  }
  return bc.get((Symbol.MethodSymbol) symbol);
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:12,代码来源:EGDotNode.java

示例14: getFirstMethodOrConstructor

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@CheckForNull
private static MethodTree getFirstMethodOrConstructor(CompilationUnitTree cut) {
  return (MethodTree) cut.types().stream()
    .findFirst()
    .map(ClassTree.class::cast)
    .map(ClassTree::members)
    .map(List::stream)
    .flatMap(members -> members.filter(m -> m.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)).findFirst())
    .orElse(null);
}
 
开发者ID:SonarSource,项目名称:source-graph-viewer,代码行数:11,代码来源:Viewer.java

示例15: visitNode

import org.sonar.plugins.java.api.tree.Tree; //导入依赖的package包/类
@Override
public void visitNode(Tree tree) {

  if (((NewClassTree) tree).symbolType().isSubtypeOf("org.apache.commons.collections4.list.UnmodifiableList")) {
    reportIssue(tree, "Avoid using UnmodifiableList");
  }
}
 
开发者ID:SonarSource,项目名称:sonar-custom-rules-examples,代码行数:8,代码来源:AvoidUnmodifiableListRule.java


注:本文中的org.sonar.plugins.java.api.tree.Tree类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。