KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > srcgen > resolve > SourceUpdateMethodResolver


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: SourceUpdateMethodResolver.java,v 1.7 2004/01/14 22:28:38 per_nyfelt Exp $
8
package org.ozoneDB.tools.OPP.srcgen.resolve;
9
10 import org.ozoneDB.tools.OPP.message.MessageWriter;
11 import org.ozoneDB.tools.OPP.OPPHelper;
12 import org.ozoneDB.tools.OPP.OPP;
13 import org.ozoneDB.tools.OPP.srcgen.MethodResolver;
14 import org.ozoneDB.tools.OPP.srcgen.ResolverException;
15 import org.ozoneDB.tools.OPP.srcgen.ClassQuery;
16 import org.ozoneDB.tools.OPP.srcgen.streamfactory.InputStreamFactory;
17 import org.ozoneDB.OzoneRemote;
18 import org.ozoneDB.core.Lock;
19
20 import java.io.*;
21 import java.util.regex.Pattern JavaDoc;
22
23 /**
24  * Helper class that allows to parse the Java source code of interface files
25  * to find update methods
26  *
27  * @author Joakim Ohlrogge
28  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
29  * @version $Revision: 1.7 $Date: 2004/01/14 22:28:38 $
30  */

31 public class SourceUpdateMethodResolver implements MethodResolver {
32     private MessageWriter genListener;
33     private InputStreamFactory streamFactory;
34     private ClassQuery classQuery;
35
36     public SourceUpdateMethodResolver(InputStreamFactory _streamFactory, MessageWriter _genListener, ClassQuery _classQuery) {
37         streamFactory = _streamFactory;
38         genListener = _genListener;
39         classQuery = _classQuery;
40     }
41
42     /**
43      * Recursivly search all remote interfaces (implement OzoneRemote) for
44      * update methods.
45      */

46     private void searchUpdateMethods(String JavaDoc cl, MethodResolver.UpdateMethodBag updateMethodsIf) throws IOException, ClassNotFoundException JavaDoc {
47         String JavaDoc dbRemote = OzoneRemote.class.getName();
48         java.util.Vector JavaDoc stack = new java.util.Vector JavaDoc();
49         stack.addElement(cl);
50
51         while (!stack.isEmpty()) {
52             String JavaDoc dbInterface = (String JavaDoc) stack.lastElement();
53             stack.removeElementAt(stack.size() - 1);
54             genListener.debug("checking: " + dbInterface);
55
56             if (!dbRemote.equals(dbInterface) && classQuery.isAssignable(dbRemote, dbInterface)) {
57                 String JavaDoc[] ifs = classQuery.getInterfaces(dbInterface);
58                 for (int i = 0; i < ifs.length; i++) {
59                     stack.addElement(ifs[i]);
60                 }
61                 //if (classQuery.isInterface(dbInterface)) {
62
searchUpdateMethods2(dbInterface, updateMethodsIf);
63             }
64             //}
65
}
66     }
67
68     /**
69      * Search all methods that are marked in the resolver code of the remote
70      * Java interface.
71      */

72     private void searchUpdateMethods2(String JavaDoc dbInterface, MethodResolver.UpdateMethodBag updateMethodsIf) throws IOException {
73         // method name regexp
74
Pattern JavaDoc re = OPPHelper.newRE(OPP.UPDATE_SIGN, true);
75         // method comment regexp
76
Pattern JavaDoc mre = OPPHelper.newRE(OPP.METHOD_PATTERN, false);
77         // javadoc regexp
78
Pattern JavaDoc jre = OPPHelper.newRE(OPP.JAVADOC_PATTERN, false);
79         // javadoc comemnt start
80
Pattern JavaDoc docstart = OPPHelper.newRE("/\\*\\*", false);
81
82
83         genListener.debug("reading: " + dbInterface);
84         LineNumberReader reader = new LineNumberReader(new InputStreamReader(streamFactory.newInstance(dbInterface)));
85
86         try {
87             String JavaDoc line = reader.readLine();
88
89             // to support multi line method signatures
90
// the regexp for update must match on the method signature line
91
// or one the followings
92

93             boolean isJavadocUpdate = false;
94             String JavaDoc lastMatchedMethod = null;
95             while (line != null) {
96                 // System.out.println (line);
97
boolean isUpdate = OPPHelper.reSearch(re, line, 0, 0) != null;
98                 // before each method stands his appropriate javadoc comment
99
String JavaDoc match = OPPHelper.reSearch(docstart, line, 0, 0);
100                 if (match != null) {
101                     lastMatchedMethod = null;
102                     isJavadocUpdate = false;
103                 }
104                 match = OPPHelper.reSearch(mre, line, 0, 1);
105                 if (match != null) {
106                     lastMatchedMethod = match;
107                 }
108                 match = OPPHelper.reSearch(jre, line, 0, 0);
109                 if (match != null) {
110                     isJavadocUpdate = true;
111                 }
112                 if (lastMatchedMethod != null && (isUpdate || isJavadocUpdate)) {
113                     String JavaDoc methodName = lastMatchedMethod;
114                     genListener.debug("method: " + methodName + " matched per update regexp in interface ");
115                     updateMethodsIf.addMethod(methodName, Lock.LEVEL_WRITE);
116                     lastMatchedMethod = null;
117                     isJavadocUpdate = false;
118                 } else if (lastMatchedMethod == null && isUpdate) {
119                     genListener.warning(OPPHelper.classFileBasename(dbInterface) + ".java", reader.getLineNumber(),
120                             "Unable to determine the method name for the "
121                             + " found update definition in line " + reader.getLineNumber() + ".");
122                 } else {
123                     /*if (lastMatchedMethod != null && !isUpdate && !isJavadocUpdate) {
124                         String methodName = lastMatchedMethod;
125                         if (updateMethodsIf.contains(methodName)) {
126                             genListener.warning(OPPHelper.classFileBasename(dbInterface) + ".java", reader.getLineNumber(),
127                                     "All '" + methodName + "' methods will be marked as update methods.");
128                         }
129                     }*/

130                 }
131                 line = reader.readLine();
132             }
133         } finally {
134             reader.close();
135         }
136     }
137
138     public void resolveMethods(String JavaDoc className, MethodResolver.UpdateMethodBag result) throws ResolverException {
139
140         try {
141             searchUpdateMethods(className, result);
142         } catch (IOException e) {
143             throw new ResolverException("Could not resolve update methods (" + className + "). " + e.getMessage(), e);
144         } catch (ClassNotFoundException JavaDoc e) {
145             throw new ResolverException("Could not resolve update methods (" + className + "). " + e.getMessage(), e);
146         }
147
148     }
149 }
Popular Tags