blob: b7a5e249f74356f610e9c9425740516632f71b09 (
plain) (
blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.mapping;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javassist.ByteArrayClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import javassist.bytecode.Descriptor;
import com.google.common.collect.Maps;
import cuchaz.enigma.Constants;
public class Ancestries implements Serializable
{
private static final long serialVersionUID = 738687982126844179L;
private Map<String,String> m_superclasses;
public Ancestries( )
{
m_superclasses = Maps.newHashMap();
}
public void readFromJar( InputStream in )
throws IOException
{
ClassPool classPool = new ClassPool();
ZipInputStream zin = new ZipInputStream( in );
ZipEntry entry;
while( ( entry = zin.getNextEntry() ) != null )
{
// filter out non-classes
if( entry.isDirectory() || !entry.getName().endsWith( ".class" ) )
{
continue;
}
// read the class into a buffer
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[Constants.KiB];
int totalNumBytesRead = 0;
while( zin.available() > 0 )
{
int numBytesRead = zin.read( buf );
if( numBytesRead < 0 )
{
break;
}
bos.write( buf, 0, numBytesRead );
// sanity checking
totalNumBytesRead += numBytesRead;
if( totalNumBytesRead > Constants.MiB )
{
throw new Error( "Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!" );
}
}
// determine the class name (ie chop off the ".class")
String className = Descriptor.toJavaName( entry.getName().substring( 0, entry.getName().length() - ".class".length() ) );
// get a javassist handle for the class
classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) );
try
{
CtClass c = classPool.get( className );
addSuperclass( c.getName(), c.getClassFile().getSuperclass() );
}
catch( NotFoundException ex )
{
throw new Error( "Unable to load class: " + className );
}
}
}
public void addSuperclass( String className, String superclassName )
{
className = Descriptor.toJvmName( className );
superclassName = Descriptor.toJvmName( superclassName );
if( className.equals( superclassName ) )
{
throw new IllegalArgumentException( "Class cannot be its own superclass! " + className );
}
if( !isJre( className ) && !isJre( superclassName ) )
{
m_superclasses.put( className, superclassName );
}
}
public String getSuperclassName( String className )
{
return m_superclasses.get( className );
}
public List<String> getAncestry( String className )
{
List<String> ancestors = new ArrayList<String>();
while( className != null )
{
className = getSuperclassName( className );
ancestors.add( className );
}
return ancestors;
}
private boolean isJre( String className )
{
return className.startsWith( "java/" )
|| className.startsWith( "javax/" );
}
}
|