Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2013 John Casey. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. /
 
 package org.commonjava.vertx.vabr.anno.proc;
 
 import static org.commonjava.vertx.vabr.anno.proc.QualifierInfo.EMPTY_QUALIFIER;
 
 import java.io.Writer;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  javax.inject.Qualifier;
 
 
 /* @formatter:off */
     "org.commonjava.vertx.vabr.anno.FilterRoutes",
     "org.commonjava.vertx.vabr.anno.FilterRoute",
     "org.commonjava.vertx.vabr.anno.HandlerClass",
     "org.commonjava.vertx.vabr.anno.PathPrefix",
     "org.commonjava.vertx.vabr.anno.Routes",
     "org.commonjava.vertx.vabr.anno.Route"
 } )
 /* @formatter:on */
     extends AbstractProcessor
 {
 
     public static final String TEMPLATE_PKG = "groovy";
 
     public static final String ROUTE_TEMPLATE = "routes.groovy";
 
     public static final String FILTER_TEMPLATE = "filters.groovy";
 
 
     private String pkg = null;
 
     @Override
     public boolean processfinal Set<? extends TypeElementannotationsfinal RoundEnvironment roundEnv )
     {
         //        if ( annotations.isEmpty() )
         //        {
         //            return true;
         //        }
 
         ..println"Starting Route Processing..." );
 
         final Map<QualifierInfoSet<RoutingTemplateInfo>> routingTemplates = processRoutesroundEnv );
         final Map<QualifierInfoSet<FilteringTemplateInfo>> filteringTemplates = processFiltersroundEnv );
 
         ..printf"Using package: %s\n" );
 
         if ( !routingTemplates.isEmpty() )
        {
            generateOutput"Routes"RouteCollection.classroutingTemplatesroundEnv );
        }
        if ( !filteringTemplates.isEmpty() )
        {
            generateOutput"Filters"FilterCollection.classfilteringTemplatesroundEnv );
        }
        return true;
    }
    private Map<QualifierInfoSet<RoutingTemplateInfo>> processRoutesfinal RoundEnvironment roundEnv )
    {
        final Map<QualifierInfoSet<RoutingTemplateInfo>> routingTemplates = new HashMap<>();
        for ( final Element elem : roundEnv.getElementsAnnotatedWithRoutes.class ) )
        {
            ..printf"Processing: %s\n"elem );
            final Handles handles = findTypeAnnotationelemHandles.class );
             = selectShortestPackageelem );
            final Routes routes = elem.getAnnotationRoutes.class );
            if ( routes != null )
            {
                for ( final Route route : routes.value() )
                {
                    final QualifierInfo key = findQualifierAnnotationelem );
                    addToroutingTemplatesnew RoutingTemplateInfoelemroutehandles ), key );
                }
            }
        }
        for ( final Element elem : roundEnv.getElementsAnnotatedWithRoute.class ) )
        {
            final Handles handles = findTypeAnnotationelemHandles.class );
            final Route route = elem.getAnnotationRoute.class );
             = selectShortestPackageelem );
            final QualifierInfo key = findQualifierAnnotationelem );
            addToroutingTemplatesnew RoutingTemplateInfoelemroutehandles ), key );
        }
        return routingTemplates;
    }
    private <T> void addTofinal Map<QualifierInfoSet<T>> templatesfinal T templatefinal QualifierInfo qi )
    {
        Set<T> routes = templates.getqi );
        if ( routes == null )
        {
            routes = new HashSet<>();
            templates.putqiroutes );
        }
        routes.addtemplate );
    }
    private QualifierInfo findQualifierAnnotationfinal Element elem )
    {
        Element pe = elem;
        do
        {
            pe = pe.getEnclosingElement();
        }
        while ( pe != null && pe.getKind() != . );
        final List<? extends AnnotationMirrorams = pe.getAnnotationMirrors();
        for ( final AnnotationMirror am : ams )
        {
            final Element annoElem = am.getAnnotationType()
                                       .asElement();
            final Qualifier qualifier = annoElem.getAnnotation( Qualifier.class );
            if ( qualifier != null )
            {
                return new QualifierInfoannoElem );
            }
        }
        return ;
    }
    private Map<QualifierInfoSet<FilteringTemplateInfo>> processFiltersfinal RoundEnvironment roundEnv )
    {
        final Map<QualifierInfoSet<FilteringTemplateInfo>> filteringTemplates = new HashMap<>();
        for ( final Element elem : roundEnv.getElementsAnnotatedWithFilterRoutes.class ) )
        {
            ..printf"Processing: %s\n"elem );
            final Handles handles = findTypeAnnotationelemHandles.class );
             = selectShortestPackageelem );
            final FilterRoutes filters = elem.getAnnotationFilterRoutes.class );
            if ( filters != null )
            {
                for ( final FilterRoute filter : filters.value() )
                {
                    final QualifierInfo key = findQualifierAnnotationelem );
                    addTofilteringTemplatesnew FilteringTemplateInfoelemfilterhandles ), key );
                }
            }
        }
        for ( final Element elem : roundEnv.getElementsAnnotatedWithFilterRoute.class ) )
        {
            final Handles handles = findTypeAnnotationelemHandles.class );
            final FilterRoute filter = elem.getAnnotationFilterRoute.class );
            final QualifierInfo key = findQualifierAnnotationelem );
            addTofilteringTemplatesnew FilteringTemplateInfoelemfilterhandles ), key );
             = selectShortestPackageelem );
        }
        return filteringTemplates;
    }
    private <T extends Annotation> T findTypeAnnotationfinal Element elemfinal Class<T> annoCls )
    {
        Element pe = elem;
        do
        {
            pe = pe.getEnclosingElement();
        }
        while ( pe != null && pe.getKind() != . );
        return pe.getAnnotationannoCls );
    }
    private String selectShortestPackagefinal String pkgfinal Element elem )
    {
        Element pe = elem;
        do
        {
            pe = pe.getEnclosingElement();
        }
        while ( pe != null && pe.getKind() != . );
        final String p = ( (PackageElementpe ).getQualifiedName()
                                                .toString();
        if ( pkg == null || p.length() < pkg.length() )
        {
            ..printf"Setting package: %s\n"p );
            return p;
        }
        return pkg;
    }
    private <T extends AbstractTemplateInfovoid generateOutputfinal String pkgfinal String simpleClassSuffixfinal String codeTemplateName,
                                                                  final Class<?> collectionClassfinal Map<QualifierInfoSet<T>> templates,
                                                                  final RoundEnvironment roundEnv )
    {
        Template template;
        try
        {
            final FileObject resource = .getFiler()
                                                     .getResource.codeTemplateName );
            if ( resource == null )
            {
                throw new IllegalStateException"Cannot find route template: " + codeTemplateName );
            }
            template = .createTemplateresource.toUri()
                                                      .toURL() );
        }
        catch ( CompilationFailedException | ClassNotFoundException | IOException e )
        {
            throw new IllegalStateException"Cannot load template: " +  + "/" + codeTemplateName + ". Reason: " + e.getMessage(), e );
        }
        ..printf"Package: %s\n"pkg );
        final Filer filer = .getFiler();
        final Set<StringgeneratedClassNames = new HashSet<>();
        for ( final Entry<QualifierInfoSet<T>> entry : templates.entrySet() )
        {
            final QualifierInfo key = entry.getKey();
            final Set<?> tmpls = entry.getValue();
            final Map<StringObjectparams = new HashMap<>();
            params.put"pkg"pkg );
            params.put"templates"tmpls );
            params.put"qualifier" != key ? key : null );
            final Writable output = template.makeparams );
            String simpleName = "";
            if ( key !=  )
            {
                simpleName += key.getSimpleName();
            }
            simpleName += simpleClassSuffix;
            params.put"className"simpleName );
            final String clsName = pkg + "." + simpleName;
            ..printf"Generating class: %s\n"clsName );
            Writer sourceWriter = null;
            try
            {
                final FileObject file = filer.createSourceFileclsName );
                sourceWriter = file.openWriter();
                output.writeTosourceWriter );
                generatedClassNames.addclsName );
            }
            catch ( final IOException e )
            {
                .getMessager()
                             .printMessage."While generating sources for class: '" + clsName + "', error: " + e.getMessage() );
            }
            finally
            {
                if ( sourceWriter != null )
                {
                    try
                    {
                        sourceWriter.close();
                    }
                    catch ( final IOException e )
                    {
                    }
                }
            }
        }
        final String resName = "META-INF/services/" + collectionClass.getName();
        Writer svcWriter = null;
        try
        {
            final FileObject file = filer.createResource.""resName, (Element[]) null );
            ..printf"Generating templates class service entry for:\n  %s\n\nin: %s\n"joingeneratedClassNames"\n  " ), file.toUri() );
            svcWriter = file.openWriter();
            svcWriter.writejoingeneratedClassNames"\n" ) );
        }
        catch ( final IOException e )
        {
            .getMessager()
                         .printMessage.,
                                        "While generating service configuration for templates class: '" + resName + "', error: " + e.getMessage() );
        }
        finally
        {
            if ( svcWriter != null )
            {
                try
                {
                    svcWriter.close();
                }
                catch ( final IOException e )
                {
                }
            }
        }
    }
    private String joinfinal Iterable<?> objectsfinal String joint )
    {
        final StringBuilder sb = new StringBuilder();
        for ( final Object obj : objects )
        {
            if ( sb.length() > 0 )
            {
                sb.appendjoint );
            }
            sb.appendobj );
        }
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X