Issue #5475 - Changing ASM API version lookup to use Reflection against ASM Opcodes.
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
fa713979c0
commit
a3e3632340
|
@ -30,6 +30,7 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
@ -47,6 +48,7 @@ import org.objectweb.asm.ClassReader;
|
||||||
import org.objectweb.asm.ClassVisitor;
|
import org.objectweb.asm.ClassVisitor;
|
||||||
import org.objectweb.asm.FieldVisitor;
|
import org.objectweb.asm.FieldVisitor;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AnnotationParser
|
* AnnotationParser
|
||||||
|
@ -81,32 +83,37 @@ public class AnnotationParser
|
||||||
/**
|
/**
|
||||||
* Determine the runtime version of asm.
|
* 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()
|
private static int asmVersion()
|
||||||
{
|
{
|
||||||
// We need to search for the highest known ASM version.
|
// Find the highest available ASM version on the runtime/classpath, because
|
||||||
// If we run with a lower than known ASM version, then if run on a JVM with new language features
|
// if we run with a lower than available ASM version, against a class with
|
||||||
// we will get UnsupportedOperationsExceptions, even if the version of ASM is updated to support them.
|
// new language features we'll get an UnsupportedOperationException, even if
|
||||||
// If we run with a higher than known ASM version, then we will get a unknown version exception.
|
// the ASM version supports the new language features.
|
||||||
// So we need the Goldilocks version!
|
// Also, if we run with a higher than available ASM version, we'll get
|
||||||
int asmVersion = 7;
|
// an IllegalArgumentException from org.objectweb.asm.ClassVisitor.
|
||||||
while (true)
|
// 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;
|
return (int)Opcodes.class.getField("ASM" + asmFieldId).get(null);
|
||||||
new ClassVisitor(nextVersion << 16, null)
|
|
||||||
{};
|
|
||||||
asmVersion = nextVersion;
|
|
||||||
}
|
}
|
||||||
catch (Throwable th)
|
catch (Throwable e)
|
||||||
{
|
{
|
||||||
break;
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return asmVersion << 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert internal name to simple name
|
* Convert internal name to simple name
|
||||||
|
|
Loading…
Reference in New Issue