summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/struct/lazy/LazyLoader.java
blob: 94cef9ad2355008e91d5ad693c7ce0575d7ce2b4 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 *    Fernflower - The Analytical Java Decompiler
 *    http://www.reversed-java.com
 *
 *    (C) 2008 - 2010, Stiver
 *
 *    This software is NEITHER public domain NOR free software 
 *    as per GNU License. See license.txt for more details.
 *
 *    This software is distributed WITHOUT ANY WARRANTY; without 
 *    even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 *    A PARTICULAR PURPOSE. 
 */

package org.jetbrains.java.decompiler.struct.lazy;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.util.DataInputFullStream;

public class LazyLoader {

	private HashMap<String, Link> mapClassLinks = new HashMap<String, Link>(); 
	
	private IBytecodeProvider provider;
	
	public LazyLoader(IBytecodeProvider provider) {
		this.provider = provider;
	}
	
	public void addClassLink(String classname, Link link) {
		mapClassLinks.put(classname, link);
	}

	public void removeClassLink(String classname) {
		mapClassLinks.remove(classname);
	}
	
	public Link getClassLink(String classname) {
		return mapClassLinks.get(classname);
	}
	
	
	public ConstantPool loadPool(String classname) {

		try {
			
			DataInputFullStream in = getClassStream(classname); 
			if(in == null) {
				return null;
			}
			
			in.skip(8);
			
			return new ConstantPool(in); 
			
		} catch(IOException ex) {
			throw new RuntimeException(ex);
		}
	}
	
	public byte[] loadBytecode(StructMethod mt, int code_fulllength) {
		
		try {

			DataInputFullStream in = getClassStream(mt.getClassStruct().qualifiedName); 
			if(in == null) {
				return null;
			}

			byte[] res = null;
			
			in.skip(8);

			ConstantPool pool = mt.getClassStruct().getPool(); 
			if(pool == null) {
				pool = new ConstantPool(in); 
			} else {
				ConstantPool.skipPool(in);
			}

			in.skip(2);
		    int this_class = in.readUnsignedShort();
			in.skip(2);
			
			// interfaces
			in.skip(in.readUnsignedShort() * 2);

			// fields
			int size = in.readUnsignedShort();
			for (int i = 0; i < size; i++) {
				in.skip(6);
				skipAttributes(in);
			}

			// methods
			size = in.readUnsignedShort();
			for (int i = 0; i < size; i++) {
				in.skip(2);

				int name_index = in.readUnsignedShort();
				int descriptor_index = in.readUnsignedShort();

				String elem_arr[] = pool.getClassElement(ConstantPool.METHOD, this_class, name_index, descriptor_index);
				String name = elem_arr[0];
				
				if(mt.getName().equals(name)) {
					String descriptor = elem_arr[1];
					if(mt.getDescriptor().equals(descriptor)) {

						int len = in.readUnsignedShort();
						for(int j=0;j<len;j++) {

							int attr_nameindex = in.readUnsignedShort();
							String attrname = pool.getPrimitiveConstant(attr_nameindex).getString();

							if(StructGeneralAttribute.ATTRIBUTE_CODE.equals(attrname)) {
								in.skip(12);

								res = new byte[code_fulllength];
								in.readFull(res);
								return res;
							} else {
								in.skip(in.readInt());
							}
						}

						return null;
					}
				}

				skipAttributes(in);
			}

			return null;

		} catch(IOException ex) {
			throw new RuntimeException(ex);
		}
		
	}

	public DataInputFullStream getClassStream(String externPath, String internPath) throws IOException {
		InputStream instream = provider.getBytecodeStream(externPath, internPath);
		return instream == null?null:new DataInputFullStream(instream);
	}

	public DataInputFullStream getClassStream(String qualifiedClassName) throws IOException {
		Link link = mapClassLinks.get(qualifiedClassName);
		return link == null?null:getClassStream(link.externPath, link.internPath);
	}
	
	private void skipAttributes(DataInputFullStream in) throws IOException {

		int length = in.readUnsignedShort();
	    for (int i = 0; i < length; i++) {
	    	in.skip(2);
	    	in.skip(in.readInt());
	    }
	    
	}

	
	public static class Link {
		
		public static final int CLASS = 1; 
		public static final int ENTRY = 2; 
		
		public int type;
		public String externPath;
		public String internPath;
		
		public Link(int type, String externPath, String internPath) {
			this.type = type;
			this.externPath = externPath;
			this.internPath = internPath;
		}
	}
	
}