KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.modules.java.bridge.BindingFactory;
26 import org.netbeans.modules.java.bridge.LangModel;
27 import org.netbeans.modules.java.bridge.WrapperFactory;
28 import org.netbeans.modules.java.codegen.DocumentBinding;
29
30 /**
31  *
32  * @author sdedic
33  * @version
34  */

35 public class LangEnvironmentImpl implements LangModel.Env {
36     DocumentBinding binding;
37     WrapperFactory wrapper;
38     LangModel model;
39
40     public LangEnvironmentImpl(DocumentBinding docBinding) {
41         this.binding = docBinding;
42         this.wrapper = DefaultWrapper.getInstance();
43     }
44     
45     /**
46      * Finishes creation of the environment's impl -- since it needs to communicate
47      * with its model.
48      */

49     public void setModel(LangModel model) {
50         this.model = model;
51     }
52
53     /**
54      * Returns the factory for Binding object for individual model elements.
55      */

56     public BindingFactory getBindingFactory() {
57         return binding;
58     }
59     
60     /**
61      * Returns the factory that wraps individual elements of the model.
62      */

63     public WrapperFactory getWrapperFactory() {
64         return wrapper;
65     }
66
67     /**
68      * Currently no-op implementation.
69      */

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

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

111     public Identifier resolveTypeIdent(Element context, Identifier original) {
112         if (model.isSameContext(context, original))
113             return original;
114         return model.createLocalIdentifier(context, original.getFullName(),
115             original.getSourceName(), Identifier.NOT_YET_RESOLVED);
116     }
117     
118     public Node.Cookie findCookie(Element el, Class JavaDoc c) {
119         return null;
120     }
121 }
122
Popular Tags