KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > parser > LangEnvImpl


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.parser;
21
22 import org.openide.nodes.Node;
23 import org.openide.src.*;
24
25 import java.util.WeakHashMap JavaDoc;
26
27 import org.netbeans.modules.java.bridge.BindingFactory;
28 import org.netbeans.modules.java.bridge.LangModel;
29 import org.netbeans.modules.java.bridge.WrapperFactory;
30 import org.netbeans.modules.java.codegen.DocumentBinding;
31
32 /**
33  * Standard stupid & simple implementation of the language model environment.
34  *
35  * @author sdedic
36  * @version
37  */

38 public class LangEnvImpl implements LangModel.Env {
39     WeakHashMap JavaDoc cookieMap;
40     DocumentBinding binding;
41     WrapperFactory wrapper;
42     LangModel model;
43     
44     public LangEnvImpl(DocumentBinding docBinding) {
45         this.binding = docBinding;
46         this.wrapper = DefaultWrapper.getInstance();
47         cookieMap = new WeakHashMap JavaDoc(57);
48     }
49     
50     public void setModel(LangModel model) {
51         this.model = model;
52     }
53
54     public BindingFactory getBindingFactory() {
55         return binding;
56     }
57     
58     public WrapperFactory getWrapperFactory() {
59         return wrapper;
60     }
61
62     /**
63      * Currently no-op implementation.
64      */

65     public void complete(Element scope, int informationKind) {
66     }
67     
68     /** The environment is called to resolve type name according to the context it
69      * is used in. The current implementation will return all primitives and
70      * array of primitives unchanged, but will delegate resolution of class names
71      * to the {@link #resolveTypeIdent}. If the resolved type identifier is different
72      * than the original, it reconstructs the type and returns it.
73     */

74     public Type resolveType(Element context, Type original) {
75         if (original.isPrimitive())
76             return original;
77         Type t = original;
78         int depth = 0;
79         while (t.isArray()) {
80             t = t.getElementType();
81             depth++;
82         }
83         if (t.isPrimitive())
84             return original;
85         
86         Identifier id = t.getTypeIdentifier();
87         Identifier resolved = resolveTypeIdent(context, id);
88         if (resolved == id)
89             return original;
90         
91         // build up the type again.
92
t = Type.createClass(resolved);
93         while (depth > 0) {
94             t = Type.createArray(t);
95             depth--;
96         }
97         return t;
98     }
99     
100     /**
101      * This is called to resolve/transform a name of a type for this context.
102      * Assume the context will only be ClassElement or SourceElement. This implementation
103      * does not attempt to resolve identifiers, it only marks them as
104      * {@link org.openide.src.Identifier#NOT_YET_RESOLVED} to distinguish them from
105      * resolved ones.
106     */

107     public Identifier resolveTypeIdent(Element context, Identifier original) {
108         if (model.isSameContext(context, original))
109             return original;
110         return model.createLocalIdentifier(context, original.getFullName(),
111             original.getSourceName(), Identifier.NOT_YET_RESOLVED);
112     }
113     
114     public Node.Cookie findCookie(Element el, Class JavaDoc requested) {
115         return null;
116     }
117 }
118
Popular Tags