From dfec22a65ede8a0cc490499c384d12263f17ab70 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 4 May 2012 10:00:19 +0000 Subject: [PATCH] [MPLUGIN-189] cleanup with moving some classes to separate packages git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/branches/MPLUGIN-189@1333836 13f79535-47bb-0310-9956-ffa450edef68 --- .../AnnotatedField.java | 2 +- .../ComponentAnnotationContent.java | 2 +- .../datamodel/ExecuteAnnotationContent.java | 86 +++ .../datamodel/MojoAnnotationContent.java | 227 ++++++++ .../ParameterAnnotationContent.java | 2 +- .../DefaultMojoAnnotationsScanner.java | 26 +- .../scanner/MojoAnnotatedClass.java | 2 + .../annotations/scanner/MojoClassVisitor.java | 524 ------------------ .../visitors/MojoAnnotationVisitor.java | 83 +++ .../scanner/visitors/MojoClassVisitor.java | 160 ++++++ .../scanner/visitors/MojoFieldVisitor.java | 77 +++ .../annotations/TestAnnotationsReader.java | 4 +- 12 files changed, 654 insertions(+), 541 deletions(-) rename maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/{scanner => datamodel}/AnnotatedField.java (95%) rename maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/{scanner => datamodel}/ComponentAnnotationContent.java (97%) create mode 100644 maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ExecuteAnnotationContent.java create mode 100644 maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/MojoAnnotationContent.java rename maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/{scanner => datamodel}/ParameterAnnotationContent.java (98%) delete mode 100644 maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoClassVisitor.java create mode 100644 maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoAnnotationVisitor.java create mode 100644 maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoClassVisitor.java create mode 100644 maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoFieldVisitor.java diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/AnnotatedField.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/AnnotatedField.java similarity index 95% rename from maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/AnnotatedField.java rename to maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/AnnotatedField.java index 7bf891a..cc9bf6d 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/AnnotatedField.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/AnnotatedField.java @@ -1,4 +1,4 @@ -package org.apache.maven.tools.plugin.annotations.scanner; +package org.apache.maven.tools.plugin.annotations.datamodel; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/ComponentAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ComponentAnnotationContent.java similarity index 97% rename from maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/ComponentAnnotationContent.java rename to maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ComponentAnnotationContent.java index 60e3f72..b7214cc 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/ComponentAnnotationContent.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ComponentAnnotationContent.java @@ -1,4 +1,4 @@ -package org.apache.maven.tools.plugin.annotations.scanner; +package org.apache.maven.tools.plugin.annotations.datamodel; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ExecuteAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ExecuteAnnotationContent.java new file mode 100644 index 0000000..3c597c6 --- /dev/null +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ExecuteAnnotationContent.java @@ -0,0 +1,86 @@ +package org.apache.maven.tools.plugin.annotations.datamodel; +/* + * 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. + */ + +import org.apache.maven.tools.plugin.annotations.Execute; +import org.apache.maven.tools.plugin.annotations.LifecyclePhase; + +import java.lang.annotation.Annotation; + +/** + * @author Olivier Lamy + */ +public class ExecuteAnnotationContent + implements Execute +{ + private String goal; + + private String lifecycle; + + private LifecyclePhase phase; + + public LifecyclePhase phase() + { + return this.phase; + } + + public String goal() + { + return this.goal; + } + + public String lifecycle() + { + return this.lifecycle; + } + + + public void phase( String phase ) + { + this.phase = LifecyclePhase.valueOf( phase ); + } + + public void goal( String goal ) + { + this.goal = goal; + } + + public void lifecycle( String lifecycle ) + { + this.lifecycle = lifecycle; + } + + + public Class annotationType() + { + return null; + } + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder(); + sb.append( "ExecuteAnnotationContent" ); + sb.append( "{goal='" ).append( goal ).append( '\'' ); + sb.append( ", lifecycle='" ).append( lifecycle ).append( '\'' ); + sb.append( ", phase=" ).append( phase ); + sb.append( '}' ); + return sb.toString(); + } +} diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/MojoAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/MojoAnnotationContent.java new file mode 100644 index 0000000..8986633 --- /dev/null +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/MojoAnnotationContent.java @@ -0,0 +1,227 @@ +package org.apache.maven.tools.plugin.annotations.datamodel; +/* + * 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. + */ + +import org.apache.maven.tools.plugin.annotations.LifecyclePhase; +import org.apache.maven.tools.plugin.annotations.Mojo; + +import java.lang.annotation.Annotation; + +/** + * @author Olivier Lamy + */ +public class MojoAnnotationContent + implements Mojo +{ + private String name; + + private LifecyclePhase defaultPhase = LifecyclePhase.NONE; + + private String requiresDependencyResolution = "runtime"; + + private String requiresDependencyCollection = "runtime"; + + private String instantiationStrategy = "per-lookup"; + + private String executionStrategy = "once-per-session"; + + private boolean requiresProject = true; + + private boolean requiresReports = false; + + private boolean aggregator = false; + + private boolean requiresDirectInvocation = false; + + private boolean requiresOnline = false; + + private boolean inheritByDefault = true; + + private String configurator; + + private boolean threadSafe = false; + + public Class annotationType() + { + return null; + } + + public LifecyclePhase defaultPhase() + { + return defaultPhase; + } + + public void defaultPhase( String phase ) + { + this.defaultPhase = LifecyclePhase.valueOf( phase ); + } + + public String requiresDependencyResolution() + { + return requiresDependencyResolution; + } + + public void requiresDependencyResolution( String requiresDependencyResolution ) + { + this.requiresDependencyResolution = requiresDependencyResolution; + } + + public String requiresDependencyCollection() + { + return requiresDependencyCollection; + } + + public void requiresDependencyCollection( String requiresDependencyCollection ) + { + this.requiresDependencyCollection = requiresDependencyCollection; + } + + public String instantiationStrategy() + { + return instantiationStrategy; + } + + public void instantiationStrategy( String instantiationStrategy ) + { + this.instantiationStrategy = instantiationStrategy; + } + + public String executionStrategy() + { + return executionStrategy; + } + + public void executionStrategy( String executionStrategy ) + { + this.executionStrategy = executionStrategy; + } + + public boolean requiresProject() + { + return requiresProject; + } + + public void requiresProject( boolean requiresProject ) + { + this.requiresProject = requiresProject; + } + + public boolean requiresReports() + { + return requiresReports; + } + + public void requiresReports( boolean requiresReports ) + { + this.requiresReports = requiresReports; + } + + public boolean aggregator() + { + return aggregator; + } + + public void aggregator( boolean aggregator ) + { + this.aggregator = aggregator; + } + + public boolean requiresDirectInvocation() + { + return requiresDirectInvocation; + } + + public void requiresDirectInvocation( boolean requiresDirectInvocation ) + { + this.requiresDirectInvocation = requiresDirectInvocation; + } + + public boolean requiresOnline() + { + return requiresOnline; + } + + public void requiresOnline( boolean requiresOnline ) + { + this.requiresOnline = requiresOnline; + } + + public boolean inheritByDefault() + { + return inheritByDefault; + } + + public void inheritByDefault( boolean inheritByDefault ) + { + this.inheritByDefault = inheritByDefault; + } + + public String configurator() + { + return configurator; + } + + public void configurator( String configurator ) + { + this.configurator = configurator; + } + + public boolean threadSafe() + { + return threadSafe; + } + + public void threadSafe( boolean threadSafe ) + { + this.threadSafe = threadSafe; + } + + public String name() + { + return this.name; + } + + public void name( String name ) + { + this.name = name; + } + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder(); + sb.append( "MojoAnnotationContent" ); + sb.append( "{name='" ).append( name ).append( '\'' ); + sb.append( ", defaultPhase=" ).append( defaultPhase ); + sb.append( ", requiresDependencyResolution='" ).append( requiresDependencyResolution ).append( '\'' ); + sb.append( ", requiresDependencyCollection='" ).append( requiresDependencyCollection ).append( '\'' ); + sb.append( ", instantiationStrategy='" ).append( instantiationStrategy ).append( '\'' ); + sb.append( ", executionStrategy='" ).append( executionStrategy ).append( '\'' ); + sb.append( ", requiresProject=" ).append( requiresProject ); + sb.append( ", requiresReports=" ).append( requiresReports ); + sb.append( ", aggregator=" ).append( aggregator ); + sb.append( ", requiresDirectInvocation=" ).append( requiresDirectInvocation ); + sb.append( ", requiresOnline=" ).append( requiresOnline ); + sb.append( ", inheritByDefault=" ).append( inheritByDefault ); + sb.append( ", configurator='" ).append( configurator ).append( '\'' ); + sb.append( ", threadSafe=" ).append( threadSafe ); + sb.append( '}' ); + return sb.toString(); + } +} \ No newline at end of file diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/ParameterAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ParameterAnnotationContent.java similarity index 98% rename from maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/ParameterAnnotationContent.java rename to maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ParameterAnnotationContent.java index bf20605..556ddbc 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/ParameterAnnotationContent.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/datamodel/ParameterAnnotationContent.java @@ -1,4 +1,4 @@ -package org.apache.maven.tools.plugin.annotations.scanner; +package org.apache.maven.tools.plugin.annotations.datamodel; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/DefaultMojoAnnotationsScanner.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/DefaultMojoAnnotationsScanner.java index cbe9b88..e02658a 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/DefaultMojoAnnotationsScanner.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/DefaultMojoAnnotationsScanner.java @@ -22,6 +22,13 @@ import org.apache.maven.tools.plugin.annotations.Component; import org.apache.maven.tools.plugin.annotations.Execute; import org.apache.maven.tools.plugin.annotations.Mojo; import org.apache.maven.tools.plugin.annotations.Parameter; +import org.apache.maven.tools.plugin.annotations.datamodel.ComponentAnnotationContent; +import org.apache.maven.tools.plugin.annotations.datamodel.ExecuteAnnotationContent; +import org.apache.maven.tools.plugin.annotations.datamodel.MojoAnnotationContent; +import org.apache.maven.tools.plugin.annotations.datamodel.ParameterAnnotationContent; +import org.apache.maven.tools.plugin.annotations.scanner.visitors.MojoAnnotationVisitor; +import org.apache.maven.tools.plugin.annotations.scanner.visitors.MojoClassVisitor; +import org.apache.maven.tools.plugin.annotations.scanner.visitors.MojoFieldVisitor; import org.apache.maven.tools.plugin.extractor.ExtractionException; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.DirectoryScanner; @@ -128,14 +135,11 @@ public class DefaultMojoAnnotationsScanner try { - MojoClassVisitor.MojoAnnotationVisitor mojoAnnotationVisitor = + MojoAnnotationVisitor mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitorMap().get( Mojo.class.getName() ); if ( mojoAnnotationVisitor != null ) { - MojoClassVisitor.MojoAnnotationContent mojoAnnotationContent = - new MojoClassVisitor.MojoAnnotationContent(); - Class clazz = Thread.currentThread().getContextClassLoader().loadClass( - MojoClassVisitor.MojoAnnotationContent.class.getName() ); + MojoAnnotationContent mojoAnnotationContent = new MojoAnnotationContent(); for ( Map.Entry entry : mojoAnnotationVisitor.getAnnotationValues().entrySet() ) { reflector.invoke( mojoAnnotationContent, entry.getKey(), new Object[]{ entry.getValue() } ); @@ -146,10 +150,8 @@ public class DefaultMojoAnnotationsScanner mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitorMap().get( Execute.class.getName() ); if ( mojoAnnotationVisitor != null ) { - MojoClassVisitor.ExecuteAnnotationContent executeAnnotationContent = - new MojoClassVisitor.ExecuteAnnotationContent(); - Class clazz = Thread.currentThread().getContextClassLoader().loadClass( - MojoClassVisitor.MojoAnnotationContent.class.getName() ); + ExecuteAnnotationContent executeAnnotationContent = new ExecuteAnnotationContent(); + for ( Map.Entry entry : mojoAnnotationVisitor.getAnnotationValues().entrySet() ) { reflector.invoke( executeAnnotationContent, entry.getKey(), new Object[]{ entry.getValue() } ); @@ -157,10 +159,10 @@ public class DefaultMojoAnnotationsScanner mojoClassVisitor.getMojoAnnotatedClass().setExecute( executeAnnotationContent ); } - List mojoFieldVisitors = + List mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotationClass( Parameter.class.getName() ); - for ( MojoClassVisitor.MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors ) + for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors ) { ParameterAnnotationContent parameterAnnotationContent = new ParameterAnnotationContent( mojoFieldVisitor.getFieldName() ); @@ -178,7 +180,7 @@ public class DefaultMojoAnnotationsScanner mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotationClass( Component.class.getName() ); - for ( MojoClassVisitor.MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors ) + for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors ) { ComponentAnnotationContent componentAnnotationContent = new ComponentAnnotationContent( mojoFieldVisitor.getFieldName() ); diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoAnnotatedClass.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoAnnotatedClass.java index 2721c18..91e63f5 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoAnnotatedClass.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoAnnotatedClass.java @@ -20,6 +20,8 @@ package org.apache.maven.tools.plugin.annotations.scanner; import org.apache.maven.tools.plugin.annotations.Execute; import org.apache.maven.tools.plugin.annotations.Mojo; +import org.apache.maven.tools.plugin.annotations.datamodel.ComponentAnnotationContent; +import org.apache.maven.tools.plugin.annotations.datamodel.ParameterAnnotationContent; import java.util.ArrayList; import java.util.List; diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoClassVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoClassVisitor.java deleted file mode 100644 index 53e9cac..0000000 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/MojoClassVisitor.java +++ /dev/null @@ -1,524 +0,0 @@ -package org.apache.maven.tools.plugin.annotations.scanner; -/* - * 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. - */ - -import org.apache.maven.tools.plugin.annotations.Execute; -import org.apache.maven.tools.plugin.annotations.LifecyclePhase; -import org.apache.maven.tools.plugin.annotations.Mojo; -import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.util.StringUtils; -import org.objectweb.asm.AnnotationVisitor; -import org.objectweb.asm.Attribute; -import org.objectweb.asm.ClassVisitor; -import org.objectweb.asm.FieldVisitor; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Type; - -import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * @author Olivier Lamy - */ -public class MojoClassVisitor - implements ClassVisitor -{ - private Logger logger; - - private MojoAnnotatedClass mojoAnnotatedClass; - - private Map annotationVisitorMap = new HashMap(); - - private List fieldVisitors = new ArrayList(); - - public MojoClassVisitor( Logger logger ) - { - this.logger = logger; - } - - public MojoAnnotatedClass getMojoAnnotatedClass() - { - return mojoAnnotatedClass; - } - - public void setMojoAnnotatedClass( MojoAnnotatedClass mojoAnnotatedClass ) - { - this.mojoAnnotatedClass = mojoAnnotatedClass; - } - - public Map getAnnotationVisitorMap() - { - return annotationVisitorMap; - } - - public void setAnnotationVisitorMap( Map annotationVisitorMap ) - { - this.annotationVisitorMap = annotationVisitorMap; - } - - public List getFieldVisitors() - { - return fieldVisitors; - } - - public void setFieldVisitors( List fieldVisitors ) - { - this.fieldVisitors = fieldVisitors; - } - - public List findFieldWithAnnotationClass( String annotationClassName ) - { - List mojoFieldVisitors = new ArrayList(); - - for ( MojoFieldVisitor mojoFieldVisitor : this.fieldVisitors ) - { - MojoAnnotationVisitor mojoAnnotationVisitor = mojoFieldVisitor.getMojoAnnotationVisitor(); - if ( mojoAnnotationVisitor != null && StringUtils.equals( annotationClassName, - mojoAnnotationVisitor.annotationClassName ) ) - { - mojoFieldVisitors.add( mojoFieldVisitor ); - } - } - - return mojoFieldVisitors; - } - - public void visit( int version, int access, String name, String signature, String superName, String[] interfaces ) - { - mojoAnnotatedClass = new MojoAnnotatedClass(); - mojoAnnotatedClass.setClassName( Type.getObjectType( name ).getClassName() ).setParentClassName( - Type.getObjectType( superName ).getClassName() ); - logger.debug( "MojoClassVisitor#visit" ); - } - - public AnnotationVisitor visitAnnotation( String desc, boolean visible ) - { - logger.debug( "MojoClassVisitor#visitAnnotation" ); - String annotationClassName = Type.getType( desc ).getClassName(); - if ( !MojoAnnotationsScanner.acceptedClassLevelAnnotationClasses.contains( annotationClassName ) ) - { - return null; - } - MojoAnnotationVisitor mojoAnnotationVisitor = new MojoAnnotationVisitor( logger, annotationClassName ); - annotationVisitorMap.put( annotationClassName, mojoAnnotationVisitor ); - return mojoAnnotationVisitor; - } - - public FieldVisitor visitField( int access, String name, String desc, String signature, Object value ) - { - // Type.getType( desc ).getClassName() - logger.debug( "MojoClassVisitor#visitField" ); - MojoFieldVisitor mojoFieldVisitor = new MojoFieldVisitor( logger, name ); - fieldVisitors.add( mojoFieldVisitor ); - return mojoFieldVisitor; - } - - public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions ) - { - // we don't need methods informations - return null; - } - - public void visitAttribute( Attribute attr ) - { - } - - public void visitSource( String source, String debug ) - { - } - - public void visitOuterClass( String owner, String name, String desc ) - { - } - - public void visitInnerClass( String name, String outerName, String innerName, int access ) - { - } - - public void visitEnd() - { - logger.debug( "MojoClassVisitor#visitEnd" ); - } - - public static class MojoAnnotationContent - implements Mojo - { - - private String name; - - private LifecyclePhase defaultPhase = LifecyclePhase.NONE; - - private String requiresDependencyResolution = "runtime"; - - private String requiresDependencyCollection = "runtime"; - - private String instantiationStrategy = "per-lookup"; - - private String executionStrategy = "once-per-session"; - - private boolean requiresProject = true; - - private boolean requiresReports = false; - - private boolean aggregator = false; - - private boolean requiresDirectInvocation = false; - - private boolean requiresOnline = false; - - private boolean inheritByDefault = true; - - private String configurator; - - private boolean threadSafe = false; - - public Class annotationType() - { - return null; - } - - public LifecyclePhase defaultPhase() - { - return defaultPhase; - } - - public void defaultPhase( String phase ) - { - this.defaultPhase = LifecyclePhase.valueOf( phase ); - } - - public String requiresDependencyResolution() - { - return requiresDependencyResolution; - } - - public void requiresDependencyResolution( String requiresDependencyResolution ) - { - this.requiresDependencyResolution = requiresDependencyResolution; - } - - public String requiresDependencyCollection() - { - return requiresDependencyCollection; - } - - public void requiresDependencyCollection( String requiresDependencyCollection ) - { - this.requiresDependencyCollection = requiresDependencyCollection; - } - - public String instantiationStrategy() - { - return instantiationStrategy; - } - - public void instantiationStrategy( String instantiationStrategy ) - { - this.instantiationStrategy = instantiationStrategy; - } - - public String executionStrategy() - { - return executionStrategy; - } - - public void executionStrategy( String executionStrategy ) - { - this.executionStrategy = executionStrategy; - } - - public boolean requiresProject() - { - return requiresProject; - } - - public void requiresProject( boolean requiresProject ) - { - this.requiresProject = requiresProject; - } - - public boolean requiresReports() - { - return requiresReports; - } - - public void requiresReports( boolean requiresReports ) - { - this.requiresReports = requiresReports; - } - - public boolean aggregator() - { - return aggregator; - } - - public void aggregator( boolean aggregator ) - { - this.aggregator = aggregator; - } - - public boolean requiresDirectInvocation() - { - return requiresDirectInvocation; - } - - public void requiresDirectInvocation( boolean requiresDirectInvocation ) - { - this.requiresDirectInvocation = requiresDirectInvocation; - } - - public boolean requiresOnline() - { - return requiresOnline; - } - - public void requiresOnline( boolean requiresOnline ) - { - this.requiresOnline = requiresOnline; - } - - public boolean inheritByDefault() - { - return inheritByDefault; - } - - public void inheritByDefault( boolean inheritByDefault ) - { - this.inheritByDefault = inheritByDefault; - } - - public String configurator() - { - return configurator; - } - - public void configurator( String configurator ) - { - this.configurator = configurator; - } - - public boolean threadSafe() - { - return threadSafe; - } - - public void threadSafe( boolean threadSafe ) - { - this.threadSafe = threadSafe; - } - - public String name() - { - return this.name; - } - - public void name( String name ) - { - this.name = name; - } - - @Override - public String toString() - { - final StringBuilder sb = new StringBuilder(); - sb.append( "MojoAnnotationContent" ); - sb.append( "{name='" ).append( name ).append( '\'' ); - sb.append( ", defaultPhase=" ).append( defaultPhase ); - sb.append( ", requiresDependencyResolution='" ).append( requiresDependencyResolution ).append( '\'' ); - sb.append( ", requiresDependencyCollection='" ).append( requiresDependencyCollection ).append( '\'' ); - sb.append( ", instantiationStrategy='" ).append( instantiationStrategy ).append( '\'' ); - sb.append( ", executionStrategy='" ).append( executionStrategy ).append( '\'' ); - sb.append( ", requiresProject=" ).append( requiresProject ); - sb.append( ", requiresReports=" ).append( requiresReports ); - sb.append( ", aggregator=" ).append( aggregator ); - sb.append( ", requiresDirectInvocation=" ).append( requiresDirectInvocation ); - sb.append( ", requiresOnline=" ).append( requiresOnline ); - sb.append( ", inheritByDefault=" ).append( inheritByDefault ); - sb.append( ", configurator='" ).append( configurator ).append( '\'' ); - sb.append( ", threadSafe=" ).append( threadSafe ); - sb.append( '}' ); - return sb.toString(); - } - } - - public static class ExecuteAnnotationContent - implements Execute - { - - private String goal; - - private String lifecycle; - - private LifecyclePhase phase; - - public LifecyclePhase phase() - { - return this.phase; - } - - public String goal() - { - return this.goal; - } - - public String lifecycle() - { - return this.lifecycle; - } - - - public void phase( String phase ) - { - this.phase = LifecyclePhase.valueOf( phase ); - } - - public void goal( String goal ) - { - this.goal = goal; - } - - public void lifecycle( String lifecycle ) - { - this.lifecycle = lifecycle; - } - - - public Class annotationType() - { - return null; - } - - @Override - public String toString() - { - final StringBuilder sb = new StringBuilder(); - sb.append( "ExecuteAnnotationContent" ); - sb.append( "{goal='" ).append( goal ).append( '\'' ); - sb.append( ", lifecycle='" ).append( lifecycle ).append( '\'' ); - sb.append( ", phase=" ).append( phase ); - sb.append( '}' ); - return sb.toString(); - } - } - - //------------------------------------- - // internal classes - //------------------------------------- - static class MojoAnnotationVisitor - implements AnnotationVisitor - { - - private Logger logger; - - private String annotationClassName; - - private Map annotationValues = new HashMap(); - - MojoAnnotationVisitor( Logger logger, String annotationClassName ) - { - this.logger = logger; - this.annotationClassName = annotationClassName; - } - - public Map getAnnotationValues() - { - return annotationValues; - } - - public void visit( String name, Object value ) - { - annotationValues.put( name, value ); - logger.debug( "MojoAnnotationVisitor#visit:" + name + ":" + value ); - } - - public void visitEnum( String name, String desc, String value ) - { - annotationValues.put( name, value ); - logger.debug( "MojoAnnotationVisitor#visitEnum:" + name + ":" + desc + ":" + value ); - } - - public AnnotationVisitor visitAnnotation( String name, String desc ) - { - logger.debug( "MojoAnnotationVisitor#visitAnnotation:" + name + ":" + desc ); - return new MojoAnnotationVisitor( logger, this.annotationClassName ); - } - - public AnnotationVisitor visitArray( String s ) - { - logger.debug( "MojoAnnotationVisitor#visitArray" ); - return new MojoAnnotationVisitor( logger, this.annotationClassName ); - } - - public void visitEnd() - { - logger.debug( "MojoAnnotationVisitor#visitEnd" ); - } - } - - static class MojoFieldVisitor - implements FieldVisitor - { - private Logger logger; - - private String fieldName; - - private MojoAnnotationVisitor mojoAnnotationVisitor; - - MojoFieldVisitor( Logger logger, String fieldName ) - { - this.logger = logger; - this.fieldName = fieldName; - } - - public MojoAnnotationVisitor getMojoAnnotationVisitor() - { - return mojoAnnotationVisitor; - } - - public String getFieldName() - { - return fieldName; - } - - public AnnotationVisitor visitAnnotation( String desc, boolean visible ) - { - logger.debug( "MojoFieldVisitor#visitAnnotation:" + desc ); - String annotationClassName = Type.getType( desc ).getClassName(); - if ( !MojoAnnotationsScanner.acceptedFieldLevelAnnotationClasses.contains( annotationClassName ) ) - { - return null; - } - mojoAnnotationVisitor = new MojoAnnotationVisitor( logger, annotationClassName ); - return mojoAnnotationVisitor; - } - - public void visitAttribute( Attribute attribute ) - { - logger.debug( "MojoFieldVisitor#visitAttribute" ); - } - - public void visitEnd() - { - logger.debug( "MojoFieldVisitor#visitEnd" ); - } - } -} diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoAnnotationVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoAnnotationVisitor.java new file mode 100644 index 0000000..175803a --- /dev/null +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoAnnotationVisitor.java @@ -0,0 +1,83 @@ +package org.apache.maven.tools.plugin.annotations.scanner.visitors; +/* + * 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. + */ + +import org.codehaus.plexus.logging.Logger; +import org.objectweb.asm.AnnotationVisitor; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Olivier Lamy + */ +public class MojoAnnotationVisitor + implements AnnotationVisitor +{ + private Logger logger; + + private String annotationClassName; + + private Map annotationValues = new HashMap(); + + MojoAnnotationVisitor( Logger logger, String annotationClassName ) + { + this.logger = logger; + this.annotationClassName = annotationClassName; + } + + public Map getAnnotationValues() + { + return annotationValues; + } + + public void visit( String name, Object value ) + { + annotationValues.put( name, value ); + logger.debug( "MojoAnnotationVisitor#visit:" + name + ":" + value ); + } + + public void visitEnum( String name, String desc, String value ) + { + annotationValues.put( name, value ); + logger.debug( "MojoAnnotationVisitor#visitEnum:" + name + ":" + desc + ":" + value ); + } + + public AnnotationVisitor visitAnnotation( String name, String desc ) + { + logger.debug( "MojoAnnotationVisitor#visitAnnotation:" + name + ":" + desc ); + return new MojoAnnotationVisitor( logger, this.annotationClassName ); + } + + public AnnotationVisitor visitArray( String s ) + { + logger.debug( "MojoAnnotationVisitor#visitArray" ); + return new MojoAnnotationVisitor( logger, this.annotationClassName ); + } + + public void visitEnd() + { + logger.debug( "MojoAnnotationVisitor#visitEnd" ); + } + + public String getAnnotationClassName() + { + return annotationClassName; + } +} diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoClassVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoClassVisitor.java new file mode 100644 index 0000000..673ba9f --- /dev/null +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoClassVisitor.java @@ -0,0 +1,160 @@ +package org.apache.maven.tools.plugin.annotations.scanner.visitors; +/* + * 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. + */ + +import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotatedClass; +import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotationsScanner; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.util.StringUtils; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.Attribute; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Type; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Olivier Lamy + */ +public class MojoClassVisitor + implements ClassVisitor +{ + private Logger logger; + + private MojoAnnotatedClass mojoAnnotatedClass; + + private Map annotationVisitorMap = new HashMap(); + + private List fieldVisitors = new ArrayList(); + + public MojoClassVisitor( Logger logger ) + { + this.logger = logger; + } + + public MojoAnnotatedClass getMojoAnnotatedClass() + { + return mojoAnnotatedClass; + } + + public void setMojoAnnotatedClass( MojoAnnotatedClass mojoAnnotatedClass ) + { + this.mojoAnnotatedClass = mojoAnnotatedClass; + } + + public Map getAnnotationVisitorMap() + { + return annotationVisitorMap; + } + + public void setAnnotationVisitorMap( Map annotationVisitorMap ) + { + this.annotationVisitorMap = annotationVisitorMap; + } + + public List getFieldVisitors() + { + return fieldVisitors; + } + + public void setFieldVisitors( List fieldVisitors ) + { + this.fieldVisitors = fieldVisitors; + } + + public List findFieldWithAnnotationClass( String annotationClassName ) + { + List mojoFieldVisitors = new ArrayList(); + + for ( MojoFieldVisitor mojoFieldVisitor : this.fieldVisitors ) + { + MojoAnnotationVisitor mojoAnnotationVisitor = mojoFieldVisitor.getMojoAnnotationVisitor(); + if ( mojoAnnotationVisitor != null && StringUtils.equals( annotationClassName, + mojoAnnotationVisitor.getAnnotationClassName() ) ) + { + mojoFieldVisitors.add( mojoFieldVisitor ); + } + } + + return mojoFieldVisitors; + } + + public void visit( int version, int access, String name, String signature, String superName, String[] interfaces ) + { + mojoAnnotatedClass = new MojoAnnotatedClass(); + mojoAnnotatedClass.setClassName( Type.getObjectType( name ).getClassName() ).setParentClassName( + Type.getObjectType( superName ).getClassName() ); + logger.debug( "MojoClassVisitor#visit" ); + } + + public AnnotationVisitor visitAnnotation( String desc, boolean visible ) + { + logger.debug( "MojoClassVisitor#visitAnnotation" ); + String annotationClassName = Type.getType( desc ).getClassName(); + if ( !MojoAnnotationsScanner.acceptedClassLevelAnnotationClasses.contains( annotationClassName ) ) + { + return null; + } + MojoAnnotationVisitor mojoAnnotationVisitor = new MojoAnnotationVisitor( logger, annotationClassName ); + annotationVisitorMap.put( annotationClassName, mojoAnnotationVisitor ); + return mojoAnnotationVisitor; + } + + public FieldVisitor visitField( int access, String name, String desc, String signature, Object value ) + { + // Type.getType( desc ).getClassName() + logger.debug( "MojoClassVisitor#visitField" ); + MojoFieldVisitor mojoFieldVisitor = new MojoFieldVisitor( logger, name ); + fieldVisitors.add( mojoFieldVisitor ); + return mojoFieldVisitor; + } + + public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions ) + { + // we don't need methods informations + return null; + } + + public void visitAttribute( Attribute attr ) + { + } + + public void visitSource( String source, String debug ) + { + } + + public void visitOuterClass( String owner, String name, String desc ) + { + } + + public void visitInnerClass( String name, String outerName, String innerName, int access ) + { + } + + public void visitEnd() + { + logger.debug( "MojoClassVisitor#visitEnd" ); + } + +} diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoFieldVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoFieldVisitor.java new file mode 100644 index 0000000..77fde45 --- /dev/null +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/annotations/scanner/visitors/MojoFieldVisitor.java @@ -0,0 +1,77 @@ +package org.apache.maven.tools.plugin.annotations.scanner.visitors; +/* + * 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. + */ + +import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotationsScanner; +import org.codehaus.plexus.logging.Logger; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.Attribute; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.Type; + +/** + * @author Olivier Lamy + */ +public class MojoFieldVisitor + implements FieldVisitor +{ + private Logger logger; + + private String fieldName; + + private MojoAnnotationVisitor mojoAnnotationVisitor; + + MojoFieldVisitor( Logger logger, String fieldName ) + { + this.logger = logger; + this.fieldName = fieldName; + } + + public MojoAnnotationVisitor getMojoAnnotationVisitor() + { + return mojoAnnotationVisitor; + } + + public String getFieldName() + { + return fieldName; + } + + public AnnotationVisitor visitAnnotation( String desc, boolean visible ) + { + logger.debug( "MojoFieldVisitor#visitAnnotation:" + desc ); + String annotationClassName = Type.getType( desc ).getClassName(); + if ( !MojoAnnotationsScanner.acceptedFieldLevelAnnotationClasses.contains( annotationClassName ) ) + { + return null; + } + mojoAnnotationVisitor = new MojoAnnotationVisitor( logger, annotationClassName ); + return mojoAnnotationVisitor; + } + + public void visitAttribute( Attribute attribute ) + { + logger.debug( "MojoFieldVisitor#visitAttribute" ); + } + + public void visitEnd() + { + logger.debug( "MojoFieldVisitor#visitEnd" ); + } +} diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/annotations/TestAnnotationsReader.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/annotations/TestAnnotationsReader.java index 586ba5f..70d2145 100644 --- a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/annotations/TestAnnotationsReader.java +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/annotations/TestAnnotationsReader.java @@ -18,11 +18,11 @@ package org.apache.maven.tools.plugin.annotations; * under the License. */ -import org.apache.maven.tools.plugin.annotations.scanner.ComponentAnnotationContent; +import org.apache.maven.tools.plugin.annotations.datamodel.ComponentAnnotationContent; import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotatedClass; import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotationsScanner; import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotationsScannerRequest; -import org.apache.maven.tools.plugin.annotations.scanner.ParameterAnnotationContent; +import org.apache.maven.tools.plugin.annotations.datamodel.ParameterAnnotationContent; import org.codehaus.plexus.PlexusTestCase; import org.fest.assertions.Assertions;