Java Code Examples for com.sun.tools.javac.tree.JCTree#getKind()

The following examples show how to use com.sun.tools.javac.tree.JCTree#getKind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 7 votes vote down vote up
private Block convertLambdaBody(JCTree lambdaBody, TypeDescriptor returnTypeDescriptor) {
  Block body;
  if (lambdaBody.getKind() == Kind.BLOCK) {
    body = convertBlock((JCBlock) lambdaBody);
  } else {
    checkArgument(lambdaBody instanceof JCExpression);
    Expression lambdaMethodBody = convertExpression((JCExpression) lambdaBody);
    Statement statement =
        AstUtils.createReturnOrExpressionStatement(
            getSourcePosition(lambdaBody), lambdaMethodBody, returnTypeDescriptor);
    body =
        Block.newBuilder()
            .setSourcePosition(getSourcePosition(lambdaBody))
            .setStatements(statement)
            .build();
  }
  return body;
}
 
Example 2
Source File: VeryPretty.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isSynthetic(JCTree tree) {
       if (tree.getKind() == Kind.METHOD) {
           //filter synthetic constructors
           return (((JCMethodDecl)tree).mods.flags & Flags.GENERATEDCONSTR) != 0L;
       }
       //filter synthetic superconstructor calls
       if (tree.getKind() == Kind.EXPRESSION_STATEMENT && diffContext.origUnit != null) {
           JCExpressionStatement est = (JCExpressionStatement) tree;
           if (est.expr.getKind() == Kind.METHOD_INVOCATION) {
               JCMethodInvocation mit = (JCMethodInvocation) est.getExpression();
               if (mit.meth.getKind() == Kind.IDENTIFIER) {
                   JCIdent it = (JCIdent) mit.getMethodSelect();
                   return it.name == names._super && diffContext.syntheticTrees.contains(tree);
               }
           }
       }
return false;
   }
 
Example 3
Source File: TypeAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 4
Source File: TypeAnnotations.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 5
Source File: TypeAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 6
Source File: TypeAnnotations.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 7
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private SourcePosition getNamePosition(String name, JCTree node) {
  int start = node.getPreferredPosition();
  if (start == -1) {
    return null;
  }

  try {
    String src = javacUnit.sourcefile.getCharContent(true).toString();
    Kind kind = node.getKind();
    if (kind == Kind.ANNOTATION_TYPE
        || kind == Kind.CLASS
        || kind == Kind.ENUM
        || kind == Kind.INTERFACE) {
      // Skip the class/enum/interface token.
      while (src.charAt(start++) != ' ') {}
    } else if (kind != Kind.METHOD && kind != Kind.VARIABLE) {
      return getSourcePosition(node);
    }
    if (!Character.isJavaIdentifierStart(src.charAt(start))) {
      return getSourcePosition(node);
    }
    int endPos = start + 1;
    while (Character.isJavaIdentifierPart(src.charAt(endPos))) {
      endPos++;
    }
    return getSourcePosition(name, start, endPos);
  } catch (IOException e) {
    throw internalCompilerError(e, "Error getting name Position for: %s.", node);
  }
}
 
Example 8
Source File: TypeAnnotations.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 9
Source File: TypeAnnotations.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 10
Source File: TypeAnnotations.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
    if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
        return (JCArrayTypeTree) typetree;
    } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
        return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
    } else {
        Assert.error("Could not determine array type from type tree: " + typetree);
        return null;
    }
}
 
Example 11
Source File: TreeFinder.java    From annotation-tools with MIT License 5 votes vote down vote up
private JCTree arrayContentType(JCArrayTypeTree att) {
  JCTree node = att;
  do {
    node = ((JCArrayTypeTree) node).getType();
  } while (node.getKind() == Tree.Kind.ARRAY_TYPE);
  return node;
}
 
