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: IfHelper.java,v 1.2 2002/04/17 09:29:39 per_nyfelt Exp $
008:
009: package org.ozoneDB.tools.OPP;
010:
011: import java.io.*;
012: import java.lang.reflect.*; //import org.apache.regexp.*;
013: import org.ozoneDB.DxLib.*;
014:
015: /**
016: * Helper class that allows to parse the Java source code of interface files
017: * to find out extra class information.
018: *
019: *
020: * @author <a href="http://www.softwarebuero.de/">SMB</a>
021: * @version $Revision: 1.2 $Date: 2002/04/17 09:29:39 $
022: */
023: class IfHelper {
024:
025: protected Class cl;
026:
027: protected Class dbInterface;
028:
029: protected boolean quiet;
030:
031: protected Object re;
032:
033: protected String outputDir;
034:
035: /**
036: * Names of update methods as Strings.
037: */
038: protected DxHashMap updateMethodsIf = new DxHashMap();
039:
040: public IfHelper(Class _cl, String _outputDir, boolean _quiet) {
041: cl = _cl;
042: outputDir = _outputDir;
043: quiet = _quiet;
044: }
045:
046: /**
047: * Recursivly search all remote interfaces (implement OzoneRemote) for
048: * update methods.
049: */
050: public void searchUpdateMethods(DxHashMap updateMethodsIf)
051: throws Exception {
052: Class dbRemote = Class.forName("org.ozoneDB.OzoneRemote");
053:
054: java.util.Vector stack = new java.util.Vector();
055: stack.addElement(cl);
056:
057: while (!stack.isEmpty()) {
058: Class dbInterface = (Class) stack.lastElement();
059: stack.removeElementAt(stack.size() - 1);
060: // System.out.println( "search: " + dbInterface );
061:
062: if (dbRemote.isAssignableFrom(dbInterface)
063: && !dbRemote.equals(dbInterface)) {
064: Class[] ifs = dbInterface.getInterfaces();
065: for (int i = 0; i < ifs.length; i++) {
066: stack.addElement(ifs[i]);
067: }
068: if (dbInterface.isInterface()) {
069: searchUpdateMethods2(dbInterface, updateMethodsIf);
070: }
071: }
072: }
073: }
074:
075: /**
076: * Search all methods that are marked in the source code of the remote
077: * Java interface.
078: */
079: protected void searchUpdateMethods2(Class dbInterface,
080: DxHashMap updateMethodsIf) throws Exception {
081:
082: // method name regexp
083: Object re = OPPHelper.newRE(OPP.UPDATE_SIGN, true);
084: // method comment regexp
085: Object mre = OPPHelper.newRE(OPP.METHOD_PATTERN, false);
086: // javadoc regexp
087: Object jre = OPPHelper.newRE(OPP.JAVADOC_PATTERN, false);
088: // javadoc comemnt start
089: Object docstart = OPPHelper.newRE("/\\*\\*", false);
090:
091: //search *.java source in current and in output directory
092: String sourceName = OPP.sourceDirName
093: + OPPHelper.classFileBasename(dbInterface) + ".java";
094:
095: if (!quiet) {
096: System.out.print(" trying to process " + sourceName
097: + "... ");
098: }
099:
100: if (!new File(sourceName).exists()) {
101: sourceName = outputDir
102: + OPPHelper.classFileBasename(dbInterface)
103: + ".java";
104: if (!new File(sourceName).exists()) {
105: if (!quiet) {
106: System.out
107: .println(" no source file for this interface found.");
108: }
109: return;
110: }
111: }
112:
113: if (!quiet) {
114: System.out.println("(" + sourceName + ")");
115: }
116:
117: LineNumberReader in = new LineNumberReader(new FileReader(
118: sourceName));
119: String line = in.readLine();
120: // to support multi line method signatures
121: // the regexp for update must match on the method signature line
122: // or one the followings
123: boolean isJavadocUpdate = false;
124: String lastMatchedMethod = null;
125: while (line != null) {
126: // System.out.println (line);
127: boolean isUpdate = OPPHelper.reSearch(re, line, 0, 0) != null;
128: // before each method stands his appropriate javadoc comment
129: String match = OPPHelper.reSearch(docstart, line, 0, 0);
130: if (match != null) {
131: lastMatchedMethod = null;
132: isJavadocUpdate = false;
133: }
134:
135: match = OPPHelper.reSearch(mre, line, 0, 1);
136: if (match != null) {
137: lastMatchedMethod = match;
138: }
139: match = OPPHelper.reSearch(jre, line, 0, 0);
140: if (match != null) {
141: isJavadocUpdate = true;
142: }
143: if (lastMatchedMethod != null
144: && (isUpdate || isJavadocUpdate)) {
145: String methodName = lastMatchedMethod;
146: /*System.out.println (this.getClass().getName() + " method: "
147: + methodName + " matched per update regexp in interface ");*/
148: updateMethodsIf.addForKey(methodName, methodName);
149: lastMatchedMethod = null;
150: isJavadocUpdate = false;
151: } else if (lastMatchedMethod == null && isUpdate) {
152: OPPHelper.warnMsg(OPPHelper
153: .classFileBasename(dbInterface)
154: + ".java", in.getLineNumber(),
155: "Unable to determine the method name for the "
156: + " found update definition in line "
157: + in.getLineNumber() + ".");
158: } else {
159: if (lastMatchedMethod != null && !isUpdate
160: && !isJavadocUpdate) {
161: String methodName = lastMatchedMethod;
162: if (updateMethodsIf.contains(methodName)) {
163: OPPHelper
164: .warnMsg(
165: OPPHelper
166: .classFileBasename(dbInterface)
167: + ".java",
168: in.getLineNumber(),
169: "All '"
170: + methodName
171: + "' methods will be marked as update methods.");
172: }
173: }
174: }
175: line = in.readLine();
176: }
177: in.close();
178: }
179: }
|