KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > NameRef


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.javacore.parser;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import org.netbeans.mdr.storagemodel.StorableBaseObject;
26 import org.netbeans.mdr.util.IOUtils;
27 import org.openide.util.Utilities;
28
29 public class NameRef extends TypeParamRef {
30     public final TypeParamRef parent;
31     public final TypeRef[] args;
32
33     public static final NameRef java_lang_Object = new NameRef("java.lang.Object"); // NOI18N
34
public static final NameRef java_lang_Enum = new NameRef("java.lang.Enum"); // NOI18N
35
public static final NameRef java_lang_Annotation = new NameRef("java.lang.annotation.Annotation"); // NOI18N
36

37     private int hashCode;
38
39     public static Object JavaDoc read(InputStream JavaDoc stream, StorableBaseObject storable) throws IOException JavaDoc {
40         TypeParamRef parent = (TypeParamRef) IOUtils.read(stream, storable);
41         int len = IOUtils.readInt(stream);
42         TypeRef[] args;
43         if (len == 0) {
44             args = ElementInfo.EMPTY_TYPEREFS;
45         } else {
46             args = new TypeRef[len];
47         }
48         for (int i = 0; i < args.length; i++) {
49             args[i] = (TypeRef) IOUtils.read(stream, storable);
50         }
51         return new NameRef(stream, parent, args);
52     }
53     
54     public void write(OutputStream JavaDoc outputStream, StorableBaseObject storable) throws IOException JavaDoc {
55         IOUtils.write(outputStream, parent, storable);
56         IOUtils.writeInt(outputStream, args.length);
57         for (int i = 0; i < args.length; i++) {
58             IOUtils.write(outputStream, args[i], storable);
59         }
60         super.write(outputStream, storable);
61     }
62     
63     private NameRef(InputStream JavaDoc stream, TypeParamRef parent, TypeRef[] args) throws IOException JavaDoc {
64         super(IOUtils.readString(stream));
65         this.parent = parent;
66         this.args = args;
67     }
68     
69     public NameRef(String JavaDoc name) {
70         this(name,null,null);
71     }
72     
73     public NameRef(String JavaDoc n,TypeParamRef par,TypeRef[] args) {
74         super(getName(n, par, args));
75         if (name!=n)
76             parent=null;
77         else
78             parent=par;
79         this.args = args==null?ElementInfo.EMPTY_TYPEREFS:args;
80     }
81     
82     private static String JavaDoc getName(String JavaDoc name,TypeParamRef parent,TypeRef[] args) {
83         String JavaDoc newName=name;
84         
85         if (parent!=null && parent instanceof NameRef) {
86             NameRef parentName=(NameRef)parent;
87             
88             if (parentName.parent==null && parentName.args.length==0)
89                 newName=parentName.name.concat(".").concat(name); // NOI18N
90
}
91         return newName;
92     }
93
94     public boolean equals(Object JavaDoc typeRef) {
95         NameRef ref;
96         
97         if (this==typeRef)
98             return true;
99         if (!(typeRef instanceof NameRef))
100             return false;
101         ref=(NameRef)typeRef;
102         if (!name.equals(ref.name))
103             return false;
104         if (!Utilities.compareObjects(parent,ref.parent))
105             return false;
106         return Utilities.compareObjects(args,ref.args);
107     }
108     
109     String JavaDoc getName() {
110         String JavaDoc parentString="";
111         
112         if (parent!=null) {
113             parentString=parent.getName().concat("."); // NOI18N
114
}
115         return parentString.concat(name);
116     }
117     
118     public int hashCode() {
119         if (hashCode!=0) {
120             hashCode=name.hashCode();
121         
122             if (parent!=null)
123                 hashCode^=parent.hashCode();
124             hashCode^=args.hashCode();
125         }
126         return hashCode;
127     }
128 }
129
Popular Tags