1 19 20 package org.netbeans.modules.javacore.parser; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 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"); public static final NameRef java_lang_Enum = new NameRef("java.lang.Enum"); public static final NameRef java_lang_Annotation = new NameRef("java.lang.annotation.Annotation"); 37 private int hashCode; 38 39 public static Object read(InputStream stream, StorableBaseObject storable) throws IOException { 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 outputStream, StorableBaseObject storable) throws IOException { 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 stream, TypeParamRef parent, TypeRef[] args) throws IOException { 64 super(IOUtils.readString(stream)); 65 this.parent = parent; 66 this.args = args; 67 } 68 69 public NameRef(String name) { 70 this(name,null,null); 71 } 72 73 public NameRef(String 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 getName(String name,TypeParamRef parent,TypeRef[] args) { 83 String 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); } 91 return newName; 92 } 93 94 public boolean equals(Object 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 getName() { 110 String parentString=""; 111 112 if (parent!=null) { 113 parentString=parent.getName().concat("."); } 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 |