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


Java LiteralTree类代码示例

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


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

示例1: findValue

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
public static ExpressionTree findValue(AnnotationTree m, String name) {
    for (ExpressionTree et : m.getArguments()) {
        if (et.getKind() == Kind.ASSIGNMENT) {
            AssignmentTree at = (AssignmentTree) et;
            String varName = ((IdentifierTree) at.getVariable()).getName().toString();

            if (varName.equals(name)) {
                return at.getExpression();
            }
        }

        if (et instanceof LiteralTree/*XXX*/ && "value".equals(name)) {
            return et;
        }
    }

    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:Utilities.java

示例2: visitIdentifier

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Override
public Number visitIdentifier(IdentifierTree node, Void p) {
    String name = node.getName().toString();
    Tree newNode = handleIdentifier(name, node);
    
    if (newNode != null) {
        rewrite(node, newNode);
        if (NUMBER_LITERAL_KINDS.contains(newNode.getKind())) {
            return (Number) ((LiteralTree) newNode).getValue();
        }
    }

    Element e = info.getTrees().getElement(getCurrentPath());

    if (e != null && isStaticElement(e) && !inImport) {
        rewrite(node, make.QualIdent(e));
    }

    return super.visitIdentifier(node, p);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:JavaFixUtilities.java

示例3: getBundleName

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
private String getBundleName(MethodInvocationTree n, int index, String bfn) {
    if (n.getArguments().size() <= index) {
        return null;
    }
    ExpressionTree t = n.getArguments().get(index);
    // recognize just string literals + .class references
    if (t.getKind() == Tree.Kind.STRING_LITERAL) {
        Object o = ((LiteralTree)t).getValue();
        return o == null ? null : o.toString();
    } else if (t.getKind() == Tree.Kind.MEMBER_SELECT) {
        MemberSelectTree mst = (MemberSelectTree)t;
        if (!mst.getIdentifier().contentEquals("class")) {
            return null;
        }
        return bundleFileFromClass(new TreePath(getCurrentPath(), mst.getExpression()), bfn);
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:ResourceStringFoldProvider.java

示例4: matches

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Test
public void matches() {
  // TODO(b/67738557): consolidate helpers for creating fake trees
  LiteralTree tree =
      new LiteralTree() {

        @Override
        public Kind getKind() {
          throw new UnsupportedOperationException();
        }

        @Override
        public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
          throw new UnsupportedOperationException();
        }

        @Override
        public Object getValue() {
          return "a string literal";
        }
      };
  assertTrue(new StringLiteral("a string literal").matches(tree, null));
}
 
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:StringLiteralTest.java

示例5: matchMethodInvocation

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree t, VisitorState state) {
  if (PRECONDITIONS_CHECK.matches(t, state)
      && t.getArguments().size() >= 2
      && t.getArguments().get(1) instanceof LiteralTree) {
    LiteralTree formatStringTree = (LiteralTree) t.getArguments().get(1);
    if (formatStringTree.getValue() instanceof String) {
      String formatString = (String) formatStringTree.getValue();
      int expectedArgs = expectedArguments(formatString);
      if (expectedArgs < t.getArguments().size() - 2
          && BAD_PLACEHOLDER_REGEX.matcher(formatString).find()) {
        return describe(t, state);
      }
    }
  }
  return Description.NO_MATCH;
}
 
开发者ID:google,项目名称:error-prone,代码行数:18,代码来源:PreconditionsInvalidPlaceholder.java

示例6: describe

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
public Description describe(MethodInvocationTree t, VisitorState state) {
  LiteralTree formatTree = (LiteralTree) t.getArguments().get(1);

  String fixedFormatString =
      BAD_PLACEHOLDER_REGEX.matcher(state.getSourceForNode((JCTree) formatTree)).replaceAll("%s");
  if (expectedArguments(fixedFormatString) == t.getArguments().size() - 2) {
    return describeMatch(formatTree, SuggestedFix.replace(formatTree, fixedFormatString));
  } else {
    int missing = t.getArguments().size() - 2 - expectedArguments(fixedFormatString);
    StringBuilder builder = new StringBuilder(fixedFormatString);
    builder.deleteCharAt(builder.length() - 1);
    builder.append(" [%s");
    for (int i = 1; i < missing; i++) {
      builder.append(", %s");
    }
    builder.append("]\"");
    return describeMatch(t, SuggestedFix.replace(formatTree, builder.toString()));
  }
}
 
开发者ID:google,项目名称:error-prone,代码行数:20,代码来源:PreconditionsInvalidPlaceholder.java

示例7: matches

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
  Type leftType = ((JCTree) tree.getLeftOperand()).type;
  Types types = state.getTypes();
  Symtab symtab = state.getSymtab();
  if (!(types.isSameType(leftType, symtab.intType))
      && !(types.isSameType(leftType, symtab.byteType))
      && !(types.isSameType(leftType, symtab.shortType))
      && !(types.isSameType(leftType, symtab.charType))) {
    return false;
  }

  ExpressionTree rightOperand = tree.getRightOperand();
  if (rightOperand instanceof LiteralTree) {
    Object rightValue = ((LiteralTree) rightOperand).getValue();
    if (rightValue instanceof Number) {
      int intValue = ((Number) rightValue).intValue();
      return intValue < 0 || intValue > 31;
    }
  }

  return false;
}
 
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:BadShiftAmount.java

示例8: matchBinary

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
  if (!BINARY_TREE_MATCHER.matches(tree, state)) {
    return Description.NO_MATCH;
  }

  /*
   * For shift amounts in [32, 63], cast the left operand to long.  Otherwise change the shift
   * amount to whatever would actually be used.
   */
  int intValue = ((Number) ((LiteralTree) tree.getRightOperand()).getValue()).intValue();

  Fix fix;
  if (intValue >= 32 && intValue <= 63) {
    if (tree.getLeftOperand().getKind() == Kind.INT_LITERAL) {
      fix = SuggestedFix.postfixWith(tree.getLeftOperand(), "L");
    } else {
      fix = SuggestedFix.prefixWith(tree, "(long) ");
    }
  } else {
    // This is the equivalent shift distance according to JLS 15.19.
    String actualShiftDistance = Integer.toString(intValue & 0x1f);
    fix = SuggestedFix.replace(tree.getRightOperand(), actualShiftDistance);
  }
  return describeMatch(tree, fix);
}
 
