001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: OPPHelper.java,v 1.2 2002/04/17 09:29:39 per_nyfelt Exp $
008:
009: package org.ozoneDB.tools.OPP;
010:
011: import java.lang.reflect.*;
012: import org.apache.regexp.*;
013: import org.ozoneDB.core.*;
014: import org.ozoneDB.DxLib.*;
015:
016: /**
017: * @author <a href="http://www.softwarebuero.de/">SMB</a>
018: * @version $Revision: 1.2 $Date: 2002/04/17 09:29:39 $
019: */
020: public class OPPHelper {
021:
022: public static void progressMsg(String msg, boolean quiet) {
023: if (!quiet) {
024: System.out.println(msg);
025: }
026: }
027:
028: public static void warnMsg(String filename, int line, String msg) {
029: System.out.println(filename + ":" + line + ": warning: " + msg
030: + ".");
031: }
032:
033: public static void warnMsg(Class cl, String msg) {
034: System.out.println(cl.getName() + ": warning:" + msg);
035: }
036:
037: public static void error(String filename, int line, String msg) {
038: System.out.println(filename + ":" + line + ": error: " + msg);
039: System.exit(1);
040: }
041:
042: public static void error(Class cl, String msg) {
043: System.out.println(cl.getName() + ": error:" + msg);
044: System.exit(1);
045: }
046:
047: public static String typecodeForPrimitive(char ch) throws Exception {
048: String ret;
049: switch (ch) {
050: case 'B':
051: ret = "byte";
052: break;
053: case 'C':
054: ret = "char";
055: break;
056: case 'D':
057: ret = "double";
058: break;
059: case 'F':
060: ret = "float";
061: break;
062: case 'I':
063: ret = "int";
064: break;
065: case 'J':
066: ret = "long";
067: break;
068: case 'S':
069: ret = "short";
070: break;
071: case 'Z':
072: ret = "boolean";
073: break;
074: default:
075: throw new Exception("Unknown type code '" + ch + "'.");
076: }
077: return ret;
078: }
079:
080: public static String wrappercodeForPrimitive(Class cl)
081: throws Exception {
082: String ret;
083: String name = cl.getName();
084: if (name.equals("int")) {
085: ret = "Integer";
086: } else if (name.equals("char")) {
087: ret = "Character";
088: } else {
089: ret = name.substring(0, 1).toUpperCase()
090: + name.substring(1);
091: }
092: return ret;
093: }
094:
095: /**
096: * For all primitive types in cl return a string like '<varName>.intValue()'.
097: */
098: public static String returncodeForPrimitive(Class cl, String varName)
099: throws Exception {
100: String ret;
101: String name = cl.getName();
102: if (name.equals("int")) {
103: ret = "((Integer)" + varName + ").intValue()";
104: } else if (name.equals("boolean")) {
105: ret = "((Boolean)" + varName + ").booleanValue()";
106: } else if (name.equals("char")) {
107: ret = "((Character)" + varName + ").charValue()";
108: } else if (name.equals("long")) {
109: ret = "((Long)" + varName + ").longValue()";
110: } else if (name.equals("float")) {
111: ret = "((Float)" + varName + ").floatValue()";
112: } else if (name.equals("double")) {
113: ret = "((Double)" + varName + ").doubleValue()";
114: } else if (name.equals("byte")) {
115: ret = "((Byte)" + varName + ").byteValue()";
116: } else if (name.equals("short")) {
117: ret = "((Short)" + varName + ").shortValue()";
118: } else {
119: throw new Exception("unknown type: '" + name + "'");
120: }
121: return ret;
122: }
123:
124: public static String signature(Class[] args) {
125: String result = new String();
126: for (int i = 0; i < args.length; i++) {
127: result = i > 0 ? result + OPP.SIGNATURE_DELIMITER : result;
128: result = result + args[i].getName();
129: }
130: return "\"" + result + "\"";
131: }
132:
133: public static String packageName(Class c) {
134: String name = c.getName();
135: int index = name.lastIndexOf('.');
136: return index != -1 ? name.substring(0, index) : "";
137: }
138:
139: public static String simpleClassName(Class c) {
140: return simpleClassName(c.getName());
141: }
142:
143: public static String simpleClassName(String name) {
144: int index = name.lastIndexOf('.');
145: return index != -1 ? name.substring(index + 1) : name;
146: }
147:
148: public static String classFileBasename(Class c) {
149: return classFileBasename(c.getName());
150: }
151:
152: public static String classFileBasename(String name) {
153: return name.replace('.', java.io.File.separatorChar);
154: }
155:
156: /**
157: * Returns the array index of the specified method within the array
158: * of methods of this class.
159: */
160: public static int methodArrayIndex(Method[] methods, Method m) {
161: String mName = m.getName();
162: String mSig = OPPHelper.signature(m.getParameterTypes());
163:
164: for (int i = 0; i < methods.length; i++) {
165: String cName = methods[i].getName();
166: String cSig = OPPHelper.signature(methods[i]
167: .getParameterTypes());
168: if (mName.equals(cName) && mSig.equals(cSig)) {
169: return i;
170: }
171: }
172: throw new RuntimeException(m
173: + ": Unable to find method in class.");
174: }
175:
176: public static Method[] methodsOfClass(Class cl) {
177: Method[] methods = cl.getMethods();
178:
179: DxTreeSet set = new DxTreeSet();
180:
181: for (int i = 0; i < methods.length; i++) {
182: String name = methods[i].getName();
183: String sig = OPPHelper.signature(methods[i]
184: .getParameterTypes());
185: set.add(new MethodKey(cl.getName(), name, sig, methods[i]));
186: }
187:
188: DxIterator it = set.iterator();
189: for (int i = 0; it.next() != null; i++) {
190: MethodKey key = (MethodKey) it.object();
191: methods[i] = key.method();
192: }
193: return methods;
194: }
195:
196: public static Object newRE(String s, boolean ignoreCase)
197: throws Exception {
198: int flags = ignoreCase ? RE.MATCH_CASEINDEPENDENT
199: : RE.MATCH_NORMAL;
200: return new RE(s, flags);
201: }
202:
203: public static boolean reMatch(Object re, String s) {
204: return ((RE) re).match(s);
205: }
206:
207: // public static int reSearch (Object re, String s, int start) {
208: // for (int i=start; i < s.length(); i++) {
209: // if (((RE)re).match (s, i))
210: // return i;
211: // }
212: // return -1;
213: // }
214:
215: public static String reSearch(Object re, String s, int start,
216: int paren) {
217: for (int i = start; i < s.length(); i++) {
218: if (((RE) re).match(s, i)) {
219: return ((RE) re).getParen(paren);
220: }
221: }
222: return null;
223: }
224:
225: public static void main(String[] av) throws Exception {
226: System.out.println(av[0]);
227:
228: Object re = newRE(av[0], false);
229: System.out.println(reSearch(re, av[1], 0, 0));
230: }
231:
232: }
|