[MSHARED-792] Detect type annotations on local variables
by implementing DefaultMethodVisitor#visitLocalVariableAnnotation
Closes #5
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 @@
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 09fc24c..e5b90f1 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 @@
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/usageLocalVar/pom.xml b/src/test/resources/typeUseAnnotationDependency/usageLocalVar/pom.xml
new file mode 100644
index 0000000..26f42b7
--- /dev/null
+++ b/src/test/resources/typeUseAnnotationDependency/usageLocalVar/pom.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ 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.
+ -->
+
+<project
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>typeUseAnnotationDependencyUsageLocalVar</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0</version>
+
+ <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 = "";
+ }
+
+}