1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 23 import javax.jmi.reflect.ConstraintViolationException; 24 import javax.jmi.reflect.RefObject; 25 26 import org.netbeans.jmi.javamodel.*; 27 import org.netbeans.mdr.handlers.BaseObjectHandler; 28 import org.netbeans.mdr.handlers.ClassProxyHandler; 29 import org.netbeans.mdr.persistence.MOFID; 30 import org.netbeans.mdr.persistence.StorageException; 31 import org.netbeans.mdr.storagemodel.StorableBaseObject; 32 import org.netbeans.mdr.storagemodel.StorableClass; 33 import org.netbeans.mdr.util.DebugException; 34 35 39 public abstract class ParameterizedTypeClassImpl extends ClassProxyHandler implements ParameterizedTypeClass { 40 private static final String MOFID_PREFIX = "parameterizedType:"; private static final InstanceMap allInstances = new InstanceMap(); 42 private static int increment = 0; 43 44 public ParameterizedTypeClassImpl(StorableClass s) { 45 super(s); 46 } 47 48 public ParameterizedType createParameterizedType() { 49 throw new ConstraintViolationException(this, refMetaObject(), "Cannot create instance of ParameterizedType directly - use resolveParameterizedType instead."); } 51 52 public ParameterizedType createParameterizedType(String name, List annotations, int modifiers, String javadocText, JavaDoc javadoc, List contents, MultipartId superClassName, List interfaceNames, List typeParameters) { 53 throw new ConstraintViolationException(this, refMetaObject(), "Cannot create instance of ParameterizedType directly - use resolveParameterizedType instead."); } 55 56 public ParameterizedType resolveParameterizedType(JavaClass def, List params, ParameterizedType outerCls) { 57 if (def == null) 58 return null; 59 if (def instanceof ParameterizedTypeImpl) { 60 outerCls = ((ParameterizedTypeImpl) def).outerCls; 61 def = ((ParameterizedTypeImpl) def).definition; 62 } 63 if (params == null) { 64 params = Collections.EMPTY_LIST; 65 } 66 67 _lock(false); 68 try { 69 CacheKey fullName = constructKey(def, params, outerCls); 70 ParameterizedTypeImpl result = (ParameterizedTypeImpl) allInstances.get(fullName); 71 if (result == null) { 72 try { 73 StorableBaseObject s = _getDelegate(); 74 MOFID mofId = new MOFID(increment, MOFID_PREFIX + increment++); 75 DeferredObject o = new DeferredObject(mofId, s.getMdrStorage(), s.getImmediatePackageId(), s.getOutermostPackageId(), s.getMetaObject(), (StorableClass) s, null); 76 result = (ParameterizedTypeImpl) _getRepository().getHandler(o); 77 result.definition = def; 78 result.parameters = params; 79 result.outerCls = outerCls; 80 allInstances.put(fullName, result); 81 } catch (StorageException e) { 82 throw new DebugException(); 83 } 84 } 85 return result; 86 } finally { 87 _unlock(); 88 } 89 } 90 91 private CacheKey constructKey(JavaClass def, List params, ParameterizedType outerCls) { 92 MOFID[] paramIds = new MOFID[params.size()]; 93 int i = 0; 94 for (Iterator it = params.iterator(); it.hasNext(); i++) { 95 BaseObjectHandler param = (BaseObjectHandler) it.next(); 96 paramIds[i] = param == null ? null : param._getMofId(); } 98 return new CacheKey(((BaseObjectHandler) def)._getMofId(), paramIds, outerCls == null ? null : ((BaseObjectHandler) outerCls)._getMofId()); 99 } 100 101 protected Collection _allOfClass(boolean recursive) { 102 return allInstances.values(); 103 } 104 105 public org.netbeans.jmi.javamodel.Type resolve(java.lang.String name) { 106 JavaModelPackage pkg = (JavaModelPackage) refImmediatePackage(); 107 return pkg.getType().resolve(name); 108 } 109 110 private static class CacheKey { 111 private final MOFID def, params[], outer; 112 113 public CacheKey(MOFID def, MOFID[] params, MOFID outer) { 114 if (def == null) throw new IllegalArgumentException (); 115 this.def = def; 116 this.params = params; 117 this.outer = outer; 118 } 119 120 public boolean equals(Object o) { 121 if (!(o instanceof CacheKey)) return false; 122 123 CacheKey ck = (CacheKey) o; 124 if (params.length != ck.params.length) return false; 125 126 for (int i = 0; i < params.length; i++) { 127 if (!compare(params[i], ck.params[i])) return false; 128 } 129 130 if (!(compare(def, ck.def) && compare(outer, ck.outer))) return false; 131 132 return true; 133 } 134 135 public int hashCode() { 136 return def.hashCode() * 31 + params.length + (outer == null ? 0 : outer.hashCode()); 137 } 138 139 private boolean compare(Object o1, Object o2) { 140 return (o1 == null && o2 == null) || (o1 != null && o1.equals(o2)); 141 } 142 } 143 } 144 | Popular Tags |