Example 12
Source File: Insertions.java    From annotation-tools with MIT License 5 votes vote down vote up
static TypeTree fromJCTree(JCTree jt) {
  if (jt != null) {
    Kind kind = jt.getKind();
    switch (kind) {
    case ANNOTATED_TYPE:
      return fromJCTree(
          ((JCTree.JCAnnotatedType) jt).getUnderlyingType());
    case IDENTIFIER:
      return new IdentifierTT(
          ((JCTree.JCIdent) jt).sym.getSimpleName().toString());
    case ARRAY_TYPE:
      return new ArrayTT(
          fromJCTree(((JCTree.JCArrayTypeTree) jt).getType()));
    case MEMBER_SELECT:
      return new MemberSelectTT(
          fromJCTree(((JCTree.JCFieldAccess) jt).getExpression()),
          ((JCTree.JCFieldAccess) jt).getIdentifier());
    case EXTENDS_WILDCARD:
    case SUPER_WILDCARD:
      return new WildcardTT(kind,
          fromJCTree(((JCTree.JCWildcard) jt).getBound()));
    case UNBOUNDED_WILDCARD:
      return new WildcardTT();
    case PARAMETERIZED_TYPE:
      com.sun.tools.javac.util.List<JCExpression> typeArgs =
        ((JCTree.JCTypeApply) jt).getTypeArguments();
      List<Tree> args = new ArrayList<>(typeArgs.size());
      for (JCTree.JCExpression typeArg : typeArgs) {
        args.add(fromJCTree(typeArg));
      }
      return new ParameterizedTypeTT(
          fromJCTree(((JCTree.JCTypeApply) jt).getType()),
          args);
    default:
      break;
    }
  }
  return null;
}
 
Example 13
Source File: TreePruner.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true iff the given tree could be the type name of a constant type.
 *
 * <p>This is a conservative over-approximation: an identifier named {@code String}
 * isn't necessarily a type name, but this is used at parse-time before types have
 * been attributed.
 */
private static boolean constantType(JCTree tree) {
  switch (tree.getKind()) {
    case PRIMITIVE_TYPE:
      return true;
    case IDENTIFIER:
      return tree.toString().contentEquals("String");
    case MEMBER_SELECT:
      return tree.toString().contentEquals("java.lang.String");
    default:
      return false;
  }
}
 
Example 14
Source File: VeryPretty.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void blankLines(JCTree tree, boolean before) {
    if (tree == null) {
        return;
    }
    int n = 0;
    // NOTE - out.blankLines() may truncate a previous line, iff it contains trailing whitespace.
    switch (tree.getKind()) {
        case ANNOTATION_TYPE:
        case CLASS:
        case ENUM:
        case INTERFACE:
            n = before ? cs.getBlankLinesBeforeClass() : cs.getBlankLinesAfterClass();
    	if (((JCClassDecl) tree).defs.nonEmpty() && !before) {
                n = 0;
            } else {
                out.blanklines(n);
                toLeftMargin();
            }
            return;
        case METHOD: // do not handle for sythetic things
    	if ((((JCMethodDecl) tree).mods.flags & Flags.SYNTHETIC) == 0 &&
                ((JCMethodDecl) tree).name != names.init ||
                enclClass != null)
            {
                n = before
                        ? isFirst(tree, enclClass.defs)
                        ? enclClass.name == names.empty
                        ? cs.getBlankLinesAfterAnonymousClassHeader()
                        : cs.getBlankLinesAfterClassHeader()
                        : cs.getBlankLinesBeforeMethods()
                        : isLast(tree, enclClass.defs)
                        ? enclClass.name == names.empty
                        ? cs.getBlankLinesBeforeAnonymousClassClosingBrace()
                        : cs.getBlankLinesBeforeClassClosingBrace()
                        : cs.getBlankLinesAfterMethods();
                out.blanklines(n);
    	    toLeftMargin();
            }
            return;
        case VARIABLE: // just for the fields
            if (enclClass != null && enclClass.name != names.empty && (((JCVariableDecl) tree).mods.flags & ENUM) == 0) {
                n = before
                        ? isFirst(tree, enclClass.defs)
                        ? enclClass.name == names.empty
                        ? cs.getBlankLinesAfterAnonymousClassHeader()
                        : cs.getBlankLinesAfterClassHeader()
                        : cs.getBlankLinesBeforeFields()
                        : isLast(tree, enclClass.defs)
                        ? enclClass.name == names.empty
                        ? cs.getBlankLinesBeforeAnonymousClassClosingBrace()
                        : cs.getBlankLinesBeforeClassClosingBrace()
                        : cs.getBlankLinesAfterFields();
                out.blanklines(n);
                if (before) toLeftMargin();
            }
            return;
    }
}