You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2013/01/27 22:00:28 UTC
svn commit: r1439178 - in /uima/sandbox/uimafit/trunk: uimafit-maven-plugin/
uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/
uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/
uimafit-maven-plugin/src/main/java/org/apac...
Author: rec
Date: Sun Jan 27 21:00:28 2013
New Revision: 1439178
URL: http://svn.apache.org/viewvc?rev=1439178&view=rev
Log:
[UIMA-2599] Fill description of component and parameters from JavaDoc
[UIMA-2600] Fill vendor, version, etc. from Maven project
- uimafit: Removed Defaults class again and moved functionality into ResourceMetaDataFactory
- uimafit-maven-plugin: Moved extractor tests to correct package
- uimafit-maven-plugin: Fixed ComponentDescriptionExtractor
- uimafit-maven-plugin: Do not generate descriptors for abstract classes
- uimafit-maven-plugin: Handle certain JavaDoc tags when extracting text
Added:
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractorTest.java (with props)
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractorTest.java
- copied, changed from r1439070, uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/JavadocTextExtractorTest.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ParameterDescriptionExtractorTest.java
- copied, changed from r1439070, uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/ParameterDescriptionExtractorTest.java
Removed:
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/JavadocTextExtractorTest.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/ParameterDescriptionExtractorTest.java
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/Defaults.java
Modified:
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/EnhanceMojo.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractor.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractor.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/util/Util.java
uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/resources/TestComponent.java
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ResourceMetaDataFactory.java
uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ResourceMetaDataFactoryTest.java
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/pom.xml Sun Jan 27 21:00:28 2013
@@ -46,7 +46,9 @@
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
- <version>3.17.1-GA</version>
+ <version>3.16.1-GA</version>
+ <!-- 3.16.1-GA appears to be the last Java 5 version, see also
+ https://issues.apache.org/jira/browse/OGNL-230 -->
</dependency>
<!-- Dependencies on the Maven Plugin API and friends -->
@@ -81,7 +83,7 @@
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
- <version>3.2.0.666</version>
+ <version>3.3.0-v_771</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
@@ -89,20 +91,15 @@
<version>3.3.0-v20070423</version>
</dependency>
<dependency>
- <groupId>org.eclipse.core</groupId>
- <artifactId>runtime</artifactId>
- <version>3.3.100-v20070530</version>
+ <groupId>org.eclipse.equinox</groupId>
+ <artifactId>app</artifactId>
+ <version>1.0.0-v20070606</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>resources</artifactId>
<version>3.3.0-v20070604</version>
</dependency>
- <dependency>
- <groupId>org.eclipse.equinox</groupId>
- <artifactId>common</artifactId>
- <version>3.3.0-v20070426</version>
- </dependency>
</dependencies>
<build>
@@ -122,7 +119,7 @@
<goal>descriptor</goal>
</goals>
</execution>
- <!-- END WORKAROUND http://jira.codehaus.org/browse/MNG-5346 -->
+ <!-- END WORKAROUND http://jira.codehaus.org/browse/MNG-5346 -->
<execution>
<id>generated-helpmojo</id>
<goals>
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/EnhanceMojo.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/EnhanceMojo.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/EnhanceMojo.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/EnhanceMojo.java Sun Jan 27 21:00:28 2013
@@ -35,6 +35,7 @@ import javassist.bytecode.AnnotationsAtt
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.Annotation;
+import javassist.bytecode.annotation.MemberValue;
import javassist.bytecode.annotation.StringMemberValue;
import org.apache.commons.lang.exception.ExceptionUtils;
@@ -46,7 +47,10 @@ import org.apache.maven.plugins.annotati
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
+import org.apache.uima.fit.descriptor.ResourceMetaData;
import org.apache.uima.fit.factory.ConfigurationParameterFactory;
+import org.apache.uima.fit.factory.ResourceMetaDataFactory;
+import org.apache.uima.fit.maven.javadoc.ComponentDescriptionExtractor;
import org.apache.uima.fit.maven.javadoc.JavadocTextExtractor;
import org.apache.uima.fit.maven.javadoc.ParameterDescriptionExtractor;
import org.apache.uima.fit.maven.util.Util;
@@ -70,17 +74,23 @@ public class EnhanceMojo extends Abstrac
private ClassLoader componentLoader;
/**
+ * Override component description in generated descriptors.
+ */
+ @Parameter(defaultValue = "false", required = true)
+ private boolean overrideComponentDescription;
+
+ /**
* Override version in generated descriptors.
*
* @see #componentVersion
*/
- @Parameter(defaultValue="true", required=true)
+ @Parameter(defaultValue = "false", required = true)
private boolean overrideComponentVersion;
/**
* Version to use in generated descriptors.
*/
- @Parameter(defaultValue="${project.version}", required=false)
+ @Parameter(defaultValue = "${project.version}", required = false)
private String componentVersion;
/**
@@ -88,35 +98,35 @@ public class EnhanceMojo extends Abstrac
*
* @see #componentVendor
*/
- @Parameter(defaultValue="true", required=true)
+ @Parameter(defaultValue = "false", required = true)
private boolean overrideComponentVendor;
/**
* Vendor to use in generated descriptors.
*/
- @Parameter(defaultValue="${project.organization.name}", required=false)
+ @Parameter(defaultValue = "${project.organization.name}", required = false)
private String componentVendor;
/**
* Source file encoding.
*/
- @Parameter(defaultValue="${project.build.sourceEncoding}", required=true)
+ @Parameter(defaultValue = "${project.build.sourceEncoding}", required = true)
private String encoding;
- public void execute() throws MojoExecutionException {
+ public void execute() throws MojoExecutionException {
// Get the compiled classes from this project
String[] files = FileUtils.getFilesFromExtension(project.getBuild().getOutputDirectory(),
new String[] { "class" });
componentLoader = Util.getClassloader(project, getLog());
-
+
// Set up class pool with all the project dependencies and the project classes themselves
ClassPool classPool = new ClassPool(true);
classPool.appendClassPath(new LoaderClassPath(componentLoader));
for (String file : files) {
String clazzName = Util.getClassName(project, file);
-
+
// Check if this is a UIMA component
Class clazz;
try {
@@ -124,122 +134,209 @@ public class EnhanceMojo extends Abstrac
if (!Util.isComponent(componentLoader, clazz)) {
continue;
}
- }
- catch (ClassNotFoundException e) {
+ } catch (ClassNotFoundException e) {
getLog().warn("Cannot analyze class [" + clazzName + "]", e);
continue;
- }
-
+ }
+
// Get the Javassist class
CtClass ctClazz;
try {
ctClazz = classPool.get(clazzName);
-// if (ctClazz.isFrozen()) {
-// getLog().info("Ignoring frozen class [" + clazzName + "]");
-// continue;
-// }
+ } catch (NotFoundException e) {
+ throw new MojoExecutionException("Class [" + clazzName + "] not found in class pool: "
+ + ExceptionUtils.getRootCauseMessage(e), e);
}
- catch(NotFoundException e) {
- throw new MojoExecutionException("Class [" + clazzName + "] not found in class pool: " +
- ExceptionUtils.getRootCauseMessage(e), e);
- }
- ClassFile classFile = ctClazz.getClassFile();
- ConstPool constPool = classFile.getConstPool();
-
+
// Get the source file
String sourceFile = getSourceFile(clazzName);
-
- // Get the parameter name constants
- Map<String, Field> nameFields = getParameterConstants(clazz);
-
+
// Try to extract parameter descriptions from JavaDoc in source file
if (sourceFile != null) {
getLog().info("Enhancing class [" + clazzName + "]");
-
+
// Parse source file so we can extract the JavaDoc
CompilationUnit ast = parseSource(sourceFile);
-
- // Fetch configuration parameters from the @ConfigurationParameter annotations in the
- // compiled class
-
- for (Field field : ReflectionUtil.getFields(clazz)) {
- // Is this a configuration parameter?
- if (!ConfigurationParameterFactory.isConfigurationParameterField(field)) {
- continue;
- }
-// // Extract configuration parameter information from the uimaFIT annotation
-// // We only want to override if the description is not set yet.
- ConfigurationParameter p = ConfigurationParameterFactory.createPrimitiveParameter(field);
-// if (StringUtils.isNotBlank(p.getDescription())) {
-// continue;
-// }
-
- // Extract JavaDoc for this parameter from the source file
- String pdesc = getParameterDocumentation(ast, field, nameFields.get(p.getName()));
- if (pdesc == null) {
- getLog().warn("No description found for parameter [" + p.getName() + "]");
- continue;
- }
-
- // Update the "description" field of the annotation
- try {
- CtField ctField = ctClazz.getField(field.getName());
- AnnotationsAttribute annoAttr = (AnnotationsAttribute) ctField.getFieldInfo()
- .getAttribute(AnnotationsAttribute.visibleTag);
-
- // Locate and update annotation
- if (annoAttr != null) {
- Annotation[] annotations = annoAttr.getAnnotations();
- for (Annotation a : annotations) {
- if (a.getTypeName().equals("org.apache.uima.fit.descriptor.ConfigurationParameter")
- || a.getTypeName().equals("org.uimafit.descriptor.ConfigurationParameter")) {
- if (a.getMemberValue("description") == null) {
- a.addMemberValue("description", new StringMemberValue(pdesc, constPool));
- getLog().info("Enhanced description of parameter [" + p.getName() + "]");
- // Replace updated annotation
- annoAttr.addAnnotation(a);
- } else {
- getLog().info(
- "Not enhancing parameter [" + p.getName()
- + "] which already has a description");
+ // Enhance meta data
+ enhanceResourceMetaData(ast, clazz, ctClazz);
- }
- }
- }
- }
-
- // Replace annotations
- ctField.getFieldInfo().addAttribute(annoAttr);
- } catch (NotFoundException e) {
- throw new MojoExecutionException("Field [" + field.getName()
- + "] not found in byte code: " + ExceptionUtils.getRootCauseMessage(e), e);
- }
- }
+ // Enhance configuration parameters
+ enhanceConfigurationParameter(ast, clazz, ctClazz);
} else {
getLog().warn("No source file found for class [" + clazzName + "]");
}
-
+
try {
ctClazz.writeFile(project.getBuild().getOutputDirectory());
- }
- catch (IOException e) {
- throw new MojoExecutionException("Enhanced class [" + clazzName + "] cannot be written: " +
- ExceptionUtils.getRootCauseMessage(e), e);
-
+ } catch (IOException e) {
+ throw new MojoExecutionException("Enhanced class [" + clazzName + "] cannot be written: "
+ + ExceptionUtils.getRootCauseMessage(e), e);
+
} catch (CannotCompileException e) {
- throw new MojoExecutionException("Enhanced class [" + clazzName + "] cannot be compiled: " +
- ExceptionUtils.getRootCauseMessage(e), e);
+ throw new MojoExecutionException("Enhanced class [" + clazzName + "] cannot be compiled: "
+ + ExceptionUtils.getRootCauseMessage(e), e);
}
}
}
+
+ /**
+ * Enhance resource meta data
+ */
+ private void enhanceResourceMetaData(CompilationUnit aAST, Class aClazz, CtClass aCtClazz) throws MojoExecutionException {
+ String doc = getComponnetDocumentation(aAST, aClazz);
+
+ if (doc == null) {
+ getLog().warn("No description found for component [" + aClazz.getName() + "]");
+ return;
+ }
+
+ ClassFile classFile = aCtClazz.getClassFile();
+ ConstPool constPool = classFile.getConstPool();
+
+ AnnotationsAttribute annoAttr = (AnnotationsAttribute) classFile.getAttribute(
+ AnnotationsAttribute.visibleTag);
+
+ // Create annotation attribute if it does not exist
+ if (annoAttr == null) {
+ annoAttr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
+ }
+
+ // Create annotation if it does not exist
+ Annotation a = annoAttr.getAnnotation(ResourceMetaData.class.getName());
+ if (a == null) {
+ a = new Annotation(ResourceMetaData.class.getName(), constPool);
+ }
+
+ // Update description from JavaDoc if none is set
+ enhanceMemberValue(a, "description", doc, overrideComponentDescription,
+ ResourceMetaDataFactory.getDefaultDescription(aClazz), constPool);
+
+ // Update version if none is set
+ enhanceMemberValue(a, "version", componentVersion, overrideComponentVersion,
+ ResourceMetaDataFactory.getDefaultVersion(aClazz), constPool);
+
+ // Update vendor if none is set
+ enhanceMemberValue(a, "vendor", componentVendor, overrideComponentVendor,
+ ResourceMetaDataFactory.getDefaultVendor(aClazz), constPool);
+
+ // Replace annotation
+ annoAttr.addAnnotation(a);
+
+ // Replace annotation attribute
+ classFile.addAttribute(annoAttr);
+ }
+
+ /**
+ * Set a annotation member value if no value is present, if the present value is the default
+ * generated by uimaFIT or if a override is active.
+ *
+ * @param aAnnotation
+ * an annotation
+ * @param aName
+ * the name of the member value
+ * @param aNewValue
+ * the value to set
+ * @param aOverride
+ * set value even if it is already set
+ * @param aDefault
+ * default value set by uimaFIT - if the member has this value, it is considered unset
+ */
+ private void enhanceMemberValue(Annotation aAnnotation, String aName, String aNewValue,
+ boolean aOverride, String aDefault, ConstPool aConstPool) {
+ String value = getStringMemberValue(aAnnotation, aName);
+ boolean isEmpty = value.length() == 0;
+ boolean isDefault = value.equals(aDefault);
+
+ if ((aNewValue != null) && (isEmpty || isDefault || aOverride)) {
+ aAnnotation.addMemberValue(aName, new StringMemberValue(aNewValue, aConstPool));
+ getLog().info("Enhanced component meta data [" + aName + "]");
+ } else {
+ getLog().info("Not overwriting component meta data [" + aName + "]");
+ }
+ }
+ private String getStringMemberValue(Annotation aAnnotation, String aValue)
+ {
+ MemberValue v = aAnnotation.getMemberValue(aValue);
+ if (v == null) {
+ return "";
+ }
+ else {
+ return ((StringMemberValue) v).getValue();
+ }
+ }
+
+ /**
+ * Enhance descriptions in configuration parameters.
+ */
+ private void enhanceConfigurationParameter(CompilationUnit aAST, Class aClazz, CtClass aCtClazz)
+ throws MojoExecutionException {
+ // Get the parameter name constants
+ Map<String, Field> nameFields = getParameterConstants(aClazz);
+
+ // Fetch configuration parameters from the @ConfigurationParameter annotations in the
+ // compiled class
+ for (Field field : ReflectionUtil.getFields(aClazz)) {
+ // Is this a configuration parameter?
+ if (!ConfigurationParameterFactory.isConfigurationParameterField(field)) {
+ continue;
+ }
+
+ // Extract configuration parameter information from the uimaFIT annotation
+ ConfigurationParameter p = ConfigurationParameterFactory.createPrimitiveParameter(field);
+
+ // Extract JavaDoc for this parameter from the source file
+ String pdesc = getParameterDocumentation(aAST, field, nameFields.get(p.getName()));
+ if (pdesc == null) {
+ getLog().warn("No description found for parameter [" + p.getName() + "]");
+ continue;
+ }
+
+ // Update the "description" field of the annotation
+ try {
+ CtField ctField = aCtClazz.getField(field.getName());
+ AnnotationsAttribute annoAttr = (AnnotationsAttribute) ctField.getFieldInfo().getAttribute(
+ AnnotationsAttribute.visibleTag);
+
+ // Locate and update annotation
+ if (annoAttr != null) {
+ Annotation[] annotations = annoAttr.getAnnotations();
+
+ // Update existing annotation
+ for (Annotation a : annotations) {
+ if (a.getTypeName().equals(
+ org.apache.uima.fit.descriptor.ConfigurationParameter.class.getName())
+ || a.getTypeName().equals("org.uimafit.descriptor.ConfigurationParameter")) {
+ if (a.getMemberValue("description") == null) {
+ a.addMemberValue("description", new StringMemberValue(pdesc, aCtClazz
+ .getClassFile().getConstPool()));
+ getLog().info("Enhanced description of parameter [" + p.getName() + "]");
+ // Replace updated annotation
+ annoAttr.addAnnotation(a);
+ } else {
+ // Extract configuration parameter information from the uimaFIT annotation
+ // We only want to override if the description is not set yet.
+ getLog().info("Not overwriting description of parameter [" + p.getName() + "] ");
+ }
+ }
+ }
+ }
+
+ // Replace annotations
+ ctField.getFieldInfo().addAttribute(annoAttr);
+ } catch (NotFoundException e) {
+ throw new MojoExecutionException("Field [" + field.getName() + "] not found in byte code: "
+ + ExceptionUtils.getRootCauseMessage(e), e);
+ }
+ }
+ }
+
/**
* Get a map of parameter name to parameter name constant field, e.g. ("value",
* Field("PARAM_VALUE")).
*/
- private Map<String, Field> getParameterConstants(Class aClazz)
- {
+ private Map<String, Field> getParameterConstants(Class aClazz) {
Map<String, Field> result = new HashMap<String, Field>();
for (Field f : aClazz.getFields()) {
if (!f.getName().startsWith("PARAM_")) {
@@ -248,8 +345,7 @@ public class EnhanceMojo extends Abstrac
try {
String parameterName = (String) f.get(null);
result.put(parameterName, f);
- }
- catch (IllegalAccessException e) {
+ } catch (IllegalAccessException e) {
getLog().warn(
"Unable to access parameter name constant field [" + f.getName() + "]: "
+ ExceptionUtils.getRootCauseMessage(e), e);
@@ -257,13 +353,11 @@ public class EnhanceMojo extends Abstrac
}
return result;
}
-
- private CompilationUnit parseSource(String aSourceFile) throws MojoExecutionException
- {
+
+ private CompilationUnit parseSource(String aSourceFile) throws MojoExecutionException {
try {
return Util.parseSource(aSourceFile, encoding);
- }
- catch (IOException e) {
+ } catch (IOException e) {
throw new MojoExecutionException("Unable to parse source file [" + aSourceFile + "]: "
+ ExceptionUtils.getRootCauseMessage(e), e);
}
@@ -274,12 +368,10 @@ public class EnhanceMojo extends Abstrac
*
* @return The path to the source file or {@code null} if no source file was found.
*/
- private String getSourceFile(String aClassName)
- {
+ private String getSourceFile(String aClassName) {
String sourceName = aClassName.replace('.', '/') + ".java";
-
- for (String root : (List<String>) project.getCompileSourceRoots())
- {
+
+ for (String root : (List<String>) project.getCompileSourceRoots()) {
File f = new File(root, sourceName);
if (f.exists()) {
return f.getPath();
@@ -287,11 +379,31 @@ public class EnhanceMojo extends Abstrac
}
return null;
}
-
+
+ private String getComponnetDocumentation(CompilationUnit aAst, Class aComponentType) {
+ if (aComponentType.getName().contains("$")) {
+ // rec 2013-01-27: see comment on bindings resolving in ComponentDescriptionExtractor
+ getLog().warn(
+ "Inner classes not supported. Component description for [" + aComponentType.getName()
+ + "] cannot be extracted. ");
+ return null;
+ }
+
+ ComponentDescriptionExtractor visitor = new ComponentDescriptionExtractor(
+ aComponentType.getName());
+ aAst.accept(visitor);
+
+ if (visitor.getJavadoc() != null) {
+ JavadocTextExtractor textExtractor = new JavadocTextExtractor();
+ visitor.getJavadoc().accept(textExtractor);
+ return textExtractor.getText();
+ } else {
+ return null;
+ }
+ }
+
private String getParameterDocumentation(CompilationUnit aAst, Field aParameter,
Field aParameterNameConstant) {
-
- // Generate JavaDoc related annotations
ParameterDescriptionExtractor visitor = new ParameterDescriptionExtractor(aParameter.getName(),
(aParameterNameConstant != null) ? aParameterNameConstant.getName() : null);
aAst.accept(visitor);
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java Sun Jan 27 21:00:28 2013
@@ -22,6 +22,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.lang.reflect.Modifier;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.AbstractMojo;
@@ -83,6 +84,12 @@ public class GenerateDescriptorsMojo ext
String clazzName = clazzPath.replace("/", ".");
try {
Class clazz = componentLoader.loadClass(clazzName);
+
+ // Do not generate descriptors for abstract classes, they cannot be instantiated.
+ if (Modifier.isAbstract(clazz.getModifiers())) {
+ continue;
+ }
+
ResourceCreationSpecifier desc = null;
switch (Util.getType(componentLoader, clazz)) {
case ANALYSIS_ENGINE:
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractor.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractor.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractor.java Sun Jan 27 21:00:28 2013
@@ -19,21 +19,20 @@
package org.apache.uima.fit.maven.javadoc;
import org.eclipse.jdt.core.dom.ASTVisitor;
-import org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Javadoc;
-import org.eclipse.jdt.core.dom.StringLiteral;
-import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
/**
* Find JavaDoc for a given uimaFIT parameter.
*/
public class ComponentDescriptionExtractor extends ASTVisitor {
- private String parameterName;
+ private String className;
private Javadoc javadoc;
- public ComponentDescriptionExtractor(String aParameterName) {
- parameterName = aParameterName;
+ public ComponentDescriptionExtractor(String aClassName) {
+ className = aClassName;
}
public Javadoc getJavadoc() {
@@ -41,19 +40,25 @@ public class ComponentDescriptionExtract
}
@Override
- public boolean visit(FieldDeclaration aNode) {
- if ((!aNode.fragments().isEmpty())
- && (aNode.fragments().get(0) instanceof VariableDeclarationFragment)) {
- VariableDeclarationFragment f = (VariableDeclarationFragment) aNode.fragments().get(0);
- if (f.getName().getIdentifier().startsWith("PARAM_")
- && (f.getInitializer() instanceof StringLiteral)) {
- String name = f.getName().getIdentifier();
- String value = ((StringLiteral) f.getInitializer()).getLiteralValue();
- if (parameterName.equals(value)) {
- javadoc = aNode.getJavadoc();
- }
- }
+ public boolean visit(TypeDeclaration aNode) {
+ StringBuilder name = new StringBuilder();
+
+ // rec 2013-01-27: This should work, but for some reason resolveBinding() returns null even
+ // though binding resolving and binding recovery are both enabled in the parser.
+ // name = aNode.resolveBinding().getQualifiedName();
+
+ // Different approach to try and get the qualified name at least for the top-level class
+ CompilationUnit root = (CompilationUnit) aNode.getRoot();
+ if (root.getPackage() != null) {
+ name.append(root.getPackage().getName());
+ name.append('.');
}
- return false;
+ name.append(aNode.getName().getIdentifier());
+
+ if (name.toString().equals(className)) {
+ javadoc = aNode.getJavadoc();
+ }
+
+ return true;
}
}
\ No newline at end of file
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractor.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractor.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractor.java Sun Jan 27 21:00:28 2013
@@ -20,6 +20,8 @@ package org.apache.uima.fit.maven.javado
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Javadoc;
+import org.eclipse.jdt.core.dom.SimpleName;
+import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TextElement;
/**
@@ -30,14 +32,22 @@ public class JavadocTextExtractor extend
@Override
public boolean visit(TextElement aNode) {
- // In multi-line JavaDoc, each line is parsed as a separate node. We have to insert something
- // (a line break or space) to prevent gluing words together.
- if (text.length() > 0 && text.charAt(text.length()-1) != ' ') {
- text.append(' ');
- }
- text.append(aNode.getText());
+ append(aNode.getText());
+ return true;
+ }
+
+ @Override
+ public boolean visit(SimpleName aNode) {
+ append(aNode.getIdentifier());
return true;
}
+
+ @Override
+ public boolean visit(TagElement aNode) {
+ // Ignore any tags other then these.
+ String tn = aNode.getTagName();
+ return tn == null || "@link".equals(tn) || "@code".equals(tn);
+ }
@Override
public boolean visit(Javadoc aNode) {
@@ -47,4 +57,16 @@ public class JavadocTextExtractor extend
public String getText() {
return text.toString();
}
+
+ /**
+ * In multi-line JavaDoc, each line is parsed as a separate node. We have to insert something
+ * (a line break or space) to prevent gluing words together. This method adds such spaces.
+ */
+ private void append(String aText)
+ {
+ if (text.length() > 0 && text.charAt(text.length()-1) != ' ') {
+ text.append(' ');
+ }
+ text.append(aText);
+ }
}
\ No newline at end of file
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/util/Util.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/util/Util.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/util/Util.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/util/Util.java Sun Jan 27 21:00:28 2013
@@ -47,6 +47,9 @@ public final class Util {
public static CompilationUnit parseSource(String aSourceFile, String aEncoding)
throws IOException {
ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setKind (ASTParser.K_COMPILATION_UNIT);
+ parser.setResolveBindings(true);
+ parser.setBindingsRecovery(true);
parser.setSource(org.apache.commons.io.FileUtils.readFileToString(new File(aSourceFile),
aEncoding).toCharArray());
Map options = JavaCore.getOptions();
Added: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractorTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractorTest.java?rev=1439178&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractorTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractorTest.java Sun Jan 27 21:00:28 2013
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.fit.maven.javadoc;
+
+import org.apache.uima.fit.maven.util.Util;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class ComponentDescriptionExtractorTest {
+
+ @Test
+ public void test() throws Exception {
+ // Create the Java parser and parse the source code into an abstract syntax tree
+ CompilationUnit result = Util.parseSource("src/test/resources/TestComponent.java", "UTF-8");
+
+ ComponentDescriptionExtractor ex = new ComponentDescriptionExtractor(
+ "some.test.mypackage.TestComponent");
+ result.accept(ex);
+
+ assertNotNull(ex.getJavadoc());
+
+ JavadocTextExtractor textEx = new JavadocTextExtractor();
+ ex.getJavadoc().accept(textEx);
+
+ assertEquals("A test component used to test JavadocTextExtractor .", textEx.getText());
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ComponentDescriptionExtractorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Copied: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractorTest.java (from r1439070, uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/JavadocTextExtractorTest.java)
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractorTest.java?p2=uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractorTest.java&p1=uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/JavadocTextExtractorTest.java&r1=1439070&r2=1439178&rev=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/JavadocTextExtractorTest.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/JavadocTextExtractorTest.java Sun Jan 27 21:00:28 2013
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.uima.fit.maven;
+package org.apache.uima.fit.maven.javadoc;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
Copied: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ParameterDescriptionExtractorTest.java (from r1439070, uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/ParameterDescriptionExtractorTest.java)
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ParameterDescriptionExtractorTest.java?p2=uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ParameterDescriptionExtractorTest.java&p1=uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/ParameterDescriptionExtractorTest.java&r1=1439070&r2=1439178&rev=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/ParameterDescriptionExtractorTest.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/java/org/apache/uima/fit/maven/javadoc/ParameterDescriptionExtractorTest.java Sun Jan 27 21:00:28 2013
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.uima.fit.maven;
+package org.apache.uima.fit.maven.javadoc;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Modified: uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/resources/TestComponent.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/resources/TestComponent.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/resources/TestComponent.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-maven-plugin/src/test/resources/TestComponent.java Sun Jan 27 21:00:28 2013
@@ -16,10 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
+package some.test.mypackage;
+
import org.apache.uima.fit.descriptor.ConfigurationParameter;
+import org.apache.uima.fit.maven.javadoc.JavadocTextExtractor;
/**
- * A test component.
+ * A test component used to test {@link JavadocTextExtractor}.
+ *
+ * @author some author
*/
public class TestComponent {
/**
Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ResourceMetaDataFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ResourceMetaDataFactory.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ResourceMetaDataFactory.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ResourceMetaDataFactory.java Sun Jan 27 21:00:28 2013
@@ -45,12 +45,11 @@ public final class ResourceMetaDataFacto
if (componentAnno == null) {
// Default handling if no annotation is present.
- if (aComponentClass.getPackage() != null) {
- aMetaData.setVendor(aComponentClass.getPackage().getName());
- }
- aMetaData.setName(aComponentClass.getName());
- aMetaData.setDescription(Defaults.DEFAULT_DESCRIPTION);
- aMetaData.setVersion(Defaults.DEFAULT_VERSION);
+ aMetaData.setCopyright(getDefaultCopyright(aComponentClass));
+ aMetaData.setDescription(getDefaultDescription(aComponentClass));
+ aMetaData.setName(getDefaultName(aComponentClass));
+ aMetaData.setVendor(getDefaultVendor(aComponentClass));
+ aMetaData.setVersion(getDefaultVersion(aComponentClass));
} else {
// If annotation is present, use it
// Annotation values cannot be null, but we want to avoid empty strings in the meta data,
@@ -63,6 +62,73 @@ public final class ResourceMetaDataFacto
}
}
+ /**
+ * Used when the version of a component is unknown.
+ */
+ private static final String DEFAULT_VERSION = "unknown";
+
+ /**
+ * Used when the description of a component is unknown.
+ */
+ private static final String DEFAULT_DESCRIPTION = "Descriptor automatically generated by uimaFIT";
+
+ /**
+ * Get the default copyright of a component class.
+ */
+ public static String getDefaultCopyright(Class<?> aComponentClass)
+ {
+ // rec 2013-01-27: Basically just here for completeness - no idea where to one could obtain
+ // a copyright information for the class. Possibly from some LICENSE file in the JAR which
+ // contains the class.
+ return null;
+ }
+
+ /**
+ * Get the default version of a component class.
+ */
+ public static String getDefaultVersion(Class<?> aComponentClass)
+ {
+ // TODO This method could try to obtain a version from the package of the component
+ // aComponentClass.getPackage().getImplementationVersion()
+ return DEFAULT_VERSION;
+ }
+
+ /**
+ * Get the default description of a component class.
+ */
+ public static String getDefaultDescription(Class<?> aComponentClass)
+ {
+ return DEFAULT_DESCRIPTION;
+ }
+
+ /**
+ * Get the default vendor of a component class.
+ *
+ * @return the package name of the component, if the component is in a package, otherwise
+ * {@code null}.
+ */
+ public static String getDefaultVendor(Class<?> aComponentClass)
+ {
+ // TODO This method could try to obtain a vendor from the package of the component
+ // aComponentClass.getPackage().getImplementationVendor()
+ if (aComponentClass.getPackage() != null) {
+ return aComponentClass.getPackage().getName();
+ }
+ else {
+ return null;
+ }
+ }
+
+ /**
+ * Get the default name of a component class.
+ *
+ * @return the fully qualified name of the class.
+ */
+ public static String getDefaultName(Class<?> aComponentClass)
+ {
+ return aComponentClass.getName();
+ }
+
private static String emptyAsNull(String aString) {
if (aString == null || aString.length() == 0) {
return null;
Modified: uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ResourceMetaDataFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ResourceMetaDataFactoryTest.java?rev=1439178&r1=1439177&r2=1439178&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ResourceMetaDataFactoryTest.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ResourceMetaDataFactoryTest.java Sun Jan 27 21:00:28 2013
@@ -30,9 +30,9 @@ public class ResourceMetaDataFactoryTest
public void testWithMetaData() {
org.apache.uima.resource.metadata.ResourceMetaData meta = UIMAFramework
.getResourceSpecifierFactory().createResourceMetaData();
-
+
ResourceMetaDataFactory.configureResourceMetaData(meta, DummyComponent1.class);
-
+
assertEquals("dummy", meta.getName());
assertEquals("1.0", meta.getVersion());
assertEquals("Just a dummy", meta.getDescription());
@@ -44,9 +44,9 @@ public class ResourceMetaDataFactoryTest
public void testWithPartialMetaData() {
org.apache.uima.resource.metadata.ResourceMetaData meta = UIMAFramework
.getResourceSpecifierFactory().createResourceMetaData();
-
+
ResourceMetaDataFactory.configureResourceMetaData(meta, DummyComponent2.class);
-
+
assertEquals("dummy", meta.getName());
assertEquals("1.0", meta.getVersion());
assertEquals(null, meta.getDescription());
@@ -58,14 +58,17 @@ public class ResourceMetaDataFactoryTest
public void testWithNoMetaData() {
org.apache.uima.resource.metadata.ResourceMetaData meta = UIMAFramework
.getResourceSpecifierFactory().createResourceMetaData();
-
+
ResourceMetaDataFactory.configureResourceMetaData(meta, DummyComponent3.class);
-
+
assertEquals(DummyComponent3.class.getName(), meta.getName());
- assertEquals(Defaults.DEFAULT_VERSION, meta.getVersion());
- assertEquals(Defaults.DEFAULT_DESCRIPTION, meta.getDescription());
- assertEquals(null, meta.getCopyright());
- assertEquals(DummyComponent3.class.getPackage().getName(), meta.getVendor());
+ assertEquals(ResourceMetaDataFactory.getDefaultVersion(DummyComponent3.class),
+ meta.getVersion());
+ assertEquals(ResourceMetaDataFactory.getDefaultDescription(DummyComponent3.class),
+ meta.getDescription());
+ assertEquals(ResourceMetaDataFactory.getDefaultCopyright(DummyComponent3.class),
+ meta.getCopyright());
+ assertEquals(ResourceMetaDataFactory.getDefaultVendor(DummyComponent3.class), meta.getVendor());
}
@ResourceMetaData(name = "dummy", version = "1.0", description = "Just a dummy", copyright = "ASL 2.0", vendor = "uimaFIT")