Package com.sun.tools.javac.tree.JCTree

Examples of com.sun.tools.javac.tree.JCTree.JCLiteral


  private Matcher<LiteralTree> literalHasActualStartPosition(final int startPosition) {
    return new Matcher<LiteralTree>() {
      @Override
      public boolean matches(LiteralTree tree, VisitorState state) {
        JCLiteral literal = (JCLiteral) tree;
        return ASTHelpers.getActualStartPosition(literal, state.getSourceCode()) == startPosition;
      }
    };
  }
View Full Code Here


            Matchers.<ExpressionTree>anything()),
        state);
    if (binaryTreeMatches == null) {
      throw new IllegalStateException("Expected one of the operands to be a literal");
    }
    JCLiteral literal = (JCLiteral) binaryTreeMatches.get(0);
    JCTree nonLiteralOperand = (JCTree) binaryTreeMatches.get(1);
    boolean byteMatch = state.getTypes().isSameType(nonLiteralOperand.type,
        state.getSymtab().byteType);

    boolean willEvaluateTo = (tree.getKind() != Kind.EQUAL_TO);
    Fix fix;
    String customDiagnosticMessage;
    if (byteMatch) {
      String replacement = Byte.toString(((Number) literal.getValue()).byteValue());

      // Correct for poor javac 6 literal parsing.
      int actualStart = ASTHelpers.getActualStartPosition(literal, state.getSourceCode());
      if (actualStart != literal.getStartPosition()) {
        fix =
            SuggestedFix.replace(literal, replacement, actualStart - literal.getStartPosition(), 0);
      } else {
        fix = SuggestedFix.replace(literal, replacement);
      }
      customDiagnosticMessage = String.format(MESSAGE_TEMPLATE, "byte", (int) Byte.MIN_VALUE,
          (int) Byte.MAX_VALUE, literal.toString(), Boolean.toString(willEvaluateTo));
    } else {
      fix = SuggestedFix.replace(tree, Boolean.toString(willEvaluateTo));
      customDiagnosticMessage = String.format(MESSAGE_TEMPLATE, "char", (int) Character.MIN_VALUE,
          (int) Character.MAX_VALUE, literal.toString(), Boolean.toString(willEvaluateTo));
    }
    return Description.builder(tree, pattern)
        .addFix(fix)
        .setMessage(customDiagnosticMessage)
        .build();
View Full Code Here

              Matchers.<ExpressionTree>isSameType(comparisonType)),
          state);
      if (binaryTreeMatches == null) {
        return false;
      }
      JCLiteral literal = (JCLiteral) binaryTreeMatches.get(0);

      // Check whether literal is out of range for the specified type.  Logic is based on
      // JLS 5.6.2 - Binary Numeric Promotion:
      // If either is double, other is converted to double.
      // If either is float, other is converted to float.
      // If either is long, other is converted to long.
      // Otherwise, both are converted to int.
      Object literalValue = literal.getValue();
      switch (literal.getKind()) {
        case DOUBLE_LITERAL:
          double doubleValue = ((Double) literalValue).doubleValue();
          return doubleValue < minValue || doubleValue > maxValue;
        case FLOAT_LITERAL:
          float floatValue = ((Float) literalValue).floatValue();
View Full Code Here

        fix = SuggestedFix.postfixWith(tree.getLeftOperand(), "L");
      } else {
        fix = SuggestedFix.prefixWith(tree, "(long) ");
      }
    } else {
      JCLiteral jcLiteral = (JCLiteral) tree.getRightOperand();
      // This is the equivalent shift distance according to JLS 15.19.
      String actualShiftDistance = Integer.toString(intValue & 0x1f);
      int actualStart = ASTHelpers.getActualStartPosition(jcLiteral, state.getSourceCode());
      if (actualStart != jcLiteral.getStartPosition()) {
        fix = SuggestedFix.replace(tree.getRightOperand(), actualShiftDistance,
            actualStart - jcLiteral.getStartPosition(), 0);
      } else {
        fix = SuggestedFix.replace(tree.getRightOperand(), actualShiftDistance);
      }
    }
    return describeMatch(tree, fix);
