Skip to content

Commit

Permalink
Merge pull request #370 from elandau/bugfix/ignore_noclassdeffoundexc…
Browse files Browse the repository at this point in the history
…eption

Ignore NoClassDefFoundException during governator classpath scanning
  • Loading branch information
elandau authored Jul 6, 2017
2 parents c85c63f + 3e3f43b commit 0c471cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.netflix.governator.lifecycle;

import org.objectweb.asm.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.objectweb.asm.Type.ARRAY;
import static org.objectweb.asm.Type.BOOLEAN;
import static org.objectweb.asm.Type.BYTE;
import static org.objectweb.asm.Type.CHAR;
import static org.objectweb.asm.Type.DOUBLE;
import static org.objectweb.asm.Type.FLOAT;
import static org.objectweb.asm.Type.INT;
import static org.objectweb.asm.Type.LONG;
import static org.objectweb.asm.Type.OBJECT;
import static org.objectweb.asm.Type.SHORT;
import static org.objectweb.asm.Type.getArgumentTypes;
import static org.objectweb.asm.Type.getType;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
Expand All @@ -13,7 +22,14 @@
import java.util.HashSet;
import java.util.Set;

import static org.objectweb.asm.Type.*;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class AnnotationFinder extends ClassVisitor {
private static Logger log = LoggerFactory.getLogger(AnnotationFinder.class);
Expand All @@ -30,17 +46,16 @@ public final class AnnotationFinder extends ClassVisitor {
private ClassLoader classLoader;

private Class<?> selfClass() {
if(clazz == null)
if (clazz == null)
clazz = classFromInternalName(className);
return clazz;
}

private Class<?> classFromInternalName(String name) {
try {
return Class.forName(name.replace('/', '.'), false, classLoader);
}
catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Unable to find class " + name, e);
}
}

Expand Down Expand Up @@ -97,9 +112,10 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
try {
annotatedFields.add(selfClass().getDeclaredField(name));
break;
}
catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
} catch (NoSuchFieldException e) {
throw new IllegalStateException("Error visiting field " + name + " of class " + selfClass().getName(), e);
} catch (NoClassDefFoundError e) {
log.info("Unable to scan field '{}' of class '{}'", name, selfClass().getName(), e.getMessage());
}
}
}
Expand All @@ -123,15 +139,15 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
Type type = getType(desc);
for (Type annotationType : annotationTypes) {
if (annotationType.equals(type)) {
Type[] args = methodDesc == null ? new Type[0]
Type[] args = methodDesc == null
? new Type[0]
: getArgumentTypes(methodDesc);
Class[] argClasses = new Class[args.length];
for (int i = 0; i < args.length; i++) {
switch (args[i].getSort()) {
case OBJECT:
case ARRAY:
argClasses[i] = classFromInternalName(args[i]
.getInternalName());
argClasses[i] = classFromInternalName(args[i].getInternalName());
break;
case BOOLEAN:
argClasses[i] = boolean.class;
Expand Down Expand Up @@ -167,11 +183,9 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
else
annotatedMethods.add(selfClass().getDeclaredMethod(
name, argClasses));
}
catch (NoClassDefFoundError e) {
} catch (NoClassDefFoundError e) {
log.info("Unable to scan constructor of '{}' NoClassDefFoundError looking for '{}'", selfClass().getName(), e.getMessage());
}
catch (NoSuchMethodException e) {
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip

0 comments on commit 0c471cb

Please sign in to comment.