KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > tools > internal > MetaData


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.tools.internal;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.lang.reflect.Field JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 public class MetaData {
20     
21     Properties JavaDoc data;
22
23 public MetaData(String JavaDoc mainClass) {
24     data = new Properties JavaDoc();
25     int index = 0;
26     Class JavaDoc clazz = getClass();
27     int length = mainClass.length();
28     while (index < length) {
29         index = mainClass.indexOf('.', index);
30         if (index == -1) index = length;
31         InputStream JavaDoc is = clazz.getResourceAsStream(mainClass.substring(0, index) + ".properties");
32         if (is != null) {
33             try {
34                 data.load(is);
35             } catch (IOException JavaDoc e) {
36             } finally {
37                 try {
38                     is.close();
39                 } catch (IOException JavaDoc e) {}
40             }
41             
42         }
43         index++;
44     }
45 }
46
47 public MetaData(Properties JavaDoc data) {
48     this.data = data;
49 }
50
51 public ClassData getMetaData(Class JavaDoc clazz) {
52     String JavaDoc key = JNIGenerator.toC(clazz.getName());
53     String JavaDoc value = getMetaData(key, "");
54     return new ClassData(clazz, value);
55 }
56
57 public FieldData getMetaData(Field JavaDoc field) {
58     String JavaDoc className = JNIGenerator.getClassName(field.getDeclaringClass());
59     String JavaDoc key = className + "_" + field.getName();
60     String JavaDoc value = getMetaData(key, "");
61     return new FieldData(field, value);
62 }
63
64 boolean convertTo32Bit(Class JavaDoc[] paramTypes) {
65     boolean changed = false;
66     for (int i = 0; i < paramTypes.length; i++) {
67         Class JavaDoc paramType = paramTypes[i];
68         if (paramType == Long.TYPE) {
69             paramTypes[i] = Integer.TYPE;
70             changed = true;
71         }
72         if (paramType == long[].class) {
73             paramTypes[i] = int[].class;
74             changed = true;
75         }
76     }
77     return changed;
78 }
79
80 public MethodData getMetaData(Method JavaDoc method) {
81     String JavaDoc className = JNIGenerator.getClassName(method.getDeclaringClass());
82     String JavaDoc key = className + "_" + JNIGenerator.getFunctionName(method);
83     String JavaDoc value = getMetaData(key, null);
84     if (value == null) {
85         key = className + "_" + method.getName();
86         value = getMetaData(key, null);
87     }
88     /*
89     * Support for 64 bit port.
90     */

91     if (value == null) {
92         Class JavaDoc[] paramTypes = method.getParameterTypes();
93         if (convertTo32Bit(paramTypes)) {
94             key = className + "_" + JNIGenerator.getFunctionName(method, paramTypes);
95             value = getMetaData(key, null);
96         }
97     }
98     if (value == null) value = "";
99     return new MethodData(method, value);
100 }
101
102 public ParameterData getMetaData(Method JavaDoc method, int parameter) {
103     String JavaDoc className = JNIGenerator.getClassName(method.getDeclaringClass());
104     String JavaDoc key = className + "_" + JNIGenerator.getFunctionName(method) + "_" + parameter;
105     String JavaDoc value = getMetaData(key, null);
106     if (value == null) {
107         key = className + "_" + method.getName() + "_" + parameter;
108         value = getMetaData(key, null);
109     }
110     /*
111     * Support for 64 bit port.
112     */

113     if (value == null) {
114         Class JavaDoc[] paramTypes = method.getParameterTypes();
115         if (convertTo32Bit(paramTypes)) {
116             key = className + "_" + JNIGenerator.getFunctionName(method, paramTypes) + "_" + parameter;
117             value = getMetaData(key, null);
118         }
119     }
120     if (value == null) value = "";
121     return new ParameterData(method, parameter, value);
122 }
123
124 public String JavaDoc getMetaData(String JavaDoc key, String JavaDoc defaultValue) {
125     return data.getProperty(key, defaultValue);
126 }
127
128 public void setMetaData(Class JavaDoc clazz, ClassData value) {
129     String JavaDoc key = JNIGenerator.toC(clazz.getName());
130     setMetaData(key, value.toString());
131 }
132
133 public void setMetaData(Field JavaDoc field, FieldData value) {
134     String JavaDoc className = JNIGenerator.getClassName(field.getDeclaringClass());
135     String JavaDoc key = className + "_" + field.getName();
136     setMetaData(key, value.toString());
137 }
138
139 public void setMetaData(Method JavaDoc method, MethodData value) {
140     String JavaDoc key;
141     String JavaDoc className = JNIGenerator.getClassName(method.getDeclaringClass());
142     if (JNIGenerator.isNativeUnique(method)) {
143         key = className + "_" + method.getName ();
144     } else {
145         key = className + "_" + JNIGenerator.getFunctionName(method);
146     }
147     setMetaData(key, value.toString());
148 }
149
150 public void setMetaData(Method JavaDoc method, int arg, ParameterData value) {
151     String JavaDoc key;
152     String JavaDoc className = JNIGenerator.getClassName(method.getDeclaringClass());
153     if (JNIGenerator.isNativeUnique(method)) {
154         key = className + "_" + method.getName () + "_" + arg;
155     } else {
156         key = className + "_" + JNIGenerator.getFunctionName(method) + "_" + arg;
157     }
158     setMetaData(key, value.toString());
159 }
160
161 public void setMetaData(String JavaDoc key, String JavaDoc value) {
162     data.setProperty(key, value);
163 }
164
165 }
166
Popular Tags