1 19 package org.netbeans.mdr.handlers; 20 21 import javax.jmi.reflect.*; 22 23 import java.util.*; 24 import java.io.*; 25 import java.lang.reflect.*; 26 27 import org.netbeans.mdr.util.*; 28 import org.netbeans.mdr.storagemodel.*; 29 30 35 public abstract class StructImpl extends ImplClass implements RefStruct { 36 private final List fields; 37 private final Map values; 39 private final List qualifiedName; 41 42 private static Class getStructClass(MDRClassLoader loader, Class ifc) throws IllegalArgumentException { 43 check(loader, ifc); 44 Map cache = getLoaderCache(loader); 45 String className = getName(ifc); 46 Class result = getFromCache(cache, ifc, className); 47 48 if (result == null) { 49 try { 50 byte[] structClassFile = StructGenerator.generateStruct(className, ifc); 51 result = loader.defineClass(className, structClassFile); 52 } finally { 53 releaseCache(cache, result, className); 54 } 55 } 56 57 return result; 58 } 59 60 public static RefStruct newInstance(DatatypeDescriptor dataType, Object fieldValues[]) { 61 try { 62 HashMap values = new HashMap(fieldValues.length, 1); 63 int i = 0; 64 Iterator types = dataType.getMemberTypes().iterator(); 65 for (Iterator it = dataType.getMembers().iterator(); it.hasNext(); i++) { 66 Class type = (Class ) types.next(); 67 if (fieldValues[i] != null && !type.isInstance(fieldValues[i])) { 68 throw new TypeMismatchException(type, fieldValues[i], null); 69 } 70 values.put(it.next(), fieldValues[i]); 71 } 72 return newInstance(BaseObjectHandler.resolveInterface(dataType.getIfcName()), dataType.getMembers(), values, dataType.getTypeName()); 73 } catch (ClassNotFoundException e) { 74 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 75 } 76 } 77 78 public static RefStruct newInstance(Class ifc, List fields, Map values, List qualifiedName) { 79 MDRClassLoader loader = BaseObjectHandler.getDefaultClassLoader(); 80 Class cl = getStructClass(loader, ifc); 81 82 try { 83 Constructor cons = cl.getConstructor(new Class [] {List.class, Map.class, List.class}); 84 RefStruct struct = (RefStruct) cons.newInstance(new Object [] {fields, values, qualifiedName}); 85 86 return struct; 87 } catch (NoSuchMethodException e) { 88 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 89 } catch (IllegalAccessException e) { 90 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 91 } catch (InstantiationException e) { 92 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 93 } catch (InvocationTargetException e) { 94 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 95 } 96 } 97 98 99 protected StructImpl(List fields, Map values, List qualifiedName) { 100 this.values = values; 101 this.qualifiedName = Collections.unmodifiableList(qualifiedName); 102 this.fields = Collections.unmodifiableList(fields); 103 } 104 105 107 protected Object handleGet(String memberName) { 108 return refGetValue(memberName); 109 } 110 111 113 public Object refGetValue(String memberName) { 114 if (values.containsKey(memberName)) { 115 return values.get(memberName); 116 } else { 117 throw new DebugException("Wrong item name: " + memberName); 118 } 119 } 120 121 public List refFieldNames() { 122 return fields; 123 } 124 125 public List refTypeName() { 126 return this.qualifiedName; 127 } 128 129 public int hashCode() { 130 return qualifiedName.hashCode() * 37 + values.hashCode(); 131 } 132 133 public boolean equals(Object other) { 134 if ((other instanceof RefStruct) && ((RefStruct) other).refTypeName().equals(qualifiedName)) { 136 Object key; 137 Object value; 138 for (Iterator it = values.keySet().iterator(); it.hasNext();) { 140 key = it.next(); 141 value = values.get(key); 142 if (value == null) { 143 if (((RefStruct) other).refGetValue((String ) key) != null) 144 return false; 145 } else if (!value.equals(((RefStruct) other).refGetValue((String ) key))) 146 return false; 147 } 148 return true; 149 } else { 150 return false; 151 } 152 } 153 } 154 | Popular Tags |