KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > ParameterizedTypeClassImpl


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 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 /**
36  *
37  * @author Dusan Balek
38  */

39 public abstract class ParameterizedTypeClassImpl extends ClassProxyHandler implements ParameterizedTypeClass {
40     private static final String JavaDoc MOFID_PREFIX = "parameterizedType:"; // NOI18N
41
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."); // NOI18N
50
}
51
52     public ParameterizedType createParameterizedType(String JavaDoc name, List annotations, int modifiers, String JavaDoc 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."); // NOI18N
54
}
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(); // NOI18N
97
}
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 JavaDoc 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 JavaDoc();
115             this.def = def;
116             this.params = params;
117             this.outer = outer;
118         }
119         
120         public boolean equals(Object JavaDoc 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 JavaDoc o1, Object JavaDoc o2) {
140             return (o1 == null && o2 == null) || (o1 != null && o1.equals(o2));
141         }
142     }
143 }
144
Popular Tags