Java Code Examples for com.sun.tools.javac.tree.JCTree#JCVariableDecl

The following examples show how to use com.sun.tools.javac.tree.JCTree#JCVariableDecl . 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: NewArrayPretty.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
void run(String code) throws IOException {
    String src = "public class Test {" + code + ";}";

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);

        if (!code.equals(var.toString())) {
            System.err.println("Expected: " + code);
            System.err.println("Obtained: " + var.toString());
            throw new RuntimeException("strings do not match!");
        }
    }
}
 
Example 2
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
private JCTree[] tempify( JCTree.JCBinary tree, TreeMaker make, JCExpression expr, Context ctx, Symbol owner, String varName )
{
  switch( expr.getTag() )
  {
    case LITERAL:
    case IDENT:
      return null;

    default:
      JCTree.JCVariableDecl tempVar = make.VarDef( make.Modifiers( FINAL | SYNTHETIC ),
        Names.instance( ctx ).fromString( varName + tempVarIndex ), make.Type( expr.type ), expr );
      tempVar.sym = new Symbol.VarSymbol( FINAL | SYNTHETIC, tempVar.name, expr.type, owner );
      tempVar.type = tempVar.sym.type;
      tempVar.pos = tree.pos;
      JCExpression ident = make.Ident( tempVar );
      ident.type = expr.type;
      ident.pos = tree.pos;
      return new JCTree[] {tempVar, ident};
  }
}
 
Example 3
Source File: CompleteWord.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void collectFromMethod(Editor editor, String incomplete, List<SuggestItem> result,
                               JCTree.JCMethodDecl method) {
    //add field from start position of method to the cursor
    List<JCTree.JCStatement> statements
            = method.getBody().getStatements();
    for (JCTree.JCStatement statement : statements) {
        if (statement instanceof JCTree.JCVariableDecl) {
            JCTree.JCVariableDecl field = (JCTree.JCVariableDecl) statement;
            addVariable(field, editor, incomplete, result);
        }
    }
    //add params
    List<JCTree.JCVariableDecl> parameters = method.getParameters();
    for (JCTree.JCVariableDecl parameter : parameters) {
        addVariable(parameter, editor, incomplete, result);
    }

}
 
Example 4
Source File: NewArrayPretty.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
void run(String code) throws IOException {
    String src = "public class Test {" + code + ";}";

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);

        if (!code.equals(var.toString())) {
            System.err.println("Expected: " + code);
            System.err.println("Obtained: " + var.toString());
            throw new RuntimeException("strings do not match!");
        }
    }
}
 
Example 5
Source File: NewArrayPretty.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
void run(String code) throws IOException {
    String src = "public class Test {" + code + ";}";

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);

        if (!code.equals(var.toString())) {
            System.err.println("Expected: " + code);
            System.err.println("Obtained: " + var.toString());
            throw new RuntimeException("strings do not match!");
        }
    }
}
 
Example 6
Source File: NewArrayPretty.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
void run(String code) throws IOException {
    String src = "public class Test {" + code + ";}";

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);

        if (!code.equals(var.toString())) {
            System.err.println("Expected: " + code);
            System.err.println("Obtained: " + var.toString());
            throw new RuntimeException("strings do not match!");
        }
    }
}
 
Example 7
Source File: NewArrayPretty.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void run(String code) throws IOException {
    String src = "public class Test {" + code + ";}";

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);

        if (!code.equals(var.toString())) {
            System.err.println("Expected: " + code);
            System.err.println("Obtained: " + var.toString());
            throw new RuntimeException("strings do not match!");
        }
    }
}
 
Example 8
Source File: VarScanner.java    From convalida with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVarDef(JCTree.JCVariableDecl jcVariableDecl) {
    if ("int".equals(jcVariableDecl.getType().toString())) {
        int id = Integer.valueOf(jcVariableDecl.getInitializer().toString());
        String resourceName = jcVariableDecl.getName().toString();
        QualifiedId qualifiedId = new QualifiedId(respectivePackageName, id);
        ids.put(qualifiedId, new Id(id, className, resourceName));
    }
}
 
Example 9
Source File: SourceAnalyzerFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@CheckForNull
public Void visitVariable(@NonNull final VariableTree node, @NonNull final Map<Pair<BinaryName, String>, UsagesData<String>> p) {
    Symbol s = ((JCTree.JCVariableDecl)node).sym;
    if (s != null && s.owner != null && (s.owner.getKind().isClass() || s.owner.getKind().isInterface())) {
        addIdent(activeClass.peek(), node.getName(), p, true);
    }
    return super.visitVariable(node, p);
}
 