开发者ID:google,项目名称:error-prone,代码行数:27,代码来源:BadShiftAmount.java

示例9: matchDivZero

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
private Description matchDivZero(Tree tree, ExpressionTree operand, VisitorState state) {
  if (!anyOf(kindIs(Kind.DIVIDE), kindIs(Kind.DIVIDE_ASSIGNMENT)).matches(tree, state)) {
    return Description.NO_MATCH;
  }

  if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) {
    return Description.NO_MATCH;
  }

  LiteralTree rightOperand = (LiteralTree) operand;
  if (((Integer) rightOperand.getValue()) != 0) {
    return Description.NO_MATCH;
  }

  // Find and replace enclosing Statement.
  StatementTree enclosingStmt =
      ASTHelpers.findEnclosingNode(state.getPath(), StatementTree.class);
  return (enclosingStmt != null)
      ? describeMatch(
          tree,
          SuggestedFix.replace(enclosingStmt, "throw new ArithmeticException(\"/ by zero\");"))
      : describeMatch(tree);
}
 
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:DivZero.java

示例10: isGreaterThanEqualToZero

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
private ExpressionType isGreaterThanEqualToZero(BinaryTree tree) {
  ExpressionTree literalOperand;
  ExpressionType returnType;

  switch (tree.getKind()) {
    case GREATER_THAN_EQUAL:
      literalOperand = tree.getRightOperand();
      returnType = ExpressionType.GREATER_THAN_EQUAL;
      break;
    case LESS_THAN_EQUAL:
      literalOperand = tree.getLeftOperand();
      returnType = ExpressionType.LESS_THAN_EQUAL;
      break;
    default:
      return ExpressionType.MISMATCH;
  }

  if (literalOperand.getKind() != Kind.INT_LITERAL) {
    return ExpressionType.MISMATCH;
  }
  if (!((LiteralTree) literalOperand).getValue().equals(0)) {
    return ExpressionType.MISMATCH;
  }

  return returnType;
}
 
开发者ID:google,项目名称:error-prone,代码行数:27,代码来源:SizeGreaterThanOrEqualsZero.java

