Skip to content

Commit

Permalink
Issue #5475 - Changing ASM API version lookup to use Reflection again…
Browse files Browse the repository at this point in the history
…st ASM Opcodes.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Oct 20, 2020
1 parent fa71397 commit a3e3632
Showing 1 changed file with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -47,6 +48,7 @@
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
* AnnotationParser
Expand Down Expand Up @@ -81,31 +83,36 @@ public class AnnotationParser
/**
* Determine the runtime version of asm.
*
* @return the org.objectweb.asm.Opcode matching the runtime version of asm.
* @return the {@link org.objectweb.asm.Opcodes} ASM value matching the runtime version of asm.
*/
private static int asmVersion()
{
// We need to search for the highest known ASM version.
// If we run with a lower than known ASM version, then if run on a JVM with new language features
// we will get UnsupportedOperationsExceptions, even if the version of ASM is updated to support them.
// If we run with a higher than known ASM version, then we will get a unknown version exception.
// So we need the Goldilocks version!
int asmVersion = 7;
while (true)
// Find the highest available ASM version on the runtime/classpath, because
// if we run with a lower than available ASM version, against a class with
// new language features we'll get an UnsupportedOperationException, even if
// the ASM version supports the new language features.
// Also, if we run with a higher than available ASM version, we'll get
// an IllegalArgumentException from org.objectweb.asm.ClassVisitor.
// So must find exactly the maximum ASM api version available.

Optional<Integer> asmVersion = Arrays.stream(Opcodes.class.getFields()).sequential()
.filter((f) -> f.getName().matches("ASM[0-9]+"))
.map((f) -> f.getName().substring(3))
.map(Integer::parseInt)
.max(Integer::compareTo);

if (!asmVersion.isPresent())
throw new IllegalStateException("Invalid " + Opcodes.class.getName());

int asmFieldId = asmVersion.get();
try
{
try
{
int nextVersion = asmVersion + 1;
new ClassVisitor(nextVersion << 16, null)
{};
asmVersion = nextVersion;
}
catch (Throwable th)
{
break;
}
return (int)Opcodes.class.getField("ASM" + asmFieldId).get(null);
}
catch (Throwable e)
{
throw new IllegalStateException(e);
}
return asmVersion << 16;
}

/**
Expand Down

0 comments on commit a3e3632

Please sign in to comment.