Example 10
Source File: TypeAnnotationsPretty.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void runField(String code) throws IOException {
    String src = prefix +
            code + "; }" +
            postfix;

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
        checkMatch(code, var);
    }
}
 
Example 11
Source File: GenerateGetMethodProcessor.java    From java-master with Apache License 2.0 5 votes vote down vote up
private JCTree.JCMethodDecl createGetterMethodDecl(JCTree.JCVariableDecl jcVariableDecl) {

        ListBuffer<JCTree.JCStatement> statements = new ListBuffer<>();
        statements.append(treeMaker.Return(treeMaker.Select(treeMaker.Ident(names.fromString("this")), jcVariableDecl.getName())));
        JCTree.JCBlock body = treeMaker.Block(0, statements.toList());
        return treeMaker.MethodDef(treeMaker.Modifiers(Flags.PUBLIC), getNewMethodName(jcVariableDecl.getName()),
                jcVariableDecl.vartype, List.nil(), List.nil(), List.nil(), body, null);
    }
 
Example 12
Source File: TypeAnnotationsPretty.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void runField(String code) throws IOException {
    String src = prefix +
            code + "; }" +
            postfix;

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
        checkMatch(code, var);
    }
}
 
Example 13
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
private static void analyzeTry(SourceContext context, JCTree.JCTry tryExpr) throws IOException {
  JCTree.JCBlock block = tryExpr.getBlock();
  List<JCTree> resources = tryExpr.getResources();
  List<JCTree.JCCatch> catches = tryExpr.getCatches();
  JCTree.JCBlock lyBlock = tryExpr.getFinallyBlock();

  if (nonNull(resources)) {
    for (JCTree resource : resources) {
      analyzeParsedTree(context, resource);
    }
  }

  analyzeParsedTree(context, block);

  if (nonNull(catches)) {

    for (JCTree.JCCatch jcCatch : catches) {

      JCTree.JCVariableDecl parameter = jcCatch.getParameter();
      analyzeParsedTree(context, parameter);

      JCTree.JCBlock catchBlock = jcCatch.getBlock();
      analyzeParsedTree(context, catchBlock);
    }
  }

  analyzeParsedTree(context, lyBlock);
}
 
Example 14
Source File: JavaParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void addMethod(@NonNull JCCompilationUnit unit,
                       @NonNull ClassDescription clazz,
                       @NonNull JCTree.JCMethodDecl member) {


    final String methodName = member.getName().toString();
    final int modifiers = JavaUtil.toJavaModifiers(member.getModifiers().getFlags());

    final List<IClass> methodParameters = new ArrayList<>();
    List<JCTree.JCVariableDecl> parameters = member.getParameters();
    for (JCTree.JCVariableDecl parameter : parameters) {
        JCTree type = parameter.getType();
        IClass paramType = JavaUtil.jcTypeToClass(unit, type);
        methodParameters.add(paramType);
    }

    IClass returnType = JavaUtil.jcTypeToClass(unit, member.getReturnType());

    if (member.getName().toString().equals(CONSTRUCTOR_NAME)) {
        ConstructorDescription constructor = new ConstructorDescription(
                clazz.getFullClassName(),
                methodParameters);
        clazz.addConstructor(constructor);
    } else {
        MethodDescription methodDescription = new MethodDescription(
                methodName,
                modifiers,
                methodParameters,
                returnType);
        clazz.addMethod(methodDescription);
    }
}
 
Example 15
Source File: TypeAnnotationsPretty.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void runField(String code) throws IOException {
    String src = prefix +
            code + "; }" +
            postfix;

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
        checkMatch(code, var);
    }
}
 
Example 16
Source File: TypeAnnotationsPretty.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void runField(String code) throws IOException {
    String src = prefix +
            code + "; }" +
            postfix;

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
        checkMatch(code, var);
    }
}
 
