langtools/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java
changeset 22163 3651128c74eb
parent 22154 3c8d86bf756b
child 24069 dfb8f11542fc
equal deleted inserted replaced
22162:3b3e23e67329 22163:3651128c74eb
    45  *  deletion without notice.</b>
    45  *  deletion without notice.</b>
    46  */
    46  */
    47 public class TreeMaker implements JCTree.Factory {
    47 public class TreeMaker implements JCTree.Factory {
    48 
    48 
    49     /** The context key for the tree factory. */
    49     /** The context key for the tree factory. */
    50     protected static final Context.Key<TreeMaker> treeMakerKey =
    50     protected static final Context.Key<TreeMaker> treeMakerKey = new Context.Key<>();
    51         new Context.Key<TreeMaker>();
       
    52 
    51 
    53     /** Get the TreeMaker instance. */
    52     /** Get the TreeMaker instance. */
    54     public static TreeMaker instance(Context context) {
    53     public static TreeMaker instance(Context context) {
    55         TreeMaker instance = context.get(treeMakerKey);
    54         TreeMaker instance = context.get(treeMakerKey);
    56         if (instance == null)
    55         if (instance == null)
   607 
   606 
   608     /** Create a list of identifiers referring to the variables declared
   607     /** Create a list of identifiers referring to the variables declared
   609      *  in given list of variable declarations.
   608      *  in given list of variable declarations.
   610      */
   609      */
   611     public List<JCExpression> Idents(List<JCVariableDecl> params) {
   610     public List<JCExpression> Idents(List<JCVariableDecl> params) {
   612         ListBuffer<JCExpression> ids = new ListBuffer<JCExpression>();
   611         ListBuffer<JCExpression> ids = new ListBuffer<>();
   613         for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail)
   612         for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail)
   614             ids.append(Ident(l.head));
   613             ids.append(Ident(l.head));
   615         return ids.toList();
   614         return ids.toList();
   616     }
   615     }
   617 
   616 
   708     }
   707     }
   709 
   708 
   710     /** Create a list of trees representing given list of types.
   709     /** Create a list of trees representing given list of types.
   711      */
   710      */
   712     public List<JCExpression> Types(List<Type> ts) {
   711     public List<JCExpression> Types(List<Type> ts) {
   713         ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>();
   712         ListBuffer<JCExpression> lb = new ListBuffer<>();
   714         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   713         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   715             lb.append(Type(l.head));
   714             lb.append(Type(l.head));
   716         return lb.toList();
   715         return lb.toList();
   717     }
   716     }
   718 
   717 
   731 
   730 
   732     /** Create annotation trees from annotations.
   731     /** Create annotation trees from annotations.
   733      */
   732      */
   734     public List<JCAnnotation> Annotations(List<Attribute.Compound> attributes) {
   733     public List<JCAnnotation> Annotations(List<Attribute.Compound> attributes) {
   735         if (attributes == null) return List.nil();
   734         if (attributes == null) return List.nil();
   736         ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
   735         ListBuffer<JCAnnotation> result = new ListBuffer<>();
   737         for (List<Attribute.Compound> i = attributes; i.nonEmpty(); i=i.tail) {
   736         for (List<Attribute.Compound> i = attributes; i.nonEmpty(); i=i.tail) {
   738             Attribute a = i.head;
   737             Attribute a = i.head;
   739             result.append(Annotation(a));
   738             result.append(Annotation(a));
   740         }
   739         }
   741         return result.toList();
   740         return result.toList();
   798             } else {
   797             } else {
   799                 result = visitCompoundInternal(compound);
   798                 result = visitCompoundInternal(compound);
   800             }
   799             }
   801         }
   800         }
   802         public JCAnnotation visitCompoundInternal(Attribute.Compound compound) {
   801         public JCAnnotation visitCompoundInternal(Attribute.Compound compound) {
   803             ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
   802             ListBuffer<JCExpression> args = new ListBuffer<>();
   804             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
   803             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
   805                 Pair<MethodSymbol,Attribute> pair = values.head;
   804                 Pair<MethodSymbol,Attribute> pair = values.head;
   806                 JCExpression valueTree = translate(pair.snd);
   805                 JCExpression valueTree = translate(pair.snd);
   807                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
   806                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
   808             }
   807             }
   809             return Annotation(Type(compound.type), args.toList());
   808             return Annotation(Type(compound.type), args.toList());
   810         }
   809         }
   811         public JCAnnotation visitTypeCompoundInternal(Attribute.TypeCompound compound) {
   810         public JCAnnotation visitTypeCompoundInternal(Attribute.TypeCompound compound) {
   812             ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
   811             ListBuffer<JCExpression> args = new ListBuffer<>();
   813             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
   812             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
   814                 Pair<MethodSymbol,Attribute> pair = values.head;
   813                 Pair<MethodSymbol,Attribute> pair = values.head;
   815                 JCExpression valueTree = translate(pair.snd);
   814                 JCExpression valueTree = translate(pair.snd);
   816                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
   815                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
   817             }
   816             }
   818             return TypeAnnotation(Type(compound.type), args.toList());
   817             return TypeAnnotation(Type(compound.type), args.toList());
   819         }
   818         }
   820         public void visitArray(Attribute.Array array) {
   819         public void visitArray(Attribute.Array array) {
   821             ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
   820             ListBuffer<JCExpression> elems = new ListBuffer<>();
   822             for (int i = 0; i < array.values.length; i++)
   821             for (int i = 0; i < array.values.length; i++)
   823                 elems.append(translate(array.values[i]));
   822                 elems.append(translate(array.values[i]));
   824             result = NewArray(null, List.<JCExpression>nil(), elems.toList()).setType(array.type);
   823             result = NewArray(null, List.<JCExpression>nil(), elems.toList()).setType(array.type);
   825         }
   824         }
   826         JCExpression translate(Attribute a) {
   825         JCExpression translate(Attribute a) {
   879     }
   878     }
   880 
   879 
   881     /** Create a list of type parameter trees from a list of type variables.
   880     /** Create a list of type parameter trees from a list of type variables.
   882      */
   881      */
   883     public List<JCTypeParameter> TypeParams(List<Type> typarams) {
   882     public List<JCTypeParameter> TypeParams(List<Type> typarams) {
   884         ListBuffer<JCTypeParameter> tparams = new ListBuffer<JCTypeParameter>();
   883         ListBuffer<JCTypeParameter> tparams = new ListBuffer<>();
   885         for (List<Type> l = typarams; l.nonEmpty(); l = l.tail)
   884         for (List<Type> l = typarams; l.nonEmpty(); l = l.tail)
   886             tparams.append(TypeParam(l.head.tsym.name, (TypeVar)l.head));
   885             tparams.append(TypeParam(l.head.tsym.name, (TypeVar)l.head));
   887         return tparams.toList();
   886         return tparams.toList();
   888     }
   887     }
   889 
   888 
   895 
   894 
   896     /** Create a a list of value parameter trees x0, ..., xn from a list of
   895     /** Create a a list of value parameter trees x0, ..., xn from a list of
   897      *  their types and an their owner.
   896      *  their types and an their owner.
   898      */
   897      */
   899     public List<JCVariableDecl> Params(List<Type> argtypes, Symbol owner) {
   898     public List<JCVariableDecl> Params(List<Type> argtypes, Symbol owner) {
   900         ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
   899         ListBuffer<JCVariableDecl> params = new ListBuffer<>();
   901         MethodSymbol mth = (owner.kind == MTH) ? ((MethodSymbol)owner) : null;
   900         MethodSymbol mth = (owner.kind == MTH) ? ((MethodSymbol)owner) : null;
   902         if (mth != null && mth.params != null && argtypes.length() == mth.params.length()) {
   901         if (mth != null && mth.params != null && argtypes.length() == mth.params.length()) {
   903             for (VarSymbol param : ((MethodSymbol)owner).params)
   902             for (VarSymbol param : ((MethodSymbol)owner).params)
   904                 params.append(VarDef(param, null));
   903                 params.append(VarDef(param, null));
   905         } else {
   904         } else {