View Full Code Here

  /**
   * Extracts the long literal corresponding to a given {@link LiteralTree} node from the source
   * code as a string. Returns null if the source code is not available.
   */
  private static String getLongLiteral(LiteralTree literalTree, VisitorState state) {
    JCLiteral longLiteral = (JCLiteral) literalTree;
    CharSequence sourceFile = state.getSourceCode();
    if (sourceFile == null) {
      return null;
    }
    int start = longLiteral.getStartPosition();
    java.util.regex.Matcher matcher = LONG_LITERAL_PATTERN.matcher(
        sourceFile.subSequence(start, sourceFile.length()));
    if (matcher.lookingAt()) {
      return matcher.group();
    }
View Full Code Here

      MethodInvocationTree tree, VisitorState state, FormatParameters parameters) {

    List<String> errors = new ArrayList<>();
    int formatIndex = parameters.getFormatIndex();
    List<? extends ExpressionTree> args = tree.getArguments();
    JCLiteral format = (JCLiteral) args.get(formatIndex);
    String formatString = (String) format.getValue();

    List<ExpressionTree> leadingArguments = new ArrayList<>();
    List<ExpressionTree> formatArguments = new ArrayList<>();

    for (int i = 0; i < args.size(); i++) {
View Full Code Here

  // Run the FindBugs checker on the string, to catch anything we don't currently detect.
  private void verifyPrintf(MethodInvocationTree tree, FormatParameters parameters)
      throws FormatFlagsConversionMismatchException, IllegalFormatException, FormatterException {
     List<? extends ExpressionTree> args = tree.getArguments();

    JCLiteral format = (JCLiteral) args.get(parameters.getFormatIndex());
    String formatString = (String) format.getValue();

    List<String> argTypes = new ArrayList<>();
    for (int i = parameters.getFormatIndex() + 1; i < args.size(); ++i) {
      Type type = ((JCExpression) args.get(i)).type;
      argTypes.add(getFormatterType(type));
View Full Code Here

                if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0)
                    tree.init = translate(tree.init);
                else {
                    String t = tree.vartype.toString();
                    if (t.equals("boolean"))
                        tree.init = new JCLiteral(TypeTag.BOOLEAN, 0) { };
                    else if (t.equals("byte"))
                        tree.init = new JCLiteral(TypeTag.BYTE, 0) { };
                    else if (t.equals("char"))
                        tree.init = new JCLiteral(TypeTag.CHAR, 0) { };
                    else if (t.equals("double"))
                        tree.init = new JCLiteral(TypeTag.DOUBLE, 0.d) { };
                    else if (t.equals("float"))
                        tree.init = new JCLiteral(TypeTag.FLOAT, 0.f) { };
                    else if (t.equals("int"))
                        tree.init = new JCLiteral(TypeTag.INT, 0) { };
                    else if (t.equals("long"))
                        tree.init = new JCLiteral(TypeTag.LONG, 0) { };
                    else if (t.equals("short"))
                        tree.init = new JCLiteral(TypeTag.SHORT, 0) { };
                    else
                        tree.init = new JCLiteral(TypeTag.BOT, null) { };
                }
            }
            result = tree;
        }
View Full Code Here

                if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0)
                    tree.init = translate(tree.init);
                else {
                    String t = tree.vartype.toString();
                    if (t.equals("boolean"))
                        tree.init = new JCLiteral(TypeTags.BOOLEAN, 0) { };
                    else if (t.equals("byte"))
                        tree.init = new JCLiteral(TypeTags.BYTE, 0) { };
                    else if (t.equals("char"))
                        tree.init = new JCLiteral(TypeTags.CHAR, 0) { };
                    else if (t.equals("double"))
                        tree.init = new JCLiteral(TypeTags.DOUBLE, 0.d) { };
                    else if (t.equals("float"))
                        tree.init = new JCLiteral(TypeTags.FLOAT, 0.f) { };
                    else if (t.equals("int"))
                        tree.init = new JCLiteral(TypeTags.INT, 0) { };
                    else if (t.equals("long"))
                        tree.init = new JCLiteral(TypeTags.LONG, 0) { };
                    else if (t.equals("short"))
                        tree.init = new JCLiteral(TypeTags.SHORT, 0) { };
                    else
                        tree.init = new JCLiteral(TypeTags.BOT, null) { };
                }
            }
            result = tree;
        }
View Full Code Here

    //
    // Literals
   

    JCExpression ceylonLiteral(String s) {
        JCLiteral lit = make().Literal(s);
        return lit;
    }
View Full Code Here

TOP

Related Classes of com.sun.tools.javac.tree.JCTree.JCLiteral

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.