KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > StructImpl


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.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 /**
31  *
32  * @author Martin Matula
33  * @version
34  */

35 public abstract class StructImpl extends ImplClass implements RefStruct {
36     private final List fields;
37     // field values
38
private final Map values;
39     // fully qualified name of metaobject
40
private final List qualifiedName;
41
42     private static Class JavaDoc getStructClass(MDRClassLoader loader, Class JavaDoc ifc) throws IllegalArgumentException JavaDoc {
43         check(loader, ifc);
44         Map cache = getLoaderCache(loader);
45         String JavaDoc className = getName(ifc);
46         Class JavaDoc 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 JavaDoc 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 JavaDoc type = (Class JavaDoc) 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 JavaDoc e) {
74             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
75         }
76     }
77     
78     public static RefStruct newInstance(Class JavaDoc ifc, List fields, Map values, List qualifiedName) {
79         MDRClassLoader loader = BaseObjectHandler.getDefaultClassLoader();
80     Class JavaDoc cl = getStructClass(loader, ifc);
81
82         try {
83             Constructor cons = cl.getConstructor(new Class JavaDoc[] {List.class, Map.class, List.class});
84             RefStruct struct = (RefStruct) cons.newInstance(new Object JavaDoc[] {fields, values, qualifiedName});
85
86             return struct;
87     } catch (NoSuchMethodException JavaDoc e) {
88             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
89     } catch (IllegalAccessException JavaDoc e) {
90             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
91     } catch (InstantiationException JavaDoc 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     /** Creates new StructHandler */
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     // --- handler methods
106

107     protected Object JavaDoc handleGet(String JavaDoc memberName) {
108         return refGetValue(memberName);
109     }
110
111     // --- implementation of RefStruct interface -------------------------------
112

113     public Object JavaDoc refGetValue(String JavaDoc 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 JavaDoc other) {
134         // check whether other is instance of the same structure
135
if ((other instanceof RefStruct) && ((RefStruct) other).refTypeName().equals(qualifiedName)) {
136             Object JavaDoc key;
137             Object JavaDoc value;
138             // check whether all field values are same for both objects
139
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 JavaDoc) key) != null)
144                         return false;
145                 } else if (!value.equals(((RefStruct) other).refGetValue((String JavaDoc) key)))
146                     return false;
147             }
148             return true;
149         } else {
150             return false;
151         }
152     }
153 }
154
Popular Tags