Commit 3fba94f0999ab9475e2997e2b4a6fc9ce79562e3
1 parent
d0bae58d
Prevent ASM ClassWriter from swallowing important exceptions
Showing
1 changed file
with
70 additions
and
22 deletions
java/common/com/mumfrey/liteloader/transformers/IsolatedClassWriter.java
| 1 | -package com.mumfrey.liteloader.transformers; | ||
| 2 | - | ||
| 3 | -import org.objectweb.asm.ClassReader; | ||
| 4 | -import org.objectweb.asm.ClassWriter; | ||
| 5 | - | ||
| 6 | -/** | ||
| 7 | - * ClassWriter isolated from ASM so that it exists in the LaunchClassLoader | ||
| 8 | - * | ||
| 9 | - * @author Adam Mummery-Smith | ||
| 10 | - */ | ||
| 11 | -public class IsolatedClassWriter extends ClassWriter | ||
| 12 | -{ | ||
| 13 | - public IsolatedClassWriter(int flags) | ||
| 14 | - { | ||
| 15 | - super(flags); | ||
| 16 | - } | ||
| 17 | - | ||
| 18 | - public IsolatedClassWriter(ClassReader classReader, int flags) | ||
| 19 | - { | ||
| 20 | - super(classReader, flags); | ||
| 21 | - } | ||
| 22 | -} | 1 | +package com.mumfrey.liteloader.transformers; |
| 2 | + | ||
| 3 | +import org.objectweb.asm.ClassReader; | ||
| 4 | +import org.objectweb.asm.ClassWriter; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * ClassWriter isolated from ASM so that it exists in the LaunchClassLoader | ||
| 8 | + * | ||
| 9 | + * @author Adam Mummery-Smith | ||
| 10 | + */ | ||
| 11 | +public class IsolatedClassWriter extends ClassWriter | ||
| 12 | +{ | ||
| 13 | + public IsolatedClassWriter(int flags) | ||
| 14 | + { | ||
| 15 | + super(flags); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + public IsolatedClassWriter(ClassReader classReader, int flags) | ||
| 19 | + { | ||
| 20 | + super(classReader, flags); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Returns the common super type of the two given types. The default | ||
| 25 | + * implementation of this method <i>loads</i> the two given classes and uses | ||
| 26 | + * the java.lang.Class methods to find the common super class. It can be | ||
| 27 | + * overridden to compute this common super type in other ways, in particular | ||
| 28 | + * without actually loading any class, or to take into account the class | ||
| 29 | + * that is currently being generated by this ClassWriter, which can of | ||
| 30 | + * course not be loaded since it is under construction. | ||
| 31 | + * | ||
| 32 | + * @param type1 the internal name of a class. | ||
| 33 | + * @param type2 the internal name of another class. | ||
| 34 | + * @return the internal name of the common super class of the two given | ||
| 35 | + * classes. | ||
| 36 | + */ | ||
| 37 | + @Override | ||
| 38 | + protected String getCommonSuperClass(final String type1, final String type2) | ||
| 39 | + { | ||
| 40 | + Class<?> c, d; | ||
| 41 | + ClassLoader classLoader = getClass().getClassLoader(); | ||
| 42 | + try | ||
| 43 | + { | ||
| 44 | + c = Class.forName(type1.replace('/', '.'), false, classLoader); | ||
| 45 | + d = Class.forName(type2.replace('/', '.'), false, classLoader); | ||
| 46 | + } | ||
| 47 | + catch (Exception e) | ||
| 48 | + { | ||
| 49 | + throw new RuntimeException(e.toString(), e); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + if (c.isAssignableFrom(d)) | ||
| 53 | + { | ||
| 54 | + return type1; | ||
| 55 | + } | ||
| 56 | + if (d.isAssignableFrom(c)) | ||
| 57 | + { | ||
| 58 | + return type2; | ||
| 59 | + } | ||
| 60 | + if (c.isInterface() || d.isInterface()) | ||
| 61 | + { | ||
| 62 | + return "java/lang/Object"; | ||
| 63 | + } | ||
| 64 | + do | ||
| 65 | + { | ||
| 66 | + c = c.getSuperclass(); | ||
| 67 | + } while (!c.isAssignableFrom(d)); | ||
| 68 | + return c.getName().replace('.', '/'); | ||
| 69 | + } | ||
| 70 | +} |