From 30bbb057c63be236fa2c8f735415f4877f5aae59 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 12 Sep 2016 16:43:45 +0200 Subject: [PATCH] add utils and test project --- test/pom.xml | 161 ++++++++++ .../test/ParameterDocumentation.java | 15 + .../processor/test/ServiceDocumentation.java | 18 ++ .../processor/test/TESTWikiProcessor.java | 149 +++++++++ .../main/resources/ConfluenceWikiTemplate.txt | 21 ++ utils/nbactions.xml | 13 + utils/pom.xml | 122 ++++++++ .../bsc/processor/BaseAbstractProcessor.java | 284 ++++++++++++++++++ .../main/resources/ConfluenceWikiTemplate.txt | 22 ++ 9 files changed, 805 insertions(+) create mode 100644 test/pom.xml create mode 100644 test/src/main/java/org/bsc/maven/plugin/processor/test/ParameterDocumentation.java create mode 100644 test/src/main/java/org/bsc/maven/plugin/processor/test/ServiceDocumentation.java create mode 100644 test/src/main/java/org/bsc/maven/plugin/processor/test/TESTWikiProcessor.java create mode 100644 test/src/main/resources/ConfluenceWikiTemplate.txt create mode 100644 utils/nbactions.xml create mode 100644 utils/pom.xml create mode 100644 utils/src/main/java/org/bsc/processor/BaseAbstractProcessor.java create mode 100644 utils/src/main/resources/ConfluenceWikiTemplate.txt diff --git a/test/pom.xml b/test/pom.xml new file mode 100644 index 0000000..c12f246 --- /dev/null +++ b/test/pom.xml @@ -0,0 +1,161 @@ + + 4.0.0 + + org.bsc.maven + maven-processor-plugin-test + 3.1.0-SNAPSHOT + jar + maven processor plugin test - ${project.version} + + + + + + + + + + junit + junit + 4.10 + test + + + + + 2012 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + + + org.bsc.maven + maven-processor-plugin + ${project.version} + + + org.bsc + jaxrs-wiki-processor + 1.0-SNAPSHOT + + + + + + + + process-test + + process-test + + process-test-classes + + ${basedir}/src/site + + + ${user.home}/src + ./src/main/java + + + + false + + + org.bsc.maven.plugin.processor.test.TESTWikiProcessor + org.bsc.jaxrs.JAXRSWikiProcessor + + + + + + + + + + + process + + process + + process-classes + + ${basedir}/src/site + + + ${basedir}/src/main/java + ${user.home}/src + + + false + + + org.bsc.jaxrs.JAXRSWikiProcessor + + + + + + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.7 + + + add-source + generate-sources + + add-source + + + + ${basedir}/src-generated/src + + + + + add-test-source + generate-sources + + add-test-source + + + + ${basedir}/src-generated/test + + + + + + + + + + + diff --git a/test/src/main/java/org/bsc/maven/plugin/processor/test/ParameterDocumentation.java b/test/src/main/java/org/bsc/maven/plugin/processor/test/ParameterDocumentation.java new file mode 100644 index 0000000..f8515d8 --- /dev/null +++ b/test/src/main/java/org/bsc/maven/plugin/processor/test/ParameterDocumentation.java @@ -0,0 +1,15 @@ +package org.bsc.maven.plugin.processor.test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.SOURCE) +@Target({ ElementType.PARAMETER}) +public @interface ParameterDocumentation { + + String value() default ""; + + +} diff --git a/test/src/main/java/org/bsc/maven/plugin/processor/test/ServiceDocumentation.java b/test/src/main/java/org/bsc/maven/plugin/processor/test/ServiceDocumentation.java new file mode 100644 index 0000000..6fea188 --- /dev/null +++ b/test/src/main/java/org/bsc/maven/plugin/processor/test/ServiceDocumentation.java @@ -0,0 +1,18 @@ +package org.bsc.maven.plugin.processor.test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Retention(RetentionPolicy.SOURCE) +@Target({ ElementType.METHOD}) +public @interface ServiceDocumentation { + + public String since() default ""; + + public String value() default ""; + + public String note() default ""; +} diff --git a/test/src/main/java/org/bsc/maven/plugin/processor/test/TESTWikiProcessor.java b/test/src/main/java/org/bsc/maven/plugin/processor/test/TESTWikiProcessor.java new file mode 100644 index 0000000..9bb9500 --- /dev/null +++ b/test/src/main/java/org/bsc/maven/plugin/processor/test/TESTWikiProcessor.java @@ -0,0 +1,149 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.bsc.maven.plugin.processor.test; + +import java.io.IOException; +import java.util.Set; + +import javax.annotation.processing.*; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic.Kind; +import javax.tools.FileObject; +import javax.tools.StandardLocation; + +/** + * + * @author softphone + * + * + */ +@SupportedSourceVersion(SourceVersion.RELEASE_7) +@SupportedAnnotationTypes( "*" ) +//@SupportedOptions( {"subfolder", "filepath", "templateUri"}) +//@SupportedAnnotationTypes( {"javax.ws.rs.GET", "javax.ws.rs.PUT", "javax.ws.rs.POST", "javax.ws.rs.DELETE"}) +public class TESTWikiProcessor extends AbstractProcessor { + + protected void info( String msg ) { + processingEnv.getMessager().printMessage(Kind.NOTE, msg ); + } + + protected void warn( String msg ) { + //logger.warning(msg); + processingEnv.getMessager().printMessage(Kind.WARNING, msg ); + } + + protected void warn( String msg, Throwable t ) { + //logger.log(Level.WARNING, msg, t ); + processingEnv.getMessager().printMessage(Kind.WARNING, msg ); + t.printStackTrace(System.err); + } + + protected void error( String msg ) { + //logger.severe(msg); + processingEnv.getMessager().printMessage(Kind.ERROR, msg ); + } + + protected void error( String msg, Throwable t ) { + //logger.log(Level.SEVERE, msg, t ); + processingEnv.getMessager().printMessage(Kind.ERROR, msg ); + t.printStackTrace(System.err); + } + + /** + * + * @param filer + * @return + * @throws IOException + */ + protected FileObject getResourceFormClassPath(Filer filer, final String resource, final String packageName) throws IOException { + FileObject f = filer.getResource(StandardLocation.CLASS_PATH, packageName, resource); + + //java.io.Reader r = f.openReader(true); // ignoreEncodingErrors + java.io.InputStream is = f.openInputStream(); + + if( is==null ) { + warn( String.format("resource [%s] not found!", resource) ); + return null; + } + + return f; + } + + + /** + * + * @param subfolder subfolder (e.g. confluence) + * @param filePath relative path (e.g. children/file.wiki) + * @return + * @throws IOException + */ + protected FileObject getOutputFile( Filer filer, String subfolder, String filePath ) throws IOException { + + Element e = null; + FileObject res = + filer.createResource(StandardLocation.SOURCE_OUTPUT, + subfolder, + filePath, + e); + + return res; + } + + /** + * + * @param e + * @return + * @throws ClassNotFoundException + */ + protected Class getClassFromElement( Element e ) throws ClassNotFoundException { + if( null==e ) throw new IllegalArgumentException("e is null!"); + if( ElementKind.CLASS!=e.getKind() ) throw new IllegalArgumentException( String.format("element [%s] is not a class!", e)); + + TypeElement te = (TypeElement) e; + + info( String.format("loading class [%s]", te.getQualifiedName().toString())); + + return Class.forName(te.getQualifiedName().toString()); + + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (roundEnv.processingOver()) return false; + + System.out.println( "====> PROCESSOR RUN"); + + java.util.Map optionMap = processingEnv.getOptions(); + + + for( TypeElement e : annotations ) { + + for (Element re : roundEnv.getElementsAnnotatedWith(e)) { + + if( re.getKind()==ElementKind.METHOD) { + + info( String.format("[%s], Element [%s] is [%s] ", re.getEnclosingElement(), re.getKind(), re.getSimpleName())); + + } + } + } + + //final Filer filer = processingEnv.getFiler(); + + //FileObject res = getOutputFile(filer, subfolder, filePath); + + //java.io.Writer w = res.openWriter(); + + //w.close(); + + return true; + } + + +} diff --git a/test/src/main/resources/ConfluenceWikiTemplate.txt b/test/src/main/resources/ConfluenceWikiTemplate.txt new file mode 100644 index 0000000..f83f988 --- /dev/null +++ b/test/src/main/resources/ConfluenceWikiTemplate.txt @@ -0,0 +1,21 @@ + +{toc} + +h1. SERVICES +\\ +h2. ${service.name} +|*Description:* | ${service.description} | +| *Since:* | ${service.since} | +| *Notes:* | ${service.notes} | +| *Security:* | ${service.security} | +| *Usage:* | ${service.verb} ${service.path} | +| *Consumes* | ${service.consumes} | +| *Produces:* | ${service.produces} | +| *Parameters* | {quote} +|| name || default || description || +| ${param.name} | ${param.default} | ${param.description} | + +{quote}| +\\ +---- + diff --git a/utils/nbactions.xml b/utils/nbactions.xml new file mode 100644 index 0000000..e632dff --- /dev/null +++ b/utils/nbactions.xml @@ -0,0 +1,13 @@ + + + + CUSTOM-deploy + deploy + + deploy + + + sonatype + + + diff --git a/utils/pom.xml b/utils/pom.xml new file mode 100644 index 0000000..7db81bc --- /dev/null +++ b/utils/pom.xml @@ -0,0 +1,122 @@ + + 4.0.0 + + org.bsc.util + processor-utils + 1.0.1-SNAPSHOT + jar + + java processor utils - ${project.version} + + + UTF-8 + + + + scm:git:https://bartolomeo.sorrentino@code.google.com/p/maven-annotation-plugin.utils/ + scm:git:https://bartolomeo.sorrentino@code.google.com/p/maven-annotation-plugin.utils/ + https://bartolomeo.sorrentino@code.google.com/p/maven-annotation-plugin.utils/ + HEAD + + +2013 +Utilities for java annotation processor implementation JSR-269 +https://code.google.com/p/maven-annotation-plugin/ + + + + The MIT License (MIT) + http://opensource.org/licenses/MIT + + + + + + bartolomeo.sorrentino@gmail.com + Bartolomeo Sorrentino + GMT+1 + + Owner + + + + + + + + junit + junit + 4.10 + test + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + + + + + + + ${release.repo.id} + ${release.repo.url} + + + ${snapshot.repo.id} + ${snapshot.repo.url} + false + + + + + + + sonatype + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.3 + + + + sign-artifacts + verify + + sign + + + + + + + + + + + + + + diff --git a/utils/src/main/java/org/bsc/processor/BaseAbstractProcessor.java b/utils/src/main/java/org/bsc/processor/BaseAbstractProcessor.java new file mode 100644 index 0000000..1084906 --- /dev/null +++ b/utils/src/main/java/org/bsc/processor/BaseAbstractProcessor.java @@ -0,0 +1,284 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.bsc.processor; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Filer; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic.Kind; +import javax.tools.FileObject; +import javax.tools.StandardLocation; + + +import java.util.Collections; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.annotation.processing.RoundEnvironment; +import javax.tools.JavaFileManager; + + +/** + * + * @author bsorrentino + * + * + */ +//@SupportedSourceVersion(SourceVersion.RELEASE_6) +public abstract class BaseAbstractProcessor extends AbstractProcessor { + + protected void info( String msg ) { + processingEnv.getMessager().printMessage(Kind.NOTE, msg ); + } + + protected void warn( String msg ) { + //logger.warning(msg); + processingEnv.getMessager().printMessage(Kind.WARNING, msg ); + } + + protected void warn( String msg, Throwable t ) { + //logger.log(Level.WARNING, msg, t ); + processingEnv.getMessager().printMessage(Kind.WARNING, msg ); + t.printStackTrace(System.err); + } + + protected void error( String msg ) { + //logger.severe(msg); + processingEnv.getMessager().printMessage(Kind.ERROR, msg ); + } + + protected void error( String msg, Throwable t ) { + //logger.log(Level.SEVERE, msg, t ); + processingEnv.getMessager().printMessage(Kind.ERROR, msg ); + t.printStackTrace(System.err); + } + + final Pattern p = Pattern.compile("(.*)[\\.]((?:\\w+)\\.gwt\\.xml)"); + + /** + * + * @param fqn + * @return + */ + protected java.net.URL getResourceFromClassPath( String fqn ) + { + return getResourceFromClassPath(fqn, getClass().getClassLoader()); + } + + /** + * + * @param fqn + * @param cl + * @return + */ + protected java.net.URL getResourceFromClassPath( String fqn, ClassLoader cl ) + { + if( fqn == null ) { + throw new IllegalArgumentException( "fqn is null!"); + } + if( cl == null ) { + throw new IllegalArgumentException( "class loader is null!"); + } + + Matcher m = p.matcher(fqn); + + if( !m.matches() ) { + throw new IllegalArgumentException(String.format("parameter '%s' doesn't contain a valid fqn", fqn)); + } + + final String packageName = m.group(1); + final String resource = m.group(2); + + info( String.format("packageName=[%s]\nresource=[%s]\n", packageName, resource)); + + final String res; + if( packageName==null || packageName.isEmpty() ) { + res = resource; + } + else { + res = packageName.replace('.', '/').concat("/").concat(resource); + } + + final java.net.URL url = cl.getResource( res ); + + return url; + + + } + + /** + * + */ + public static interface Predicate { + + /** + * + * @param source + * @param elements + * @return + */ + boolean execute( TypeElement source, java.util.Set elements ); + } + + + protected void getElementsAnnotatedWith( + java.util.Set annotations, + RoundEnvironment roundEnv, + Predicate p ) + { + if( annotations == null ) { + throw new IllegalArgumentException( "parameter annotations is null!"); + } + if( roundEnv == null ) { + throw new IllegalArgumentException( "parameter RoundEnvironment is null!"); + } + if( p == null ) { + throw new IllegalArgumentException( "paraemeter Predicate is null!"); + } + + for( TypeElement te : annotations ) { + + final java.util.Set elems = + roundEnv.getElementsAnnotatedWith(te); + + if( !p.execute(te, elems) ) { + break; + } + } + } + /** + * + * @param location + * @return + */ + protected FileObject getResourceFormLocation( + final JavaFileManager.Location location, + final String packageName, + final String resource ) throws FileNotFoundException,IOException + { + if( location == null ) { + throw new IllegalArgumentException( "location is null!"); + } + if( packageName == null ) { + throw new IllegalArgumentException( "packageName loader is null!"); + } + if( resource == null ) { + throw new IllegalArgumentException( "resource loader is null!"); + } + + final Filer filer = processingEnv.getFiler(); + + FileObject f = filer.getResource( + location, + packageName, + resource); + + java.io.InputStream is = f.openInputStream(); + + if( is==null ) { + warn( String.format("resource [%s] not found!", resource) ); + return null; + } + + is.close(); + + return f; + } + + + /** + * + * @param subfolder subfolder + * @param filePath relative path + * @return + * @throws IOException + */ + protected FileObject createSourceOutputFile( + String subfolder, + String filePath ) throws IOException + { + + final Filer filer = processingEnv.getFiler(); + + Element e = null; + FileObject res = filer.createResource( + StandardLocation.SOURCE_OUTPUT, + subfolder, + filePath, + e); + return res; + } + + /** + * + * @param e + * @return + * @throws ClassNotFoundException + */ + protected Class getClassFromElement( Element e ) throws ClassNotFoundException + { + if( null==e ) throw new IllegalArgumentException("e is null!"); + if( ElementKind.CLASS!=e.getKind() ) { + throw new IllegalArgumentException( String.format("element [%s] is not a class!", e)); + } + + TypeElement te = (TypeElement) e; + + info( String.format("loading class [%s]", te.getQualifiedName().toString())); + + return Class.forName(te.getQualifiedName().toString()); + + } + + /** + * + * @return com.sun.source.util.Trees + */ + /* + protected com.sun.source.util.Trees newTreesInstance() { + + return com.sun.source.util.Trees.instance(processingEnv); + + }; + */ + + /** + * + * @return + */ + protected java.util.Map getOptions() + { + java.util.Map optionMap = processingEnv.getOptions(); + + if(optionMap==null) { + optionMap = Collections.emptyMap(); + } + return optionMap ; + } + + /** + * + * @param typeElement + * @return + */ + protected String getFullClassName( Element typeElement ) { + + if( typeElement instanceof TypeElement ) { + + return ((TypeElement)typeElement).getQualifiedName().toString(); + } + + return typeElement.getSimpleName().toString(); + } + + + + +} diff --git a/utils/src/main/resources/ConfluenceWikiTemplate.txt b/utils/src/main/resources/ConfluenceWikiTemplate.txt new file mode 100644 index 0000000..e884169 --- /dev/null +++ b/utils/src/main/resources/ConfluenceWikiTemplate.txt @@ -0,0 +1,22 @@ + +{toc} + +h1. SERVICES +\\ +h2. ${service.name} +|*Description:* | ${service.description} | +| *Since:* | ${service.since} | +| *Notes:* | ${service.notes} | +| *Security:* | ${service.security} | +| *Usage:* | ${service.verb} ${service.path} | +| *Consumes* | ${service.consumes} | +| *Produces:* | ${service.produces} | +| *Response* | ${service.response} | +| *Parameters* | {quote} +|| name || default || description || +| ${param.name} | ${param.default} | ${param.description} | + +{quote}| +\\ +---- +