From a44d4f1719b825acb29c04ceec38da5c3f199762 Mon Sep 17 00:00:00 2001 From: Robert Scholte Date: Tue, 19 Jul 2016 19:27:24 +0000 Subject: [PATCH] [MPLUGIN-304] MojoAnnotationsScanner should ignore special classes git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1753419 13f79535-47bb-0310-9956-ffa450edef68 --- .../DefaultMojoAnnotationsScanner.java | 41 ++++++++++++------ .../DefaultMojoAnnotationsScannerTest.java | 35 +++++++++++++++ .../src/test/resources/java9-module.jar | Bin 0 -> 266 bytes 3 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java create mode 100644 maven-plugin-tools-annotations/src/test/resources/java9-module.jar diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java index 8796274..f4c8301 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java @@ -19,6 +19,18 @@ package org.apache.maven.tools.plugin.extractor.annotations.scanner; * under the License. */ +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + import org.apache.maven.artifact.Artifact; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Execute; @@ -41,17 +53,6 @@ import org.codehaus.plexus.util.reflection.ReflectorException; import org.objectweb.asm.ClassReader; import org.objectweb.asm.Type; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - /** * @author Olivier Lamy * @since 3.0 @@ -61,6 +62,9 @@ public class DefaultMojoAnnotationsScanner extends AbstractLogEnabled implements MojoAnnotationsScanner { + // classes with a dash must be ignored + private static final Pattern SCANNABLE_CLASS = Pattern.compile( "[^-]+\\.class" ); + private Reflector reflector = new Reflector(); public Map scan( MojoAnnotationsScannerRequest request ) @@ -126,19 +130,28 @@ public class DefaultMojoAnnotationsScanner ZipInputStream archiveStream = new ZipInputStream( new FileInputStream( archiveFile ) ); + String zipEntryName = null; try { + for ( ZipEntry zipEntry = archiveStream.getNextEntry(); zipEntry != null; zipEntry = archiveStream.getNextEntry() ) { - if ( !zipEntry.getName().endsWith( ".class" ) ) + zipEntryName = zipEntry.getName(); + if ( !SCANNABLE_CLASS.matcher( zipEntryName ).matches() ) { continue; } - analyzeClassStream( mojoAnnotatedClasses, archiveStream, artifact, excludeMojo ); } } + catch ( IllegalArgumentException e ) + { + // In case of a class with newer specs an IllegalArgumentException can be thrown + getLogger().error( "Failed to analyze " + archiveFile.getAbsolutePath() + "!/" + zipEntryName ); + + throw e; + } finally { IOUtil.close( archiveStream ); @@ -174,7 +187,7 @@ public class DefaultMojoAnnotationsScanner for ( String classFile : classFiles ) { - if ( !classFile.endsWith( ".class" ) ) + if ( !SCANNABLE_CLASS.matcher( classFile ).matches() ) { continue; } diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java new file mode 100644 index 0000000..6218848 --- /dev/null +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java @@ -0,0 +1,35 @@ +package org.apache.maven.tools.plugin.extractor.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 java.io.File; + +import junit.framework.TestCase; + +public class DefaultMojoAnnotationsScannerTest + extends TestCase +{ + private DefaultMojoAnnotationsScanner scanner = new DefaultMojoAnnotationsScanner(); + + public void testSkipModuleInfoClassInArchive() throws Exception + { + scanner.scanArchive( new File( "src/test/resources/java9-module.jar"), null, false ); + } +} diff --git a/maven-plugin-tools-annotations/src/test/resources/java9-module.jar b/maven-plugin-tools-annotations/src/test/resources/java9-module.jar new file mode 100644 index 0000000000000000000000000000000000000000..5a7edc016613a0a95601d4f2b03bd1bc1f858d66 GIT binary patch literal 266 zcmWIWW@Zs#VBp|j@C(1}p_(o&(Z;~QunULg0Sr$Bc(byB O