Skip to content

Commit

Permalink
#41 - exception type detection (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
csoroiu authored and olamy committed Mar 3, 2019
1 parent 9341849 commit fabc20f
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.buildResult=failure
# this is where the method signature change happened
invoker.java.version=1.8+
96 changes: 96 additions & 0 deletions animal-sniffer-maven-plugin/src/it/issue-41/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2009 codehaus.org.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localdomain.localhost</groupId>
<artifactId>real-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>issue-41</name>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>@mojo.java.target@</source>
<target>@mojo.java.target@</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>${pluginGroupId}</groupId>
<artifactId>${pluginArtifactId}</artifactId>
<version>${pluginVersion}</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java15</artifactId>
<version>1.0</version>
</signature>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<pluginGroupId>@project.groupId@</pluginGroupId>
<pluginArtifactId>@project.artifactId@</pluginArtifactId>
<pluginVersion>@project.version@</pluginVersion>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package localhost;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Issue41Bad
{

public void exceptionMultiCatch() {
try
{
Method method = Issue41Bad.class.getDeclaredMethod( "emptyMethod" );
method.invoke( null );
}
catch ( NoSuchMethodException | IllegalAccessException | InvocationTargetException exception )
{
logException( exception );
}
}

public void exceptionClassNotAvailableInJava6()
{
try
{
Method method = Issue41Bad.class.getDeclaredMethod( "emptyMethod" );
method.invoke( null );
}
catch ( ReflectiveOperationException exception )
{
logException( exception );
}
}

public void exceptionMixedCatches() {
try
{
Method method = Issue41Bad.class.getDeclaredMethod( "emptyMethod" );
method.invoke( null );
}
catch ( NoSuchMethodException exception )
{
logException( exception );
}
catch ( IllegalAccessException | InvocationTargetException exception )
{
logException( exception );
}
catch ( ReflectiveOperationException exception )
{
logException( exception );
}
}

private void logException( Throwable e )
{
e.printStackTrace();
}
}
9 changes: 9 additions & 0 deletions animal-sniffer-maven-plugin/src/it/issue-41/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
File log = new File(basedir, 'build.log')
assert log.exists()
assert !log.text.contains( 'Issue41Bad.java: Undefined reference: ReflectiveOperationException' )
assert log.text.contains( 'Issue41Bad.java:15: Undefined reference: ReflectiveOperationException' )
assert !log.text.contains( 'Issue41Bad.java:19: Undefined reference: ReflectiveOperationException' )
assert log.text.contains( 'Issue41Bad.java:28: Undefined reference: ReflectiveOperationException' )
assert !log.text.contains( 'Issue41Bad.java:32: Undefined reference: ReflectiveOperationException' )
assert log.text.contains( 'Issue41Bad.java:44: Undefined reference: ReflectiveOperationException' )
assert log.text.contains( 'Issue41Bad.java:48: Undefined reference: ReflectiveOperationException' )
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected void process( final String name, InputStream image )
}
}

private static interface MatchRule
private interface MatchRule
{
boolean matches( String text );
}
Expand Down Expand Up @@ -354,12 +354,15 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible)
@Override
public MethodVisitor visitMethod( int access, final String name, final String desc, String signature, String[] exceptions )
{
line = 0;
return new MethodVisitor(Opcodes.ASM5)
{
/**
* True if @IgnoreJRERequirement is set.
*/
boolean ignoreError = ignoreClass;
Label label = null;
Map<Label, Set<String>> exceptions = new HashMap<Label, Set<String>>();

@Override
public AnnotationVisitor visitAnnotation( String annoDesc, boolean visible )
Expand Down Expand Up @@ -410,13 +413,59 @@ public void visitFieldInsn( int opcode, String owner, String name, String desc )
check( owner, name + '#' + desc );
}

@Override
public void visitTryCatchBlock( Label start, Label end, Label handler, String type )
{
if ( type != null )
{
Set<String> exceptionTypes = exceptions.get( handler );
if ( exceptionTypes == null )
{
exceptionTypes = new HashSet<String>();
exceptions.put( handler, exceptionTypes );
}
// we collect the types for the handler
// because we do not have the line number here
// and we need a list for a multi catch block
exceptionTypes.add( type );
}
}

@Override
public void visitFrame( int type, int nLocal, Object[] local, int nStack, Object[] stack )
{
Set<String> exceptionTypes = exceptions.remove(label);
if ( exceptionTypes != null )
{
for (String exceptionType: exceptionTypes)
{
checkType( exceptionType );
}
for ( int i = 0; i < nStack; i++ )
{
Object obj = stack[i];
// on the frame stack we check if we have a type which is not
// present in the catch/multi catch statement
if ( obj instanceof String && !exceptionTypes.contains( obj ) )
{
checkType( obj.toString() );
}
}
}
}

@Override
public void visitLineNumber( int line, Label start )
{
CheckingVisitor.this.line = line;
}

private void checkType( Type asmType )
@Override
public void visitLabel( Label label ) {
this.label = label;
}

private void checkType(Type asmType )
{
if ( asmType == null )
{
Expand Down Expand Up @@ -512,7 +561,7 @@ private boolean find( Clazz c , String sig , boolean baseFind )
return false;
}

if ( find( (Clazz) classes.get( c.getSuperClass() ), sig, false ) )
if ( find( classes.get( c.getSuperClass() ), sig, false ) )
{
return true;
}
Expand Down

0 comments on commit fabc20f

Please sign in to comment.