Example 17
Source File: ModelBuilder.java    From vertx-codetrans with Apache License 2.0 5 votes vote down vote up
@Override
public StatementModel visitVariable(VariableTree node, VisitContext context) {
  JCTree.JCVariableDecl decl = (JCTree.JCVariableDecl) node;
  ExpressionModel initializer;
  if (node.getInitializer() != null) {
    initializer = scan(node.getInitializer(), context);
  } else {
    initializer = null;
  }
  TypeInfo type = factory.create(decl.type);
  ElementKind kind = decl.sym.getKind();
  VariableScope scope = resolvescope(context, kind, decl.getName().toString());
  return context.builder.variableDecl(scope, type, decl.name.toString(), initializer);
}
 
Example 18
Source File: TypeAnnotationsPretty.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void runField(String code) throws IOException {
    String src = prefix +
            code + "; }" +
            postfix;

    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
            null, Arrays.asList(new MyFileObject(src)));

    for (CompilationUnitTree cut : ct.parse()) {
        JCTree.JCVariableDecl var =
                (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
        checkMatch(code, var);
    }
}
 
Example 19
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
/**
 * Erase all structural interface type literals to Object
 */
@Override
public void visitSelect( JCTree.JCFieldAccess tree )
{
  super.visitSelect( tree );

  if( _tp.isGenerate() && !shouldProcessForGeneration() )
  {
    // Don't process tree during GENERATE, unless the tree was generated e.g., a bridge method
    return;
  }

  if( TypeUtil.isStructuralInterface( _tp, tree.sym ) && !isReceiver( tree ) )
  {
    Symbol.ClassSymbol objectSym = getObjectClass();
    JCTree.JCIdent objIdent = _tp.getTreeMaker().Ident( objectSym );
    Tree parent = _tp.getParent( tree );
    if( parent instanceof JCTree.JCVariableDecl )
    {
      ((JCTree.JCVariableDecl)parent).type = objectSym.type;
      long parameterModifier = 8589934592L; // Flag.Flag.PARAMETER.value
      if( (((JCTree.JCVariableDecl)parent).mods.flags & parameterModifier) != 0 )
      {
        objIdent.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).sym.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).vartype = objIdent;
      }
    }
    else if( parent instanceof JCTree.JCWildcard )
    {
      JCTree.JCWildcard wildcard = (JCTree.JCWildcard)parent;
      wildcard.type = new Type.WildcardType( objectSym.type, wildcard.kind.kind, wildcard.type.tsym );
    }
    result = objIdent;
  }
  else if( isJailbreakReceiver( tree ) )
  {
    result = replaceWithReflection( tree );
  }
  else
  {
    result = tree;
  }
}
 
Example 20
Source File: JavaParser.java    From manifold with Apache License 2.0 4 votes vote down vote up
public JCTree.JCExpression parseExpr( String expr, DiagnosticCollector<JavaFileObject> errorHandler )
{
  //!! Do not init() here; do not use _javac or _mfm for parseExpr() since this method is generally used
  //!! during the Parse phase, which happens earlier than the Enter phase where the plugin initializes much
  //!! of its state i.e., if parseExpr() is called before an Enter phase, it could use Manifold prematurely
  //!! and screw the pooch.  Also, we don't need Manifold for parsing expressions since it only produces a
  //!! simple AST with nothing resolved.
  // init();

  ArrayList<JavaFileObject> javaStringObjects = new ArrayList<>();
  String src =
    "class Sample {\n" +
    "  Object foo = " + expr + ";\n" +
    "}\n";
  javaStringObjects.add( new StringJavaFileObject( "sample", src ) );
  StringWriter errors = new StringWriter();
  BasicJavacTask javacTask = (BasicJavacTask)Objects.requireNonNull( _parserJavac.get() )
    .getTask( errors, null, errorHandler, Collections.singletonList( "-proc:none" ), null, javaStringObjects );
  try
  {
    initTypeProcessing( javacTask, Collections.singleton( "sample" ) );
    Iterable<? extends CompilationUnitTree> iterable = javacTask.parse();
    if( errors.getBuffer().length() > 0 )
    {
      System.err.println( errors.getBuffer() );
    }
    for( CompilationUnitTree x : iterable )
    {
      List<? extends Tree> typeDecls = x.getTypeDecls();
      if( !typeDecls.isEmpty() )
      {
        JCTree.JCClassDecl tree = (JCTree.JCClassDecl)typeDecls.get( 0 );
        JCTree.JCVariableDecl field = (JCTree.JCVariableDecl)tree.getMembers().get( 0 );
        return field.getInitializer();
      }
    }
    return null;
  }
  catch( Exception e )
  {
    return null;
  }
}