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