forked from cpupk/ecd
-
Notifications
You must be signed in to change notification settings - Fork 57
/
DecompileUtil.java
190 lines (171 loc) · 5.94 KB
/
DecompileUtil.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
/*******************************************************************************
* Copyright (c) 2017 Chen Chao and other ECD project contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.sf.feeling.decompiler.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.SourceMapper;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.sf.feeling.decompiler.editor.DecompilerSourceMapper;
import org.sf.feeling.decompiler.editor.DecompilerType;
import org.sf.feeling.decompiler.editor.SourceMapperFactory;
public class DecompileUtil
{
public static String decompile( IClassFile cf, String type, boolean always, boolean reuseBuf, boolean force )
throws CoreException
{
String decompilerType = type;
String origSrc = cf.getSource( );
// have to check our mark since all line comments are stripped
// in debug align mode
if ( origSrc == null
|| always
|| !reuseBuf
|| force )
{
DecompilerSourceMapper sourceMapper = SourceMapperFactory.getSourceMapper( decompilerType );
char[] src = sourceMapper.findSource( cf.getType( ) );
if ( src == null && !DecompilerType.FernFlower.equals( decompilerType ) )
{
src = SourceMapperFactory.getSourceMapper( DecompilerType.FernFlower ).findSource( cf.getType( ) );
}
if ( src == null )
{
return origSrc;
}
else
return new String( src );
}
return origSrc;
}
public static String decompiler( FileStoreEditorInput input, String decompilerType )
{
DecompilerSourceMapper sourceMapper = SourceMapperFactory.getSourceMapper( decompilerType );
File file = new File( input.getURI( ) );
return sourceMapper.decompile( decompilerType, file );
}
public static String getPackageName( String source )
{
Pattern p = Pattern.compile( "(?i)package\\s+\\S+" ); //$NON-NLS-1$
Matcher m = p.matcher( source );
if ( m.find( ) )
{
return m.group( )
.replace( "package", "" ) //$NON-NLS-1$ //$NON-NLS-2$
.replace( ";", "" ) //$NON-NLS-1$ //$NON-NLS-2$
.trim( );
}
return null;
}
public static String updateBuffer( IClassFile cf, String origSrc ) throws JavaModelException
{
updateSourceRanges( cf, origSrc );
return origSrc;
}
public static void updateSourceRanges( IClassFile cf, String contents ) throws JavaModelException
{
if ( cf instanceof ClassFile )
{
ClassFile classFile = (ClassFile) cf;
Object info = classFile.getElementInfo( );
IBinaryType typeInfo = info instanceof IBinaryType ? (IBinaryType) info : null;
SourceMapper mapper = classFile.getSourceMapper( );
IType type = (IType) ReflectionUtils.invokeMethod( classFile,
"getOuterMostEnclosingType", //$NON-NLS-1$
new Class[0],
new Object[0] );
HashMap sourceRange = (HashMap) ReflectionUtils.getFieldValue( mapper, "sourceRanges" ); //$NON-NLS-1$
sourceRange.remove( type );
DecompileUtil.mapSource( mapper, type, contents.toCharArray( ), typeInfo );
// List rootPaths = (List) ReflectionUtils.getFieldValue( mapper,
// "rootPaths" );
// String rootPath = null;
// if ( rootPaths != null && rootPaths.size( ) > 0 )
// {
// rootPath = (String) rootPaths.get( 0 );
// }
//
// if ( "".equals( rootPath ) )
// rootPath = null;
//
// ImportSourceMapper importMapper = new ImportSourceMapper(
// (IPath) ReflectionUtils.getFieldValue( mapper,
// "sourcePath" ),
// rootPath,
// (Map) ReflectionUtils.getFieldValue( mapper, "options" ) );
// importMapper.mapSource( type, contents.toCharArray( ), typeInfo
// );
}
}
public static String deleteOneEmptyLine( String origSrc )
{
int index = origSrc.indexOf( "{" ); //$NON-NLS-1$
if ( index == -1 )
{
return origSrc;
}
String prefix = origSrc.substring( 0, index + 1 );
String suffix = origSrc.substring( index + 1 );
List<String> splits = new ArrayList( Arrays.asList( prefix.split( "\n" ) ) ); //$NON-NLS-1$
boolean flag = false;
for ( int i = 0; i < splits.size( ); i++ )
{
String split = splits.get( i );
if ( split.trim( ).length( ) == 0 )
{
splits.remove( i );
flag = true;
break;
}
}
if ( flag )
{
StringBuffer buffer = new StringBuffer( );
for ( int i = 0; i < splits.size( ); i++ )
{
String split = splits.get( i );
buffer.append( split );
if ( i < splits.size( ) - 1 )
{
buffer.append( "\n" ); //$NON-NLS-1$
}
}
buffer.append( suffix );
return buffer.toString( );
}
return null;
}
public static void mapSource(SourceMapper sourceMapper, IType type, char[] source, IBinaryType info)
{
try {
ReflectionUtils.invokeMethod( sourceMapper, "mapSource", new Class[]{ //$NON-NLS-1$
IType.class, char[].class, IBinaryType.class
}, new Object[]{
type, source, info
} );
} catch (final NoSuchMethodError e) {
// API changed with Java 9 support (#daa227e4f5b7af888572a286c4f973b7a167ff2e)
ReflectionUtils.invokeMethod( sourceMapper, "mapSourceSwitch", new Class[]{ //$NON-NLS-1$
NamedMember.class, char[].class, IBinaryType.class
}, new Object[]{
type, source, info
} );
}
}
}