You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2019/01/18 00:42:50 UTC

[maven-dependency-analyzer] 01/01: [MSHARED-792] Detect type annotations on local variables

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch MSHARED-792
in repository https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git

commit 4edc29ef0c734ad1f659cb62333e66fe71dbe06b
Author: Andreas Hubold <an...@coremedia.com>
AuthorDate: Thu Jan 3 09:56:12 2019 +0100

    [MSHARED-792] Detect type annotations on local variables
    
    by implementing DefaultMethodVisitor#visitLocalVariableAnnotation
---
 .../analyzer/asm/DefaultMethodVisitor.java         |  9 +++++++
 .../DefaultProjectDependencyAnalyzerTest.java      | 26 +++++++++++++++++++
 .../resources/typeUseAnnotationDependency/pom.xml  |  1 +
 .../{ => usageLocalVar}/pom.xml                    | 18 +++++++------
 .../usageLocalVar/UsageLocalVar.java               | 30 ++++++++++++++++++++++
 5 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultMethodVisitor.java b/src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultMethodVisitor.java
index 24ca79d..2721e24 100644
--- a/src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultMethodVisitor.java
+++ b/src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultMethodVisitor.java
@@ -76,6 +76,15 @@ public class DefaultMethodVisitor
         return annotationVisitor;
     }
 
+    @Override
+    public AnnotationVisitor visitLocalVariableAnnotation( int typeRef, TypePath typePath, Label[] start, Label[] end,
+                                                           int[] index, String desc, boolean visible )
+    {
+        resultCollector.addDesc( desc );
+
+        return annotationVisitor;
+    }
+
     public void visitTypeInsn( final int opcode, final String desc )
     {
         if ( desc.charAt( 0 ) == '[' )
diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
index 84b0d48..78bdca7 100644
--- a/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
+++ b/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
@@ -328,6 +328,32 @@ public class DefaultProjectDependencyAnalyzerTest
         assertEquals( expectedAnalysis, actualAnalysis );
     }
 
+    public void testTypeUseAnnotationDependencyOnLocalVariable()
+            throws TestToolsException, ProjectDependencyAnalyzerException
+    {
+        // java.lang.annotation.ElementType.TYPE_USE introduced with Java 1.8
+        if ( !SystemUtils.isJavaVersionAtLeast( JavaVersion.JAVA_1_8 ) )
+        {
+            return;
+        }
+
+        Properties properties = new Properties();
+        properties.put( "maven.compiler.source", "1.8" );
+        properties.put( "maven.compiler.target", "1.8" );
+        compileProject( "typeUseAnnotationDependency/pom.xml", properties);
+
+        MavenProject usage = getProject( "typeUseAnnotationDependency/usageLocalVar/pom.xml" );
+
+        ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( usage );
+
+        Artifact annotation = createArtifact( "org.apache.maven.shared.dependency-analyzer.tests",
+                                            "typeUseAnnotationDependencyAnnotation", "jar", "1.0", "compile" );
+        Set<Artifact> usedDeclaredArtifacts = Collections.singleton( annotation );
+        ProjectDependencyAnalysis expectedAnalysis = new ProjectDependencyAnalysis(usedDeclaredArtifacts, null, null);
+
+        assertEquals( expectedAnalysis, actualAnalysis );
+    }
+
     // private methods --------------------------------------------------------
 
     private void compileProject( String pomPath )
diff --git a/src/test/resources/typeUseAnnotationDependency/pom.xml b/src/test/resources/typeUseAnnotationDependency/pom.xml
index 62e966c..73bc5ec 100644
--- a/src/test/resources/typeUseAnnotationDependency/pom.xml
+++ b/src/test/resources/typeUseAnnotationDependency/pom.xml
@@ -33,6 +33,7 @@
 	<modules>
 		<module>annotation</module>
 		<module>usage</module>
+		<module>usageLocalVar</module>
 	</modules>
 	
 </project>
diff --git a/src/test/resources/typeUseAnnotationDependency/pom.xml b/src/test/resources/typeUseAnnotationDependency/usageLocalVar/pom.xml
similarity index 79%
copy from src/test/resources/typeUseAnnotationDependency/pom.xml
copy to src/test/resources/typeUseAnnotationDependency/usageLocalVar/pom.xml
index 62e966c..26f42b7 100644
--- a/src/test/resources/typeUseAnnotationDependency/pom.xml
+++ b/src/test/resources/typeUseAnnotationDependency/usageLocalVar/pom.xml
@@ -26,13 +26,15 @@
 >
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
-	<artifactId>typeUseAnnotationDependency</artifactId>
-	<packaging>pom</packaging>
+	<artifactId>typeUseAnnotationDependencyUsageLocalVar</artifactId>
+	<packaging>jar</packaging>
 	<version>1.0</version>
-	
-	<modules>
-		<module>annotation</module>
-		<module>usage</module>
-	</modules>
-	
+
+	<dependencies>
+		<dependency>
+			<groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+			<artifactId>typeUseAnnotationDependencyAnnotation</artifactId>
+			<version>1.0</version>
+		</dependency>
+	</dependencies>
 </project>
diff --git a/src/test/resources/typeUseAnnotationDependency/usageLocalVar/src/main/java/typeUseAnnotationDependency/usageLocalVar/UsageLocalVar.java b/src/test/resources/typeUseAnnotationDependency/usageLocalVar/src/main/java/typeUseAnnotationDependency/usageLocalVar/UsageLocalVar.java
new file mode 100644
index 0000000..e1d8353
--- /dev/null
+++ b/src/test/resources/typeUseAnnotationDependency/usageLocalVar/src/main/java/typeUseAnnotationDependency/usageLocalVar/UsageLocalVar.java
@@ -0,0 +1,30 @@
+package typeUseAnnotationDependency.usageLocalVar;
+
+import typeUseAnnotationDependency.annotation.Annotation;
+
+/*
+ * 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.
+ */
+
+public class UsageLocalVar {
+
+  public void getValue() {
+    @Annotation String s = "";
+  }
+  
+}