示例11: matchNewClass

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
  if (ASTHelpers.isSameType(
          state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state)
      && tree.getArguments().size() == 1) {
    ExpressionTree argument = tree.getArguments().get(0);
    Type type = ((JCTree) argument).type;
    if (type.getKind() == TypeKind.CHAR) {
      if (argument.getKind() == Kind.CHAR_LITERAL) {
        char ch = (Character) ((LiteralTree) argument).getValue();
        return describeMatch(
            tree,
            SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
      } else {
        return describeMatch(
            tree,
            SuggestedFix.replace(
                tree,
                "new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")"));
      }
    }
  }
  return Description.NO_MATCH;
}
 
开发者ID:google,项目名称:error-prone,代码行数:25,代码来源:StringBuilderInitWithChar.java

示例12: literalHasIdentifierMatching

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
private Scanner literalHasIdentifierMatching(
    final boolean shouldMatch, final Matcher<Tree> toMatch) {
  ScannerTest test =
      new ScannerTest() {
        private boolean matched = false;

        @Override
        public Void visitLiteral(LiteralTree node, VisitorState visitorState) {
          visitorState = visitorState.withPath(getCurrentPath());
          if (toMatch.matches(node, visitorState)) {
            matched = true;
          }
          return super.visitLiteral(node, visitorState);
        }

        @Override
        void assertDone() {
          assertEquals(matched, shouldMatch);
        }
      };
  tests.add(test);
  return test;
}
 
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:HasIdentifierTest.java

示例13: describe

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
public Description describe(MethodInvocationTree t, VisitorState state) {
  LiteralTree formatTree = (LiteralTree) t.getArguments().get(1);

  String fixedFormatString = state.getSourceForNode((JCTree) formatTree).toString()
      .replaceAll(BAD_PLACEHOLDER_REGEX, "%s");
  SuggestedFix fix = new SuggestedFix();
  if (expectedArguments(fixedFormatString) == t.getArguments().size() - 2) {
    fix.replace(formatTree, fixedFormatString);
    return describeMatch(formatTree, fix);
  } else {
    int missing = t.getArguments().size() - 2 - expectedArguments(fixedFormatString);
    StringBuilder builder = new StringBuilder(fixedFormatString);
    builder.deleteCharAt(builder.length() - 1);
    builder.append(" [%s");
    for (int i = 1; i < missing; i++) {
      builder.append(", %s");
    }
    builder.append("]\"");
    fix.replace(formatTree, builder.toString());
    return describeMatch(t, fix);
  }
}
 
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:23,代码来源:PreconditionsTooManyArgs.java

示例14: matches

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
  Type leftType = ((JCTree) tree.getLeftOperand()).type;
  Types types = state.getTypes();
  Symtab symtab = state.getSymtab();
  if (!(types.isSameType(leftType, symtab.intType)) &&
      !(types.isSameType(leftType, symtab.byteType)) &&
      !(types.isSameType(leftType, symtab.shortType)) &&
      !(types.isSameType(leftType, symtab.charType))) {
    return false;
  }

  ExpressionTree rightOperand = tree.getRightOperand();
  if (rightOperand instanceof LiteralTree) {
    Object rightValue = ((LiteralTree) rightOperand).getValue();
    if (rightValue instanceof Number) {
      int intValue = ((Number) rightValue).intValue();
      return intValue < 0 || intValue > 31;
    }
  }

  return false;
}
 
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:24,代码来源:BadShiftAmount.java

示例15: isConstantOne

import com.sun.source.tree.LiteralTree; //导入依赖的package包/类
private boolean isConstantOne(JCExpression exp) {
  Tree.Kind kind = exp.getKind();

  if (kind == Kind.INT_LITERAL || kind == Kind.LONG_LITERAL || kind == Kind.FLOAT_LITERAL
      || kind == Kind.DOUBLE_LITERAL) {
    if (exp instanceof LiteralTree) {
      Object literalValue = ((LiteralTree) exp).getValue();
      if (literalValue instanceof Number) {
        int intValue = ((Number) literalValue).intValue();
        if (intValue == 1) {
          return true;
        }
      }
    }
  }
  return false;
}
 
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:18,代码来源:ElementsCountedInLoop.java


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