====== Unterschiede ====== Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Both sides previous revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
de:howto:java_source_preparator [2009/03/15 12:10] til |
de:howto:java_source_preparator [2024/09/18 08:32] (aktuell) |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | FIXME: Import aus MediaWiki muss für diese Seite noch abgeschlossen werden | + | ====== How-To: Präparator für Java-Quellcode ====== |
- | = Parser für Java-Quellcode = | + | **Das Zusatzfeature:** |
+ | Parsen von Java-Quellcode ermöglichen und damit Java-Quellcode über regain durchsuchbar machen. | ||
+ | **Lösungsansatz:** | ||
- | Feature: | + | Einen neuen [[:de:components:preparator|Präparator]] implementieren. |
- | Parsen von Java-Quellcode ermöglichen | + | **Vorteile:** |
- | ;Lösungsansatz: | + | * Java-Quellcode muss nicht mit dem ''PlainTextPreparator'' geparsed werden. Der Quellcode ist nicht als einfacher Text im Index, sondern in aufbereiteter Form. |
+ | * Die Suche mit regain ist schneller als eine JAVA-IDE-Suche. | ||
- | Einen neuen Preparator implementieren. | + | **Nachteile:** |
- | ;Vorteile: | + | * Benötigt große Teile der Eclipse IDE und ist dadurch schwergewichter als eigentlich nötig. D.h. es wird viel ungenutzter Code mitgeschleppt, so dass der Präparator 13 MB groß ist. |
- | Java-Quellcode muss nicht mit einem PlainTextPreparator geparsed werden. Die Suche mit regain wird schneller als jede JAVA-IDE-Suche sein. | + | **Autor:** |
- | ;Schlüsselwörter: | + | Thomas Tesche (thtesche), cluster:Systems CSG GmbH, http://www.clustersystems.info/ |
- | Java, Sourcecode, .java | ||
- | ;Java-Quellen: | + | ===== Download ===== |
- | *[http://www.onjava.com/pub/a/onjava/2006/01/18/using-lucene-to-search-java-source.html?page=1 Using Lucene to Search Java Source Code] | + | **Binaries:** |
- | + | ||
- | *[http://www-128.ibm.com/developerworks/opensource/library/os-ast/ Exploring Eclipse's ASTParser] | + | |
- | + | ||
- | ;Binaries: | + | |
Man benötigt einen großen Teil einer Eclipse 3.3 Distribution. Da die verwendeten Klassen innerhalb der Distribution immer wieder verschoben werden, muss man die Abhängkeiten der aktuell verwendeten Distri berücksichtigen. Aus einer Eclipse 3.3.1 Distri wurden folgende jar's verwendet: | Man benötigt einen großen Teil einer Eclipse 3.3 Distribution. Da die verwendeten Klassen innerhalb der Distribution immer wieder verschoben werden, muss man die Abhängkeiten der aktuell verwendeten Distri berücksichtigen. Aus einer Eclipse 3.3.1 Distri wurden folgende jar's verwendet: | ||
- | * org.eclipse.core.contenttype_3.2.100.v20070319.jar | + | * ''org.eclipse.core.contenttype_3.2.100.v20070319.jar'' |
+ | * ''org.eclipse.core.jobs_3.3.1.R33x_v20070709.jar'' | ||
+ | * ''org.eclipse.core.resources_3.3.1.R33x_v20080205.jar'' | ||
+ | * ''org.eclipse.core.runtime_3.3.100.v20070530.jar'' | ||
+ | * ''org.eclipse.equinox.common_3.3.0.v20070426.jar'' | ||
+ | * ''org.eclipse.equinox.preferences_3.2.101.R33x_v20080117.jar'' | ||
+ | * ''org.eclipse.jdt.core_3.3.3.v_793_R33x.jar'' | ||
+ | * ''org.eclipse.osgi_3.3.2.R33x_v20080105.jar'' | ||
- | * org.eclipse.core.jobs_3.3.1.R33x_v20070709.jar | + | **Download des fertigen Präparators:** |
- | * org.eclipse.core.resources_3.3.1.R33x_v20080205.jar | + | Enthalten in der regain Standardversion. |
- | * org.eclipse.core.runtime_3.3.100.v20070530.jar | ||
- | * org.eclipse.equinox.common_3.3.0.v20070426.jar | + | ===== Anleitung ===== |
- | * org.eclipse.equinox.preferences_3.2.101.R33x_v20080117.jar | + | Klasse ''JavaPreparator'': |
- | * org.eclipse.jdt.core_3.3.3.v_793_R33x.jar | + | <code java> |
- | + | package net.sf.regain.crawler.preparator; | |
- | * org.eclipse.osgi_3.3.2.R33x_v20080105.jar | + | |
- | + | ||
- | + | ||
- | + | ||
- | ;Autor: | + | |
- | + | ||
- | Thomas Tesche (thtesche), cluster:Consult, http://www.thtesche.com/ und http://clusterconsult.thtesche.com/ | + | |
- | + | ||
- | + | ||
- | + | ||
- | ;Download: | + | |
- | + | ||
- | *[http://www.assembla.com/spaces/files/regain2 regain2 Download Seite] | + | |
- | + | ||
- | *[http://clusterconsult.thtesche.com/download/regain-preview/ cluster:Consult Downloads (langsam)] | + | |
- | + | ||
- | ==Implementierung== | + | |
- | + | ||
- | * ''JavaPreparator'' | + | |
- | + | ||
- | + | ||
- | + | ||
- | ''<div>package net.sf.regain.crawler.preparator; | + | |
import net.sf.regain.crawler.preparator.java.*; | import net.sf.regain.crawler.preparator.java.*; | ||
- | |||
import java.util.ArrayList; | import java.util.ArrayList; | ||
- | |||
import java.util.Vector; | import java.util.Vector; | ||
- | |||
import net.sf.regain.RegainException; | import net.sf.regain.RegainException; | ||
- | |||
import net.sf.regain.crawler.document.AbstractPreparator; | import net.sf.regain.crawler.document.AbstractPreparator; | ||
- | |||
import net.sf.regain.crawler.document.RawDocument; | import net.sf.regain.crawler.document.RawDocument; | ||
Zeile 84: | Zeile 60: | ||
public JavaPreparator() throws RegainException { | public JavaPreparator() throws RegainException { | ||
- | |||
super(new String[]{"text/java"}); | super(new String[]{"text/java"}); | ||
- | |||
} | } | ||
public void prepare(RawDocument rawDocument) throws RegainException { | public void prepare(RawDocument rawDocument) throws RegainException { | ||
- | |||
Vector<String> contentParts = new Vector<String>(); | Vector<String> contentParts = new Vector<String>(); | ||
- | |||
Vector<String> titleParts = new Vector<String>(); | Vector<String> titleParts = new Vector<String>(); | ||
try { | try { | ||
- | |||
// Creates the parser | // Creates the parser | ||
- | |||
JavaParser parser = new JavaParser(); | JavaParser parser = new JavaParser(); | ||
- | |||
parser.setSource(rawDocument.getContentAsString()); | parser.setSource(rawDocument.getContentAsString()); | ||
- | |||
JClass cls = parser.getDeclaredClass(); | JClass cls = parser.getDeclaredClass(); | ||
- | |||
- | |||
String class_interface = cls.isInterface() ? "Interface: " : "Class: "; | String class_interface = cls.isInterface() ? "Interface: " : "Class: "; | ||
- | |||
- | |||
titleParts.add(class_interface + cls.getClassName()); | titleParts.add(class_interface + cls.getClassName()); | ||
- | |||
// extract the class info (including inner classes) | // extract the class info (including inner classes) | ||
- | |||
contentParts.add(extractClassInfo(cls, false).toString()); | contentParts.add(extractClassInfo(cls, false).toString()); | ||
- | |||
- | |||
setTitle(concatenateStringParts(titleParts, Integer.MAX_VALUE)); | setTitle(concatenateStringParts(titleParts, Integer.MAX_VALUE)); | ||
- | |||
setCleanedContent(concatenateStringParts(contentParts, Integer.MAX_VALUE)); | setCleanedContent(concatenateStringParts(contentParts, Integer.MAX_VALUE)); | ||
- | |||
- | |||
- | |||
} catch (Exception ex) { | } catch (Exception ex) { | ||
- | |||
throw new RegainException("Error parsing Java file: " + rawDocument.getUrl(), ex); | throw new RegainException("Error parsing Java file: " + rawDocument.getUrl(), ex); | ||
- | |||
} | } | ||
- | |||
- | |||
- | |||
} | } | ||
- | |||
- | |||
private StringBuffer extractClassInfo(JClass cls, boolean innerClass) { | private StringBuffer extractClassInfo(JClass cls, boolean innerClass) { | ||
- | |||
- | |||
- | |||
StringBuffer strBuffer = new StringBuffer(); | StringBuffer strBuffer = new StringBuffer(); | ||
- | |||
- | |||
//For each class add Class Name field | //For each class add Class Name field | ||
- | |||
String class_interface = ""; | String class_interface = ""; | ||
- | |||
if (cls.isInterface()) { | if (cls.isInterface()) { | ||
- | |||
class_interface = " Interface: "; | class_interface = " Interface: "; | ||
- | |||
} else if (innerClass) { | } else if (innerClass) { | ||
- | |||
class_interface = ", InnerClass: "; | class_interface = ", InnerClass: "; | ||
- | |||
} else { | } else { | ||
- | |||
class_interface = " Class: "; | class_interface = " Class: "; | ||
- | |||
} | } | ||
- | |||
- | |||
strBuffer.append(class_interface).append(cls.getClassName()); | strBuffer.append(class_interface).append(cls.getClassName()); | ||
- | |||
- | |||
String superCls = cls.getSuperClass(); | String superCls = cls.getSuperClass(); | ||
- | |||
if (superCls != null) //Add the class it extends as extends field | if (superCls != null) //Add the class it extends as extends field | ||
- | |||
{ | { | ||
- | |||
strBuffer.append(", Superclass: ").append(superCls); | strBuffer.append(", Superclass: ").append(superCls); | ||
- | |||
} | } | ||
// Add interfaces it implements | // Add interfaces it implements | ||
- | |||
ArrayList interfaces = cls.getInterfaces(); | ArrayList interfaces = cls.getInterfaces(); | ||
- | |||
for (int i = 0; i < interfaces.size(); i++) { | for (int i = 0; i < interfaces.size(); i++) { | ||
- | |||
strBuffer.append(", implements: ").append((String) interfaces.get(i)); | strBuffer.append(", implements: ").append((String) interfaces.get(i)); | ||
- | |||
} | } | ||
- | |||
- | |||
// Add details on methods declared | // Add details on methods declared | ||
- | |||
strBuffer.append(extractMethodInfo(cls)); | strBuffer.append(extractMethodInfo(cls)); | ||
- | |||
- | |||
// Examine inner classes and extract the same details as for the class | // Examine inner classes and extract the same details as for the class | ||
- | |||
ArrayList innerCls = cls.getInnerClasses(); | ArrayList innerCls = cls.getInnerClasses(); | ||
- | |||
for (int i = 0; i < innerCls.size(); i++) { | for (int i = 0; i < innerCls.size(); i++) { | ||
- | |||
strBuffer.append(extractClassInfo((JClass) innerCls.get(i), true)); | strBuffer.append(extractClassInfo((JClass) innerCls.get(i), true)); | ||
- | |||
} | } | ||
- | |||
- | |||
return strBuffer; | return strBuffer; | ||
- | |||
} | } | ||
- | |||
- | |||
private StringBuffer extractMethodInfo(JClass cls) { | private StringBuffer extractMethodInfo(JClass cls) { | ||
- | |||
- | |||
- | |||
StringBuffer strBuffer = new StringBuffer(); | StringBuffer strBuffer = new StringBuffer(); | ||
- | |||
- | |||
// get all methods | // get all methods | ||
- | |||
ArrayList methods = cls.getMethodDeclarations(); | ArrayList methods = cls.getMethodDeclarations(); | ||
- | |||
for (int i = 0; i < methods.size(); i++) { | for (int i = 0; i < methods.size(); i++) { | ||
- | |||
JMethod method = (JMethod) methods.get(i); | JMethod method = (JMethod) methods.get(i); | ||
- | |||
- | |||
strBuffer.append(", "); | strBuffer.append(", "); | ||
- | |||
// Add return type field | // Add return type field | ||
- | |||
String returnType = method.getReturnType(); | String returnType = method.getReturnType(); | ||
- | |||
if (returnType != null) { | if (returnType != null) { | ||
- | |||
if (!returnType.equalsIgnoreCase("void")) { | if (!returnType.equalsIgnoreCase("void")) { | ||
- | |||
strBuffer.append(" ").append(returnType); | strBuffer.append(" ").append(returnType); | ||
- | |||
} | } | ||
- | |||
} | } | ||
// Add method name field | // Add method name field | ||
- | |||
strBuffer.append(" ").append(method.getMethodName()).append("("); | strBuffer.append(" ").append(method.getMethodName()).append("("); | ||
- | |||
- | |||
ArrayList params = method.getParameters(); | ArrayList params = method.getParameters(); | ||
- | |||
for (int k = 0; k < params.size(); k++) // For each method add parameter types | for (int k = 0; k < params.size(); k++) // For each method add parameter types | ||
- | |||
{ | { | ||
- | |||
if (k != 0) { | if (k != 0) { | ||
- | |||
strBuffer.append(" "); | strBuffer.append(" "); | ||
- | |||
} | } | ||
- | |||
strBuffer.append((String) params.get(k)); | strBuffer.append((String) params.get(k)); | ||
- | |||
} | } | ||
- | |||
strBuffer.append(") "); | strBuffer.append(") "); | ||
String code = method.getCodeBlock(); | String code = method.getCodeBlock(); | ||
- | |||
if (code != null) //add the method code block | if (code != null) //add the method code block | ||
- | |||
{ | { | ||
- | |||
strBuffer.append(code); | strBuffer.append(code); | ||
- | |||
} | } | ||
- | |||
} | } | ||
- | |||
- | |||
return strBuffer; | return strBuffer; | ||
- | |||
} | } | ||
- | |||
- | |||
private StringBuffer extractImportDeclarations(JavaParser parser) { | private StringBuffer extractImportDeclarations(JavaParser parser) { | ||
- | |||
- | |||
- | |||
StringBuffer strBuffer = new StringBuffer(); | StringBuffer strBuffer = new StringBuffer(); | ||
- | |||
- | |||
ArrayList imports = parser.getImportDeclarations(); | ArrayList imports = parser.getImportDeclarations(); | ||
- | |||
if (imports == null) { | if (imports == null) { | ||
- | |||
return strBuffer; | return strBuffer; | ||
- | |||
} | } | ||
for (int i = 0; i < imports.size(); i++) //add import declarations as keyword | for (int i = 0; i < imports.size(); i++) //add import declarations as keyword | ||
- | |||
{ | { | ||
- | |||
strBuffer.append((String) imports.get(i)); | strBuffer.append((String) imports.get(i)); | ||
- | |||
} | } | ||
- | |||
- | |||
return strBuffer; | return strBuffer; | ||
- | |||
} | } | ||
private StringBuffer extractComments(JavaParser parser) { | private StringBuffer extractComments(JavaParser parser) { | ||
- | |||
StringBuffer strBuffer = new StringBuffer(); | StringBuffer strBuffer = new StringBuffer(); | ||
- | |||
ArrayList comments = parser.getComments(); | ArrayList comments = parser.getComments(); | ||
- | |||
if (comments == null) { | if (comments == null) { | ||
- | |||
return strBuffer; | return strBuffer; | ||
- | |||
} | } | ||
for (int i = 0; i < comments.size(); i++) { | for (int i = 0; i < comments.size(); i++) { | ||
- | |||
String docComment = (String) comments.get(i); | String docComment = (String) comments.get(i); | ||
- | |||
strBuffer.append(docComment); | strBuffer.append(docComment); | ||
- | |||
} | } | ||
return strBuffer; | return strBuffer; | ||
- | |||
} | } | ||
- | } </div>'' | + | } |
+ | </code> | ||
+ | Klasse ''JavaParser.java'': | ||
- | *''JavaParser.java'' | + | <code java> |
+ | package net.sf.regain.crawler.preparator.java; | ||
- | '' | + | import java.util.ArrayList; |
- | + | import java.util.List; | |
- | package net.sf.regain.crawler.preparator.java; | + | import java.util.ListIterator; |
- | + | import net.sf.regain.RegainException; | |
- | import java.util.ArrayList; | + | import org.eclipse.jdt.core.dom.*; |
- | + | ||
- | import java.util.List; | + | |
- | + | ||
- | import java.util.ListIterator; | + | |
- | + | ||
- | import net.sf.regain.RegainException; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.AST; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.ASTParser; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.Block; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.CompilationUnit; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.ImportDeclaration; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.Javadoc; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.MethodDeclaration; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.SimpleType; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.Type; | + | |
- | + | ||
- | import org.eclipse.jdt.core.dom.TypeDeclaration; | + | |
- | + | ||
- | public class JavaParser { | + | |
+ | public class JavaParser { | ||
private ASTParser _parser = ASTParser.newParser(AST.JLS3); | private ASTParser _parser = ASTParser.newParser(AST.JLS3); | ||
- | |||
private CompilationUnit _unit = null; | private CompilationUnit _unit = null; | ||
- | |||
private JClass _class = null; | private JClass _class = null; | ||
- | |||
- | |||
public void setSource(String sourceStr) throws RegainException { | public void setSource(String sourceStr) throws RegainException { | ||
- | |||
- | |||
- | |||
try { | try { | ||
- | |||
_parser.setKind(ASTParser.K_COMPILATION_UNIT); | _parser.setKind(ASTParser.K_COMPILATION_UNIT); | ||
- | |||
_parser.setSource(sourceStr.toString().toCharArray()); | _parser.setSource(sourceStr.toString().toCharArray()); | ||
- | |||
_unit = (CompilationUnit) _parser.createAST(null); | _unit = (CompilationUnit) _parser.createAST(null); | ||
- | |||
- | |||
- | |||
} catch (Exception ex) { | } catch (Exception ex) { | ||
- | |||
throw new RegainException("Error parsing Java file", ex); | throw new RegainException("Error parsing Java file", ex); | ||
- | |||
} | } | ||
- | |||
- | |||
- | |||
} | } | ||
- | |||
- | |||
public ArrayList getImportDeclarations() { | public ArrayList getImportDeclarations() { | ||
- | |||
List imports = _unit.imports(); | List imports = _unit.imports(); | ||
- | |||
if (imports.size() == 0) { | if (imports.size() == 0) { | ||
- | |||
return null; | return null; | ||
- | |||
} | } | ||
ArrayList importDecl = new ArrayList(); | ArrayList importDecl = new ArrayList(); | ||
- | |||
ListIterator iter = imports.listIterator(); | ListIterator iter = imports.listIterator(); | ||
- | |||
while (iter.hasNext()) { | while (iter.hasNext()) { | ||
- | |||
ImportDeclaration decl = (ImportDeclaration) iter.next(); | ImportDeclaration decl = (ImportDeclaration) iter.next(); | ||
- | |||
importDecl.add(decl.getName().toString()); | importDecl.add(decl.getName().toString()); | ||
- | |||
} | } | ||
return importDecl; | return importDecl; | ||
- | |||
} | } | ||
- | |||
- | |||
public ArrayList getComments() { | public ArrayList getComments() { | ||
- | |||
List comments = _unit.getCommentList(); | List comments = _unit.getCommentList(); | ||
- | |||
if (comments.size() == 0) { | if (comments.size() == 0) { | ||
- | |||
return null; | return null; | ||
- | |||
} | } | ||
ArrayList javaDocComments = new ArrayList(); | ArrayList javaDocComments = new ArrayList(); | ||
- | |||
ListIterator iterator = comments.listIterator(); | ListIterator iterator = comments.listIterator(); | ||
- | |||
while (iterator.hasNext()) { | while (iterator.hasNext()) { | ||
- | |||
Object object = iterator.next(); | Object object = iterator.next(); | ||
- | |||
if (object instanceof Javadoc) { | if (object instanceof Javadoc) { | ||
- | |||
String comment = ((Javadoc) object).getComment(); | String comment = ((Javadoc) object).getComment(); | ||
- | |||
- | |||
javaDocComments.add(comment); | javaDocComments.add(comment); | ||
- | |||
} | } | ||
- | |||
} | } | ||
return javaDocComments; | return javaDocComments; | ||
- | |||
} | } | ||
- | |||
- | |||
public JClass getDeclaredClass() { | public JClass getDeclaredClass() { | ||
- | |||
List types = _unit.types(); | List types = _unit.types(); | ||
- | |||
ListIterator typeIter = types.listIterator(0); | ListIterator typeIter = types.listIterator(0); | ||
- | |||
if (typeIter.hasNext()) { | if (typeIter.hasNext()) { | ||
- | |||
TypeDeclaration object = (TypeDeclaration) typeIter.next(); | TypeDeclaration object = (TypeDeclaration) typeIter.next(); | ||
- | |||
_class = new JClass(); | _class = new JClass(); | ||
- | |||
setClassInformation(_class, object); | setClassInformation(_class, object); | ||
- | |||
return _class; | return _class; | ||
- | |||
} | } | ||
- | |||
return null; | return null; | ||
- | |||
} | } | ||
- | |||
- | |||
private void setClassInformation(JClass cls, TypeDeclaration object) { | private void setClassInformation(JClass cls, TypeDeclaration object) { | ||
- | |||
cls.setIsInterface(object.isInterface()); | cls.setIsInterface(object.isInterface()); | ||
- | |||
cls.setClassName(object.getName().getIdentifier()); | cls.setClassName(object.getName().getIdentifier()); | ||
- | |||
- | |||
SimpleType _superClass = (SimpleType) object.getSuperclassType(); | SimpleType _superClass = (SimpleType) object.getSuperclassType(); | ||
- | |||
if (_superClass != null) { | if (_superClass != null) { | ||
- | |||
cls.setSuperClass(_superClass.getName().getFullyQualifiedName()); | cls.setSuperClass(_superClass.getName().getFullyQualifiedName()); | ||
- | |||
} | } | ||
- | |||
- | |||
List interfaceLst = object.superInterfaceTypes(); | List interfaceLst = object.superInterfaceTypes(); | ||
- | |||
- | |||
ListIterator interfaces = interfaceLst.listIterator(); | ListIterator interfaces = interfaceLst.listIterator(); | ||
- | |||
while (interfaces.hasNext()) { | while (interfaces.hasNext()) { | ||
- | + | SimpleType sin = (SimpleType) interfaces.next(); | |
- | { | + | cls.getInterfaces().add(sin.toString()); |
- | + | ||
- | SimpleType sin = (SimpleType) interfaces.next(); | + | |
- | + | ||
- | cls.getInterfaces().add(sin.toString()); | + | |
- | + | ||
- | } | + | |
- | + | ||
- | + | ||
} | } | ||
addMethods(cls, object); | addMethods(cls, object); | ||
- | |||
- | |||
TypeDeclaration[] innerTypes = object.getTypes(); | TypeDeclaration[] innerTypes = object.getTypes(); | ||
- | |||
for (int i = 0; i < innerTypes.length; i++) { | for (int i = 0; i < innerTypes.length; i++) { | ||
- | |||
JClass innerCls = new JClass(); | JClass innerCls = new JClass(); | ||
- | |||
setClassInformation(innerCls, innerTypes[i]); | setClassInformation(innerCls, innerTypes[i]); | ||
- | |||
cls.getInnerClasses().add(innerCls); | cls.getInnerClasses().add(innerCls); | ||
- | |||
} | } | ||
- | |||
} | } | ||
private void addMethods(JClass cls, TypeDeclaration object) { | private void addMethods(JClass cls, TypeDeclaration object) { | ||
- | |||
MethodDeclaration[] met = object.getMethods(); | MethodDeclaration[] met = object.getMethods(); | ||
- | |||
for (int i = 0; i < met.length; i++) { | for (int i = 0; i < met.length; i++) { | ||
- | |||
MethodDeclaration dec = met[i]; | MethodDeclaration dec = met[i]; | ||
- | |||
JMethod method = new JMethod(); | JMethod method = new JMethod(); | ||
- | |||
method.setMethodName(dec.getName().toString()); | method.setMethodName(dec.getName().toString()); | ||
- | |||
Type returnType = dec.getReturnType2(); | Type returnType = dec.getReturnType2(); | ||
- | |||
if (returnType != null) { | if (returnType != null) { | ||
- | |||
method.setReturnType(returnType.toString()); | method.setReturnType(returnType.toString()); | ||
- | |||
} | } | ||
- | |||
Block d = dec.getBody(); | Block d = dec.getBody(); | ||
- | |||
if (d == null) { | if (d == null) { | ||
- | |||
continue; | continue; | ||
- | |||
} | } | ||
- | |||
method.setCodeBlock(d.toString()); | method.setCodeBlock(d.toString()); | ||
- | |||
List param = dec.parameters(); | List param = dec.parameters(); | ||
- | |||
ListIterator paramList = param.listIterator(); | ListIterator paramList = param.listIterator(); | ||
- | |||
while (paramList.hasNext()) { | while (paramList.hasNext()) { | ||
- | |||
SingleVariableDeclaration sin = (SingleVariableDeclaration) paramList.next(); | SingleVariableDeclaration sin = (SingleVariableDeclaration) paramList.next(); | ||
- | |||
method.getParameters().add(sin.getType().toString()); | method.getParameters().add(sin.getType().toString()); | ||
- | |||
} | } | ||
- | |||
cls.getMethodDeclarations().add(method); | cls.getMethodDeclarations().add(method); | ||
- | |||
} | } | ||
- | |||
} | } | ||
- | + | } | |
- | }'' | + | </code> |
- | *''JClass.java'' | + | Klasse ''JClass.java'': |
- | ''package net.sf.regain.crawler.preparator.java; | + | <code java> |
+ | package net.sf.regain.crawler.preparator.java; | ||
- | import java.util.ArrayList; | + | import java.util.ArrayList; |
- | + | ||
- | public class JClass { | + | |
+ | public class JClass { | ||
private String className = null; | private String className = null; | ||
- | |||
private boolean isInterface = false; | private boolean isInterface = false; | ||
- | |||
private ArrayList methodDeclarations = new ArrayList(); | private ArrayList methodDeclarations = new ArrayList(); | ||
- | |||
private ArrayList innerClasses = new ArrayList(); | private ArrayList innerClasses = new ArrayList(); | ||
- | |||
private String superClass = null; | private String superClass = null; | ||
- | |||
private ArrayList interfaces = new ArrayList(); | private ArrayList interfaces = new ArrayList(); | ||
public String getClassName() { | public String getClassName() { | ||
- | |||
return className; | return className; | ||
- | |||
} | } | ||
public void setClassName(String className) { | public void setClassName(String className) { | ||
- | |||
this.className = className; | this.className = className; | ||
- | |||
} | } | ||
public boolean isInterface() { | public boolean isInterface() { | ||
- | |||
return isInterface; | return isInterface; | ||
- | |||
} | } | ||
public void setIsInterface(boolean isInterface) { | public void setIsInterface(boolean isInterface) { | ||
- | |||
this.isInterface = isInterface; | this.isInterface = isInterface; | ||
- | |||
} | } | ||
public ArrayList getMethodDeclarations() { | public ArrayList getMethodDeclarations() { | ||
- | |||
return methodDeclarations; | return methodDeclarations; | ||
- | |||
} | } | ||
public void setMethodDeclarations(ArrayList methodDeclarations) { | public void setMethodDeclarations(ArrayList methodDeclarations) { | ||
- | |||
this.methodDeclarations = methodDeclarations; | this.methodDeclarations = methodDeclarations; | ||
- | |||
} | } | ||
public ArrayList getInnerClasses() { | public ArrayList getInnerClasses() { | ||
- | |||
return innerClasses; | return innerClasses; | ||
- | |||
} | } | ||
public void setInnerClasses(ArrayList innerClasses) { | public void setInnerClasses(ArrayList innerClasses) { | ||
- | |||
this.innerClasses = innerClasses; | this.innerClasses = innerClasses; | ||
- | |||
} | } | ||
public String getSuperClass() { | public String getSuperClass() { | ||
- | |||
return superClass; | return superClass; | ||
- | |||
} | } | ||
public void setSuperClass(String superClass) { | public void setSuperClass(String superClass) { | ||
- | |||
this.superClass = superClass; | this.superClass = superClass; | ||
- | |||
} | } | ||
public ArrayList getInterfaces() { | public ArrayList getInterfaces() { | ||
- | |||
return interfaces; | return interfaces; | ||
- | |||
} | } | ||
public void setInterfaces(ArrayList interfaces) { | public void setInterfaces(ArrayList interfaces) { | ||
- | |||
this.interfaces = interfaces; | this.interfaces = interfaces; | ||
- | |||
} | } | ||
+ | } | ||
+ | </code> | ||
- | }'' | ||
+ | Klasse ''JMethod.java'': | ||
+ | <code java> | ||
+ | package net.sf.regain.crawler.preparator.java; | ||
- | *''JMethod.java'' | + | import java.util.ArrayList; |
- | + | ||
- | '' | + | |
- | + | ||
- | package net.sf.regain.crawler.preparator.java; | + | |
- | + | ||
- | import java.util.ArrayList; | + | |
- | + | ||
- | public class JMethod { | + | |
- | + | ||
+ | public class JMethod { | ||
private String methodName = null; | private String methodName = null; | ||
- | |||
private ArrayList parameters = new ArrayList(); | private ArrayList parameters = new ArrayList(); | ||
- | |||
private String codeBlock = null; | private String codeBlock = null; | ||
- | |||
private String returnType = null; | private String returnType = null; | ||
public String getMethodName() { | public String getMethodName() { | ||
- | |||
return methodName; | return methodName; | ||
- | |||
} | } | ||
public void setMethodName(String methodName) { | public void setMethodName(String methodName) { | ||
- | |||
this.methodName = methodName; | this.methodName = methodName; | ||
- | |||
} | } | ||
public ArrayList getParameters() { | public ArrayList getParameters() { | ||
- | |||
return parameters; | return parameters; | ||
- | |||
} | } | ||
public void setParameters(ArrayList parameters) { | public void setParameters(ArrayList parameters) { | ||
- | |||
this.parameters = parameters; | this.parameters = parameters; | ||
- | |||
} | } | ||
public String getCodeBlock() { | public String getCodeBlock() { | ||
- | |||
return codeBlock; | return codeBlock; | ||
- | |||
} | } | ||
public void setCodeBlock(String codeBlock) { | public void setCodeBlock(String codeBlock) { | ||
- | |||
this.codeBlock = codeBlock; | this.codeBlock = codeBlock; | ||
- | |||
} | } | ||
public String getReturnType() { | public String getReturnType() { | ||
- | |||
return returnType; | return returnType; | ||
- | |||
} | } | ||
public void setReturnType(String returnType) { | public void setReturnType(String returnType) { | ||
- | |||
this.returnType = returnType; | this.returnType = returnType; | ||
- | |||
} | } | ||
- | + | } | |
- | }'' | + | </code> |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | ;Änderungen im Quelltext: Alle Änderungen sind mit ... gekennzeichnet. Dies sind: | + | |
- | + | ||
- | Es wurden keine Klassen geändert, sondern nur neue hinzugefügt. | + | |
- | ;[ Diskussion zum Hack]: | + | ===== Siehe auch ===== |
+ | * [[:de:howto]] - Übersicht über alle How-Tos | ||
+ | * [[http://www.onjava.com/pub/a/onjava/2006/01/18/using-lucene-to-search-java-source.html?page=1|Using Lucene to Search Java Source Code]] | ||
+ | * [[http://www-128.ibm.com/developerworks/opensource/library/os-ast/|Exploring Eclipse's ASTParser]] | ||