/
RealignmentJDSourceMapper.java
237 lines (206 loc) · 7.66 KB
/
RealignmentJDSourceMapper.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package jd.ide.eclipse.realignment.editors;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jd.ide.eclipse.JavaDecompilerPlugin;
import jd.ide.eclipse.editors.JDClassFileEditor;
import jd.ide.eclipse.editors.JDSourceMapper;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.core.BufferManager;
import org.eclipse.jdt.internal.core.SourceMapper;
import org.eclipse.jface.preference.IPreferenceStore;
/**
* RealignmentJDSourceMapper
*
* @project Java Decompiler Eclipse Plugin
* @author Alex Kosinsky
*/
@SuppressWarnings("restriction")
public class RealignmentJDSourceMapper extends JDSourceMapper
{
public static class SourceAttachmentDetails
{
public final IClasspathEntry newEntry;
public final IJavaProject project;
public final IPath containerPath;
public final boolean isReferencedEntry;
public SourceAttachmentDetails(IClasspathEntry newEntry,
IJavaProject project, IPath containerPath, boolean isReferencedEntry)
{
this.newEntry = newEntry;
this.project = project;
this.containerPath = containerPath;
this.isReferencedEntry = isReferencedEntry;
}
}
private static final Map<JDSourceMapper, Set<IClassFile>> decompiledClasses = new HashMap<JDSourceMapper, Set<IClassFile>>();
public static synchronized void addDecompiled(JDSourceMapper mapper, IClassFile file)
{
Set<IClassFile> set = decompiledClasses.get(mapper);
if (set == null)
{
set = new HashSet<IClassFile>();
decompiledClasses.put(mapper, set);
}
set.add(file);
}
public static synchronized void clearDecompiled(JDSourceMapper mapper)
{
Set<IClassFile> set = decompiledClasses.remove(mapper);
for (IClassFile file : set)
{
BufferManager bufferManager = BufferManager.getDefaultBufferManager();
IBuffer buffer = bufferManager.getBuffer(file);
if (buffer != null)
{
try
{
// Remove the buffer
Method method = BufferManager.class.getDeclaredMethod(
"removeBuffer", new Class[] {IBuffer.class});
method.setAccessible(true);
method.invoke(bufferManager, new Object[] {buffer});
}
catch (Exception e)
{
JavaDecompilerPlugin.getDefault().getLog().log(new Status(
Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
0, e.getMessage(), e));
}
}
}
}
@SuppressWarnings("rawtypes")
public static SourceMapper newSourceMapper(IPath rootPath, IPath sourcePath,
String sourceRootPath, Map options,
SourceAttachmentDetails details)
{
try
{
return new RealignmentJDSourceMapper(
rootPath, sourcePath, sourceRootPath, options,
createJDSourceMapper(rootPath, sourcePath, sourceRootPath, options),
details);
}
catch (Exception e)
{
JavaDecompilerPlugin.getDefault().getLog().log(new Status(
Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
0, e.getMessage(), e));
}
return null;
}
static JDSourceMapper createJDSourceMapper( IPath rootPath, IPath sourcePath, String sourceRootPath, Map<?,?> options)
throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException{
Method method = JDClassFileEditor.class.getDeclaredMethod(
"newSourceMapper", new Class[] {IPath.class,IPath.class,String.class,Map.class});
method.setAccessible(true);
JDSourceMapper sourceMapper=(JDSourceMapper)method.invoke(
getJDClassFileEditorClass().newInstance(),new Object[] {rootPath,sourcePath,sourceRootPath,options});
return sourceMapper;
}
static Class<?> getJDClassFileEditorClass() throws ClassNotFoundException{
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint point = registry.getExtensionPoint("org.eclipse.ui.editors");
if (point == null)
return null;
IExtension[] extensions = point.getExtensions();
for (int i = 0; i < extensions.length; i++){
IConfigurationElement[] elements=extensions[i].getConfigurationElements();
for(int j=0;j<elements.length;j++){
String id=elements[j].getAttribute("id");
if(id.indexOf("jd.ide.eclipse.editors.JDClassFileEditor")==0)
return Class.forName(elements[j].getAttribute("class"));
}
}
return null;
}
String libraryPath=null;
public final SourceAttachmentDetails sourceDetails;
private final IPath classPath;
public RealignmentJDSourceMapper(IPath classePath,
IPath sourcePath,
String sourceRootPath,
Map<?,?> options,
JDSourceMapper sourceMapper,
SourceAttachmentDetails details)
{
super(classePath, sourcePath, sourceRootPath,options);
classPath = classePath;
this.sourceDetails = details;
Method method;
try {
method = sourceMapper.getClass().getDeclaredMethod(
"getLibraryPath", new Class[] {});
method.setAccessible(true);
libraryPath=(String)method.invoke(sourceMapper,new Object[] {});
} catch (Exception e) {
JavaDecompilerPlugin.getDefault().getLog().log(new Status(
Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
0, e.getMessage(), e));
}
}
@Override
public char[] findSource(String javaSourcePath)
{
char[] source = null;
// Decompile class file
String javaClassPath = getJavaClassPath(javaSourcePath);
if (javaClassPath != null)
source = findSource(this.classPath, javaClassPath);
return source;
}
private final static String JAVA_CLASS_SUFFIX = ".class";
private final static String JAVA_SOURCE_SUFFIX = ".java";
private final static int JAVA_SOURCE_SUFFIX_LENGTH = 5;
private static String getJavaClassPath(String javaSourcePath)
{
int index = javaSourcePath.length() - JAVA_SOURCE_SUFFIX_LENGTH;
if (javaSourcePath.regionMatches(
true, index, JAVA_SOURCE_SUFFIX,
0, JAVA_SOURCE_SUFFIX_LENGTH))
return javaSourcePath.substring(0, index) + JAVA_CLASS_SUFFIX;
return null;
}
@Override
public char[] findSource(IType type, IBinaryType info) {
IClassFile classFile = (IClassFile) type.getParent();
addDecompiled(this, classFile);
return super.findSource(type, info);
}
@Override
public char[] findSource(IPath path, String javaClassPath)
{
IPreferenceStore store = JavaDecompilerPlugin.getDefault().getPreferenceStore();
boolean saveValLineNumbers= store.getBoolean(JavaDecompilerPlugin.PREF_DISPLAY_LINE_NUMBERS);
char[] output=super.findSource(path,javaClassPath);
store.setValue(JavaDecompilerPlugin.PREF_DISPLAY_LINE_NUMBERS,saveValLineNumbers);
DecompilerOutputUtil decompilerOutputUtil=new DecompilerOutputUtil(new String(output));
output=decompilerOutputUtil.realign();
return output;
}
@Override
protected String getLibraryPath() throws IOException {
return libraryPath;
}
public String doDecompiling(String baseName, String qualifiedName) throws IOException{
loadLibrary();
return super.decompile(baseName,qualifiedName);
}
}