KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > ElementFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
29 import java.util.Set JavaDoc;
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 JavaDoc<Modifier> modifiers,
57                                         String JavaDoc simpleName,
58                                         List JavaDoc<? extends TypeMirror> argtypes,
59                                         TypeMirror restype,
60                                         List JavaDoc<? 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 JavaDoc<Modifier> modifiers,
81                                     String JavaDoc 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