001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.tools.generator;
025:
026: import java.io.*;
027: import java.util.*;
028: import java.lang.reflect.*;
029: import java.lang.annotation.*;
030:
031: import org.myoodb.core.*;
032:
033: public class Helper {
034: private final static Map BASE_CLASSES;
035: private final static Map PRIMITIVE_NAMES;
036: private final static Map PRIMITIVE_CLASSES;
037:
038: static {
039: BASE_CLASSES = new HashMap();
040: BASE_CLASSES.put("java.lang.Byte", "byte");
041: BASE_CLASSES.put("java.lang.Character", "char");
042: BASE_CLASSES.put("java.lang.Double", "double");
043: BASE_CLASSES.put("java.lang.Float", "float");
044: BASE_CLASSES.put("java.lang.Integer", "int");
045: BASE_CLASSES.put("java.lang.Long", "long");
046: BASE_CLASSES.put("java.lang.Short", "short");
047: BASE_CLASSES.put("java.lang.Boolean", "boolean");
048: }
049:
050: static {
051: PRIMITIVE_NAMES = new HashMap();
052: PRIMITIVE_NAMES.put("byte", "Byte");
053: PRIMITIVE_NAMES.put("char", "Character");
054: PRIMITIVE_NAMES.put("double", "Double");
055: PRIMITIVE_NAMES.put("float", "Float");
056: PRIMITIVE_NAMES.put("int", "Integer");
057: PRIMITIVE_NAMES.put("long", "Long");
058: PRIMITIVE_NAMES.put("short", "Short");
059: PRIMITIVE_NAMES.put("boolean", "Boolean");
060: }
061:
062: static {
063: PRIMITIVE_CLASSES = new HashMap();
064: PRIMITIVE_CLASSES.put("byte", Byte.TYPE);
065: PRIMITIVE_CLASSES.put("char", Character.TYPE);
066: PRIMITIVE_CLASSES.put("double", Double.TYPE);
067: PRIMITIVE_CLASSES.put("float", Float.TYPE);
068: PRIMITIVE_CLASSES.put("int", Integer.TYPE);
069: PRIMITIVE_CLASSES.put("long", Long.TYPE);
070: PRIMITIVE_CLASSES.put("short", Short.TYPE);
071: PRIMITIVE_CLASSES.put("boolean", Boolean.TYPE);
072: }
073:
074: public static ArrayList findInterfaces(Class interfaceClass,
075: Class assignableFromClass) {
076: ArrayList interfaces = new ArrayList();
077: Class[] interfaceTypes = interfaceClass.getInterfaces();
078: for (int i = 0; i < interfaceTypes.length; i++) {
079: Class classType = interfaceTypes[i];
080: if ((assignableFromClass == null)
081: || (assignableFromClass.isAssignableFrom(classType) == true)) {
082: interfaces.add(classType);
083: }
084: }
085: return interfaces;
086: }
087:
088: public static void findInterfaces(Class interfaceClass,
089: ArrayList interfaces) {
090: interfaces.add(interfaceClass);
091: Class[] interfaceTypes = interfaceClass.getInterfaces();
092: for (int i = 0; i < interfaceTypes.length; i++) {
093: findInterfaces(interfaceTypes[i], interfaces);
094: }
095: }
096:
097: public static void searchForMethods(String sourcePath,
098: String className, HashMap methodTable) throws IOException,
099: ClassNotFoundException {
100: ArrayList interfaces = new ArrayList();
101: findInterfaces(Class.forName(className), interfaces);
102:
103: Iterator iter = interfaces.iterator();
104: while (iter.hasNext()) {
105: Class classType = (Class) iter.next();
106: System.out.println(" Searching: " + classType);
107:
108: Method[] methods = classType.getMethods();
109: for (int i = 0; i < methods.length; i++) {
110: org.myoodb.MyOodbAccess access = methods[i]
111: .getAnnotation(org.myoodb.MyOodbAccess.class);
112:
113: if (access != null) {
114: if (access.value().equalsIgnoreCase("Write") == true) {
115: methodTable.put(methods[i].getName(),
116: new Integer(
117: org.myoodb.MyOodbAccess.WRITE));
118: } else if (access.value()
119: .equalsIgnoreCase("Tunnel") == true) {
120: methodTable
121: .put(methods[i].getName(), new Integer(
122: org.myoodb.MyOodbAccess.TUNNEL));
123: } else if (access.value().equalsIgnoreCase(
124: "Realtime") == true) {
125: methodTable
126: .put(
127: methods[i].getName(),
128: new Integer(
129: org.myoodb.MyOodbAccess.REALTIME));
130: } else if (access.value().equalsIgnoreCase(
131: "Abandon") == true) {
132: methodTable
133: .put(
134: methods[i].getName(),
135: new Integer(
136: org.myoodb.MyOodbAccess.ABANDON));
137: } else {
138: methodTable.put(methods[i].getName(),
139: new Integer(
140: org.myoodb.MyOodbAccess.READ));
141: }
142: } else {
143: methodTable.put(methods[i].getName(), new Integer(
144: org.myoodb.MyOodbAccess.READ));
145: }
146: }
147: }
148: }
149:
150: public static boolean isBase(String type) {
151: return BASE_CLASSES.keySet().contains(type);
152: }
153:
154: public static String getBase(String type) {
155: return (String) BASE_CLASSES.get(type);
156: }
157:
158: public static String typecodeForPrimitive(char ch) throws Exception {
159: String ret = null;
160:
161: switch (ch) {
162: case 'B': {
163: ret = "byte";
164: break;
165: }
166: case 'C': {
167: ret = "char";
168: break;
169: }
170: case 'D': {
171: ret = "double";
172: break;
173: }
174: case 'F': {
175: ret = "float";
176: break;
177: }
178: case 'I': {
179: ret = "int";
180: break;
181: }
182: case 'J': {
183: ret = "long";
184: break;
185: }
186: case 'S': {
187: ret = "short";
188: break;
189: }
190: case 'Z': {
191: ret = "boolean";
192: break;
193: }
194: default: {
195: throw new Exception("Unknown type code '" + ch + "'.");
196: }
197: }
198:
199: return ret;
200: }
201:
202: public static String wrappercodeForPrimitive(Class classType) {
203: String name = classType.getName();
204: return wrappercodeForPrimitive(name);
205: }
206:
207: public static String wrappercodeForPrimitive(String name) {
208: String ret = null;
209:
210: if (name.equals("int")) {
211: ret = "Integer";
212: } else if (name.equals("char")) {
213: ret = "Character";
214: } else {
215: ret = name.substring(0, 1).toUpperCase()
216: + name.substring(1);
217: }
218:
219: return ret;
220: }
221:
222: public static boolean isPrimitive(String type) {
223: return PRIMITIVE_CLASSES.keySet().contains(type);
224: }
225:
226: public static String returncodeForPrimitive(Class classType,
227: String varName) {
228: String name = classType.getName();
229: return returncodeForPrimitive(name, varName);
230: }
231:
232: public static String returncodeForPrimitive(String name,
233: String varName) {
234: String ret = null;
235:
236: if (name.equals("int")) {
237: ret = "((Integer)" + varName + ").intValue()";
238: } else if (name.equals("boolean")) {
239: ret = "((Boolean)" + varName + ").booleanValue()";
240: } else if (name.equals("char")) {
241: ret = "((Character)" + varName + ").charValue()";
242: } else if (name.equals("long")) {
243: ret = "((Long)" + varName + ").longValue()";
244: } else if (name.equals("float")) {
245: ret = "((Float)" + varName + ").floatValue()";
246: } else if (name.equals("double")) {
247: ret = "((Double)" + varName + ").doubleValue()";
248: } else if (name.equals("byte")) {
249: ret = "((Byte)" + varName + ").byteValue()";
250: } else if (name.equals("short")) {
251: ret = "((Short)" + varName + ").shortValue()";
252: } else {
253: throw new IllegalArgumentException("unknown type: '" + name
254: + "'");
255: }
256:
257: return ret;
258: }
259:
260: public static String packageName(Class classType) {
261: String name = classType.getName();
262: return Helper.packageName(name);
263: }
264:
265: public static String packageName(String name) {
266: int index = name.lastIndexOf('.');
267: return index != -1 ? name.substring(0, index) : "";
268: }
269:
270: public static String simpleClassName(Class classType) {
271: return simpleClassName(classType.getName());
272: }
273:
274: public static String simpleClassName(String name) {
275: int index = name.lastIndexOf('.');
276: return index != -1 ? name.substring(index + 1) : name;
277: }
278:
279: public static String classToFileName(Class classType) {
280: return classToFileName(classType.getName());
281: }
282:
283: public static String classToFileName(String name) {
284: return classToFileName(name, File.separatorChar);
285: }
286:
287: public static String classToFileName(String name, char separator) {
288: return name.replace('.', separator);
289: }
290:
291: public static String fileNameToClass(String name) {
292: return fileNameToClass(name, File.separatorChar);
293: }
294:
295: public static String fileNameToClass(String name, char separator) {
296: return name.replace(separator, '.');
297: }
298:
299: public static String nameForPrimitive(String type) {
300: return (String) PRIMITIVE_NAMES.get(type);
301: }
302:
303: public static Class classForPrimitive(String type) {
304: return (Class) PRIMITIVE_CLASSES.get(type);
305: }
306: }
|