1 19 20 package org.netbeans.modules.java.source.builder; 21 22 import com.sun.tools.javac.code.Symbol; 23 import com.sun.tools.javac.code.Symtab; 24 import com.sun.tools.javac.code.Type; 25 import com.sun.tools.javac.util.Context; 26 import com.sun.tools.javac.util.ListBuffer; 27 import com.sun.tools.javac.util.Name; 28 import java.util.List ; 29 import java.util.Set ; 30 import javax.lang.model.element.*; 31 import javax.lang.model.type.TypeMirror; 32 import org.netbeans.modules.java.source.builder.TreeFactory; 33 import org.netbeans.modules.java.source.engine.ElementMaker; 34 35 public class ElementFactory implements ElementMaker { 36 private Symtab symtab; 37 private Name.Table names; 38 39 private static final Context.Key<ElementFactory> KEY = 40 new Context.Key<ElementFactory>(); 41 42 public static ElementFactory instance(Context context) { 43 ElementFactory instance = context.get(KEY); 44 if (instance == null) { 45 instance = new ElementFactory(context); 46 context.put(KEY, instance); 47 } 48 return instance; 49 } 50 51 protected ElementFactory(Context context) { 52 symtab = Symtab.instance(context); 53 names = Name.Table.instance(context); 54 } 55 56 public ExecutableElement Executable(Set <Modifier> modifiers, 57 String simpleName, 58 List <? extends TypeMirror> argtypes, 59 TypeMirror restype, 60 List <? extends TypeMirror> thrownTypes, 61 TypeElement owner) { 62 Symbol.ClassSymbol tsym = (Symbol.ClassSymbol)owner; 63 ListBuffer<Type> args = new ListBuffer<Type>(); 64 for (TypeMirror t : argtypes) 65 args.append((Type)t); 66 ListBuffer<Type> thrown = new ListBuffer<Type>(); 67 for (TypeMirror t : thrownTypes) 68 thrown.append((Type)t); 69 Type.MethodType tp = new Type.MethodType(args.toList(), 70 (Type)restype, 71 thrown.toList(), 72 tsym); 73 long flags = TreeFactory.modifiersToFlags(modifiers); 74 Name name = names.fromString(simpleName); 75 Symbol.MethodSymbol sym = new Symbol.MethodSymbol(flags, name, tp, tsym); 76 tsym.members_field.enter(sym); 77 return sym; 78 } 79 80 public VariableElement Variable(Set <Modifier> modifiers, 81 String simpleName, 82 TypeMirror type, 83 Element owner) { 84 ListBuffer<Type> args = new ListBuffer<Type>(); 85 long flags = TreeFactory.modifiersToFlags(modifiers); 86 Name name = names.fromString(simpleName); 87 Symbol.VarSymbol sym = 88 new Symbol.VarSymbol(flags, name, (Type)type, (Symbol)owner); 89 if (owner instanceof Symbol.ClassSymbol) 90 ((Symbol.ClassSymbol)owner).members_field.enter(sym); 91 return sym; 92 } 93 } 94 | Popular Tags |