Skip to content

Commit

Permalink
modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartolomeo Sorrentino authored and Bartolomeo Sorrentino committed Apr 23, 2019
1 parent 0f00f58 commit 5cc3644
Showing 1 changed file with 72 additions and 169 deletions.
241 changes: 72 additions & 169 deletions utils/src/main/java/org/bsc/processor/BaseAbstractProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,194 +5,121 @@

package org.bsc.processor;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Stream;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
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 info(String fmt, Object... args) {
final String msg = java.lang.String.format(fmt, (Object[]) args);
processingEnv.getMessager().printMessage(Kind.NOTE, msg);
}

protected void warn( String msg ) {
//logger.warning(msg);
processingEnv.getMessager().printMessage(Kind.WARNING, msg );
protected void warn(String fmt, Object... args) {
final String msg = java.lang.String.format(fmt, (Object[]) args);
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 );
protected void warn(String msg, Throwable 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 fmt, Object... args) {
final String msg = java.lang.String.format(fmt, (Object[]) args);
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 );
protected void error(String msg, Throwable 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());
protected java.util.Map<String,String> getOptions() {
return Optional.ofNullable(processingEnv.getOptions())
.orElseGet( () -> Collections.emptyMap() );
}

/**
*
* @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;


}

/**
*
* @param filter
* @return
*/
public static interface Predicate {

/**
*
* @param source
* @param elements
* @return
*/
boolean execute( TypeElement source, java.util.Set<? extends Element> elements );
}


protected void getElementsAnnotatedWith(
java.util.Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv,
Predicate p )
public Stream<? extends Element> elementStreamFromAnnotations(
java.util.Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv,
Predicate<? super TypeElement> filter )
{
if( annotations == null ) {
throw new IllegalArgumentException( "parameter annotations is null!");
throw new IllegalArgumentException( "annotations is null!");
}
if( roundEnv == null ) {
throw new IllegalArgumentException( "parameter RoundEnvironment is null!");
throw new IllegalArgumentException( "roundEnv is null!");
}
if( p == null ) {
throw new IllegalArgumentException( "paraemeter Predicate is null!");
}

for( TypeElement te : annotations ) {

final java.util.Set<? extends Element> elems =
roundEnv.getElementsAnnotatedWith(te);

if( !p.execute(te, elems) ) {
break;
}
if( filter == null ) {
throw new IllegalArgumentException( "filter is null!");
}

return annotations.stream()
.filter( filter )
.flatMap((e) -> roundEnv.getElementsAnnotatedWith(e).stream() );

}

/**
*
* @param location
* @return
* @param am
* @param supplier
* @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);
protected <R extends java.util.Map<String,Object>> R toMapObject( AnnotationMirror am, java.util.function.Supplier<R> supplier ) {

final Collector<java.util.Map.Entry<? extends Element, ? extends AnnotationValue>, R, R> c =
Collector.of(
supplier,
( map, entry ) ->
map.put( entry.getKey().getSimpleName().toString(), entry.getValue().getValue()),
( v1, v2 ) -> v1
);

final R result = am.getElementValues()
.entrySet()
.stream()
.collect( c );

return result;

}

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
Expand All @@ -201,21 +128,22 @@ protected FileObject getResourceFormLocation(
* @throws IOException
*/
protected FileObject createSourceOutputFile(
String subfolder,
String filePath ) throws IOException
Path subfolder,
Path filePath ) throws IOException
{

final Filer filer = processingEnv.getFiler();

Element e = null;
final Element e = null;
FileObject res = filer.createResource(
StandardLocation.SOURCE_OUTPUT,
subfolder,
filePath,
subfolder.toString(),
filePath.toString(),
e);
return res;
}


/**
*
* @param e
Expand All @@ -224,45 +152,20 @@ protected FileObject createSourceOutputFile(
*/
protected Class<?> getClassFromElement( Element e ) throws ClassNotFoundException
{
if( null==e ) throw new IllegalArgumentException("e is null!");
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()));
info( "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<String,String> getOptions()
{
java.util.Map<String,String> optionMap = processingEnv.getOptions();

if(optionMap==null) {
optionMap = Collections.emptyMap();
}
return optionMap ;
}

/**
*
* @param typeElement
Expand Down

0 comments on commit 5cc3644

Please sign in to comment.