2014-03-04 09:13:11 -05:00
|
|
|
/*
|
2014-08-28 13:34:14 -04:00
|
|
|
* Copyright 2000-2014 JetBrains s.r.o.
|
2014-03-04 09:13:11 -05:00
|
|
|
*
|
2014-08-28 13:34:14 -04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2014-03-04 09:13:11 -05:00
|
|
|
*
|
2014-08-28 13:34:14 -04:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-03-04 09:13:11 -05:00
|
|
|
*
|
2014-08-28 13:34:14 -04:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-03-04 09:13:11 -05:00
|
|
|
*/
|
2014-08-28 12:49:56 -04:00
|
|
|
package org.jetbrains.java.decompiler.main;
|
2014-03-04 09:13:11 -05:00
|
|
|
|
2014-08-28 12:49:56 -04:00
|
|
|
import org.jetbrains.java.decompiler.code.CodeConstants;
|
|
|
|
import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode;
|
|
|
|
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
|
|
|
|
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
|
|
|
|
import org.jetbrains.java.decompiler.main.rels.ClassWrapper;
|
|
|
|
import org.jetbrains.java.decompiler.main.rels.MethodWrapper;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.AnnotationExprent;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.NewExprent;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarTypeProcessor;
|
|
|
|
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPaar;
|
|
|
|
import org.jetbrains.java.decompiler.modules.renamer.PoolInterceptor;
|
|
|
|
import org.jetbrains.java.decompiler.struct.StructClass;
|
|
|
|
import org.jetbrains.java.decompiler.struct.StructField;
|
2014-09-04 06:30:28 -04:00
|
|
|
import org.jetbrains.java.decompiler.struct.StructMember;
|
2014-08-28 12:49:56 -04:00
|
|
|
import org.jetbrains.java.decompiler.struct.StructMethod;
|
2014-08-28 13:34:14 -04:00
|
|
|
import org.jetbrains.java.decompiler.struct.attr.*;
|
2014-08-28 12:49:56 -04:00
|
|
|
import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant;
|
|
|
|
import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor;
|
|
|
|
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
|
|
|
|
import org.jetbrains.java.decompiler.struct.gen.VarType;
|
2014-08-28 13:34:14 -04:00
|
|
|
import org.jetbrains.java.decompiler.struct.gen.generics.*;
|
2014-08-28 12:49:56 -04:00
|
|
|
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
2014-09-04 06:30:28 -04:00
|
|
|
|
|
|
|
import java.util.LinkedHashMap;
|
2014-08-28 13:34:14 -04:00
|
|
|
import java.util.List;
|
2014-09-04 06:30:28 -04:00
|
|
|
import java.util.Map;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-03-04 09:13:11 -05:00
|
|
|
public class ClassWriter {
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
private ClassReference14Processor ref14processor;
|
|
|
|
private PoolInterceptor interceptor;
|
|
|
|
|
|
|
|
public ClassWriter() {
|
|
|
|
ref14processor = new ClassReference14Processor();
|
|
|
|
interceptor = DecompilerContext.getPoolInterceptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void invokeProcessors(ClassNode node) {
|
|
|
|
ClassWrapper wrapper = node.wrapper;
|
|
|
|
StructClass cl = wrapper.getClassStruct();
|
|
|
|
|
|
|
|
InitializerProcessor.extractInitializers(wrapper);
|
|
|
|
|
|
|
|
if (node.type == ClassNode.CLASS_ROOT && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_CLASS_1_4)) {
|
|
|
|
ref14processor.processClassReferences(node);
|
|
|
|
}
|
|
|
|
|
2014-09-02 12:56:03 -04:00
|
|
|
if (cl.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM)) {
|
2014-08-28 13:34:14 -04:00
|
|
|
EnumProcessor.clearEnum(wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ASSERTIONS)) {
|
|
|
|
AssertProcessor.buildAssertions(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
public void classLambdaToJava(ClassNode node, StringBuilder buffer, Exprent method_object, int indent) {
|
2014-08-28 13:34:14 -04:00
|
|
|
// get the class node with the content method
|
2014-09-04 06:30:28 -04:00
|
|
|
ClassNode classNode = node;
|
|
|
|
while (classNode != null && classNode.type == ClassNode.CLASS_LAMBDA) {
|
|
|
|
classNode = classNode.parent;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
if (classNode == null) {
|
2014-08-28 13:34:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean lambdaToAnonymous = DecompilerContext.getOption(IFernflowerPreferences.LAMBDA_TO_ANONYMOUS_CLASS);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
ClassNode outerNode = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
|
2014-09-01 08:08:53 -04:00
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, node);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
try {
|
|
|
|
ClassWrapper wrapper = classNode.wrapper;
|
|
|
|
StructClass cl = wrapper.getClassStruct();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
DecompilerContext.getLogger().startWriteClass(node.simpleName);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (node.lambda_information.is_method_reference) {
|
|
|
|
if (!node.lambda_information.is_content_method_static && method_object != null) {
|
|
|
|
// reference to a virtual method
|
|
|
|
buffer.append(method_object.toJava(indent));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// reference to a static method
|
|
|
|
buffer.append(ExprProcessor.getCastTypeName(new VarType(node.lambda_information.content_class_name, false)));
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append("::");
|
|
|
|
buffer.append(node.lambda_information.content_method_name);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
else {
|
|
|
|
// lambda method
|
|
|
|
StructMethod mt = cl.getMethod(node.lambda_information.content_method_key);
|
|
|
|
MethodWrapper methodWrapper = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor());
|
|
|
|
MethodDescriptor md_content = MethodDescriptor.parseDescriptor(node.lambda_information.content_method_descriptor);
|
|
|
|
MethodDescriptor md_lambda = MethodDescriptor.parseDescriptor(node.lambda_information.method_descriptor);
|
|
|
|
|
|
|
|
if (!lambdaToAnonymous) {
|
|
|
|
buffer.append('(');
|
|
|
|
|
|
|
|
boolean firstParameter = true;
|
|
|
|
int index = node.lambda_information.is_content_method_static ? 0 : 1;
|
|
|
|
int start_index = md_content.params.length - md_lambda.params.length;
|
|
|
|
|
|
|
|
for (int i = 0; i < md_content.params.length; i++) {
|
|
|
|
if (i >= start_index) {
|
|
|
|
if (!firstParameter) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String parameterName = methodWrapper.varproc.getVarName(new VarVersionPaar(index, 0));
|
|
|
|
buffer.append(parameterName == null ? "param" + index : parameterName); // null iff decompiled with errors
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
firstParameter = false;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
index += md_content.params[i].stack_size;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(") ->");
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(" {");
|
|
|
|
buffer.append(DecompilerContext.getNewLineSeparator());
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
methodLambdaToJava(node, classNode, mt, buffer, indent + 1, !lambdaToAnonymous);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
InterpreterUtil.appendIndent(buffer, indent);
|
|
|
|
buffer.append("}");
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
finally {
|
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, outerNode);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
DecompilerContext.getLogger().endWriteClass();
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
public void classToJava(ClassNode node, StringBuilder buffer, int indent) {
|
|
|
|
ClassNode outerNode = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
|
2014-09-01 08:08:53 -04:00
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, node);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
try {
|
|
|
|
// last minute processing
|
|
|
|
invokeProcessors(node);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
ClassWrapper wrapper = node.wrapper;
|
|
|
|
StructClass cl = wrapper.getClassStruct();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
DecompilerContext.getLogger().startWriteClass(cl.qualifiedName);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
writeClassDefinition(node, buffer, indent);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean hasContent = false;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
// fields
|
|
|
|
boolean enumFields = false;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
for (StructField fd : cl.getFields()) {
|
|
|
|
boolean hide = fd.isSynthetic() && DecompilerContext.getOption(IFernflowerPreferences.REMOVE_SYNTHETIC) ||
|
|
|
|
wrapper.getHiddenMembers().contains(InterpreterUtil.makeUniqueKey(fd.getName(), fd.getDescriptor()));
|
|
|
|
if (hide) continue;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-02 12:56:03 -04:00
|
|
|
boolean isEnum = fd.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM);
|
2014-08-28 13:34:14 -04:00
|
|
|
if (isEnum) {
|
2014-09-04 06:30:28 -04:00
|
|
|
if (enumFields) {
|
|
|
|
buffer.append(',');
|
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
enumFields = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
else if (enumFields) {
|
|
|
|
buffer.append(';');
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
enumFields = false;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
fieldToJava(wrapper, cl, fd, buffer, indent + 1);
|
|
|
|
|
|
|
|
hasContent = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (enumFields) {
|
|
|
|
buffer.append(';');
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
// methods
|
|
|
|
for (StructMethod mt : cl.getMethods()) {
|
|
|
|
boolean hide = mt.isSynthetic() && DecompilerContext.getOption(IFernflowerPreferences.REMOVE_SYNTHETIC) ||
|
|
|
|
mt.hasModifier(CodeConstants.ACC_BRIDGE) && DecompilerContext.getOption(IFernflowerPreferences.REMOVE_BRIDGE) ||
|
|
|
|
wrapper.getHiddenMembers().contains(InterpreterUtil.makeUniqueKey(mt.getName(), mt.getDescriptor()));
|
|
|
|
if (hide) continue;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
int position = buffer.length();
|
|
|
|
if (hasContent) {
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
}
|
|
|
|
boolean methodSkipped = !methodToJava(node, mt, buffer, indent + 1);
|
|
|
|
if (!methodSkipped) {
|
|
|
|
hasContent = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer.setLength(position);
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
// member classes
|
|
|
|
for (ClassNode inner : node.nested) {
|
|
|
|
if (inner.type == ClassNode.CLASS_MEMBER) {
|
|
|
|
StructClass innerCl = inner.classStruct;
|
|
|
|
boolean isSynthetic = (inner.access & CodeConstants.ACC_SYNTHETIC) != 0 || innerCl.isSynthetic();
|
|
|
|
boolean hide = isSynthetic && DecompilerContext.getOption(IFernflowerPreferences.REMOVE_SYNTHETIC) ||
|
|
|
|
wrapper.getHiddenMembers().contains(innerCl.qualifiedName);
|
|
|
|
if (hide) continue;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (hasContent) {
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
}
|
|
|
|
classToJava(inner, buffer, indent + 1);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
hasContent = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
InterpreterUtil.appendIndent(buffer, indent);
|
|
|
|
buffer.append('}');
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (node.type != ClassNode.CLASS_ANONYMOUS) {
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, outerNode);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
DecompilerContext.getLogger().endWriteClass();
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private void writeClassDefinition(ClassNode node, StringBuilder buffer, int indent) {
|
|
|
|
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
|
|
|
String indentString = InterpreterUtil.getIndentString(indent);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
if (node.type == ClassNode.CLASS_ANONYMOUS) {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(" {");
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
return;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
ClassWrapper wrapper = node.wrapper;
|
|
|
|
StructClass cl = wrapper.getClassStruct();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
int flags = node.type == ClassNode.CLASS_ROOT ? cl.getAccessFlags() : node.access;
|
|
|
|
boolean isDeprecated = cl.getAttributes().containsKey("Deprecated");
|
|
|
|
boolean isSynthetic = (flags & CodeConstants.ACC_SYNTHETIC) != 0 || cl.getAttributes().containsKey("Synthetic");
|
|
|
|
boolean isEnum = DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM) && (flags & CodeConstants.ACC_ENUM) != 0;
|
|
|
|
boolean isInterface = (flags & CodeConstants.ACC_INTERFACE) != 0;
|
|
|
|
boolean isAnnotation = (flags & CodeConstants.ACC_ANNOTATION) != 0;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isDeprecated) {
|
|
|
|
appendDeprecation(buffer, indentString, lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (interceptor != null) {
|
|
|
|
String oldName = interceptor.getOldName(cl.qualifiedName);
|
|
|
|
appendRenameComment(buffer, oldName, MType.CLASS, indent, lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isSynthetic) {
|
|
|
|
appendComment(buffer, "synthetic class", indentString, lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
appendAnnotations(buffer, cl, indent, lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(indentString);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isEnum) {
|
|
|
|
// remove abstract and final flags (JLS 8.9 Enums)
|
|
|
|
flags &= ~CodeConstants.ACC_ABSTRACT;
|
|
|
|
flags &= ~CodeConstants.ACC_FINAL;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
appendModifiers(buffer, flags, CLASS_ALLOWED, isInterface, CLASS_EXCLUDED);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isEnum) {
|
|
|
|
buffer.append("enum ");
|
|
|
|
}
|
|
|
|
else if (isInterface) {
|
|
|
|
if (isAnnotation) {
|
|
|
|
buffer.append('@');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append("interface ");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer.append("class ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
GenericClassDescriptor descriptor = null;
|
|
|
|
if (DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES)) {
|
|
|
|
StructGenericSignatureAttribute attr = (StructGenericSignatureAttribute)cl.getAttributes().getWithKey("Signature");
|
|
|
|
if (attr != null) {
|
|
|
|
descriptor = GenericMain.parseClassSignature(attr.getSignature());
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(node.simpleName);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (descriptor != null && !descriptor.fparameters.isEmpty()) {
|
|
|
|
appendTypeParameters(buffer, descriptor.fparameters, descriptor.fbounds);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(' ');
|
2014-05-26 12:03:22 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (!isEnum && !isInterface && cl.superClass != null) {
|
|
|
|
VarType supertype = new VarType(cl.superClass.getString(), true);
|
|
|
|
if (!VarType.VARTYPE_OBJECT.equals(supertype)) {
|
|
|
|
buffer.append("extends ");
|
|
|
|
if (descriptor != null) {
|
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(descriptor.superclass));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer.append(ExprProcessor.getCastTypeName(supertype));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(' ');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (!isAnnotation) {
|
|
|
|
int[] interfaces = cl.getInterfaces();
|
|
|
|
if (interfaces.length > 0) {
|
|
|
|
buffer.append(isInterface ? "extends " : "implements ");
|
|
|
|
for (int i = 0; i < interfaces.length; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
if (descriptor != null) {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(descriptor.superinterfaces.get(i)));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(ExprProcessor.getCastTypeName(new VarType(cl.getInterface(i), true)));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(' ');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append('{');
|
|
|
|
buffer.append(lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private void fieldToJava(ClassWrapper wrapper, StructClass cl, StructField fd, StringBuilder buffer, int indent) {
|
|
|
|
String indentString = InterpreterUtil.getIndentString(indent);
|
|
|
|
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-02 12:56:03 -04:00
|
|
|
boolean isInterface = cl.hasModifier(CodeConstants.ACC_INTERFACE);
|
2014-08-28 13:34:14 -04:00
|
|
|
boolean isDeprecated = fd.getAttributes().containsKey("Deprecated");
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean isEnum = fd.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
if (isDeprecated) {
|
2014-09-04 06:30:28 -04:00
|
|
|
appendDeprecation(buffer, indentString, lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (interceptor != null) {
|
|
|
|
String oldName = interceptor.getOldName(cl.qualifiedName + " " + fd.getName() + " " + fd.getDescriptor());
|
|
|
|
appendRenameComment(buffer, oldName, MType.FIELD, indent, lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (fd.isSynthetic()) {
|
|
|
|
appendComment(buffer, "synthetic field", indentString, lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
appendAnnotations(buffer, fd, indent, lineSeparator);
|
|
|
|
|
|
|
|
buffer.append(indentString);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
if (!isEnum) {
|
2014-09-04 06:30:28 -04:00
|
|
|
appendModifiers(buffer, fd.getAccessFlags(), FIELD_ALLOWED, isInterface, FIELD_EXCLUDED);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VarType fieldType = new VarType(fd.getDescriptor(), false);
|
|
|
|
|
|
|
|
GenericFieldDescriptor descriptor = null;
|
|
|
|
if (DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES)) {
|
|
|
|
StructGenericSignatureAttribute attr = (StructGenericSignatureAttribute)fd.getAttributes().getWithKey("Signature");
|
|
|
|
if (attr != null) {
|
|
|
|
descriptor = GenericMain.parseFieldSignature(attr.getSignature());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEnum) {
|
|
|
|
if (descriptor != null) {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(descriptor.type));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(ExprProcessor.getCastTypeName(fieldType));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(' ');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(fd.getName());
|
2014-08-28 13:34:14 -04:00
|
|
|
|
|
|
|
Exprent initializer;
|
2014-09-02 12:56:03 -04:00
|
|
|
if (fd.hasModifier(CodeConstants.ACC_STATIC)) {
|
2014-08-28 13:34:14 -04:00
|
|
|
initializer = wrapper.getStaticFieldInitializers().getWithKey(InterpreterUtil.makeUniqueKey(fd.getName(), fd.getDescriptor()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
initializer = wrapper.getDynamicFieldInitializers().getWithKey(InterpreterUtil.makeUniqueKey(fd.getName(), fd.getDescriptor()));
|
|
|
|
}
|
|
|
|
if (initializer != null) {
|
|
|
|
if (isEnum && initializer.type == Exprent.EXPRENT_NEW) {
|
|
|
|
NewExprent nexpr = (NewExprent)initializer;
|
|
|
|
nexpr.setEnumconst(true);
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(nexpr.toJava(indent));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(" = ");
|
|
|
|
buffer.append(initializer.toJava(indent));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-02 12:56:03 -04:00
|
|
|
else if (fd.hasModifier(CodeConstants.ACC_FINAL) && fd.hasModifier(CodeConstants.ACC_STATIC)) {
|
2014-08-28 13:34:14 -04:00
|
|
|
StructConstantValueAttribute attr =
|
|
|
|
(StructConstantValueAttribute)fd.getAttributes().getWithKey(StructGeneralAttribute.ATTRIBUTE_CONSTANT_VALUE);
|
|
|
|
if (attr != null) {
|
2014-09-04 06:30:28 -04:00
|
|
|
PrimitiveConstant constant = cl.getPool().getPrimitiveConstant(attr.getIndex());
|
|
|
|
buffer.append(" = ");
|
|
|
|
buffer.append(new ConstExprent(fieldType, constant.value).toJava(indent));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEnum) {
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(";");
|
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static void methodLambdaToJava(ClassNode lambdaNode,
|
|
|
|
ClassNode classNode,
|
|
|
|
StructMethod mt,
|
|
|
|
StringBuilder buffer,
|
|
|
|
int indent,
|
|
|
|
boolean codeOnly) {
|
|
|
|
ClassWrapper classWrapper = classNode.wrapper;
|
|
|
|
MethodWrapper methodWrapper = classWrapper.getMethodWrapper(mt.getName(), mt.getDescriptor());
|
|
|
|
|
|
|
|
MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
|
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, methodWrapper);
|
|
|
|
|
|
|
|
try {
|
|
|
|
String method_name = lambdaNode.lambda_information.method_name;
|
|
|
|
MethodDescriptor md_content = MethodDescriptor.parseDescriptor(lambdaNode.lambda_information.content_method_descriptor);
|
|
|
|
MethodDescriptor md_lambda = MethodDescriptor.parseDescriptor(lambdaNode.lambda_information.method_descriptor);
|
|
|
|
|
|
|
|
if (!codeOnly) {
|
|
|
|
InterpreterUtil.appendIndent(buffer, indent);
|
|
|
|
buffer.append("public ");
|
|
|
|
buffer.append(method_name);
|
|
|
|
buffer.append("(");
|
|
|
|
|
|
|
|
boolean firstParameter = true;
|
|
|
|
int index = lambdaNode.lambda_information.is_content_method_static ? 0 : 1;
|
|
|
|
int start_index = md_content.params.length - md_lambda.params.length;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
for (int i = 0; i < md_content.params.length; i++) {
|
|
|
|
if (i >= start_index) {
|
|
|
|
if (!firstParameter) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String typeName = ExprProcessor.getCastTypeName(md_content.params[i].copy());
|
|
|
|
if (ExprProcessor.UNDEFINED_TYPE_STRING.equals(typeName) &&
|
|
|
|
DecompilerContext.getOption(IFernflowerPreferences.UNDEFINED_PARAM_TYPE_OBJECT)) {
|
|
|
|
typeName = ExprProcessor.getCastTypeName(VarType.VARTYPE_OBJECT);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(typeName);
|
|
|
|
buffer.append(" ");
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String parameterName = methodWrapper.varproc.getVarName(new VarVersionPaar(index, 0));
|
|
|
|
buffer.append(parameterName == null ? "param" + index : parameterName); // null iff decompiled with errors
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
firstParameter = false;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
index += md_content.params[i].stack_size;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(") {");
|
|
|
|
buffer.append(DecompilerContext.getNewLineSeparator());
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
indent += 1;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (!methodWrapper.decompiledWithErrors) {
|
|
|
|
RootStatement root = classWrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root;
|
|
|
|
if (root != null) { // check for existence
|
|
|
|
try {
|
|
|
|
buffer.append(root.toJava(indent));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
catch (Throwable ex) {
|
|
|
|
DecompilerContext.getLogger().writeMessage("Method " + mt.getName() + " " + mt.getDescriptor() + " couldn't be written.", ex);
|
|
|
|
methodWrapper.decompiledWithErrors = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (methodWrapper.decompiledWithErrors) {
|
|
|
|
InterpreterUtil.appendIndent(buffer, indent);
|
|
|
|
buffer.append("// $FF: Couldn't be decompiled");
|
|
|
|
buffer.append(DecompilerContext.getNewLineSeparator());
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (!codeOnly) {
|
|
|
|
indent -= 1;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
InterpreterUtil.appendIndent(buffer, indent);
|
|
|
|
buffer.append('}');
|
|
|
|
buffer.append(DecompilerContext.getNewLineSeparator());
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
finally {
|
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, outerWrapper);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private boolean methodToJava(ClassNode node, StructMethod mt, StringBuilder buffer, int indent) {
|
2014-08-28 13:34:14 -04:00
|
|
|
ClassWrapper wrapper = node.wrapper;
|
|
|
|
StructClass cl = wrapper.getClassStruct();
|
2014-09-04 06:30:28 -04:00
|
|
|
MethodWrapper methodWrapper = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor());
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean hideMethod = false;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
|
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, methodWrapper);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
try {
|
|
|
|
boolean isInterface = cl.hasModifier(CodeConstants.ACC_INTERFACE);
|
|
|
|
boolean isAnnotation = cl.hasModifier(CodeConstants.ACC_ANNOTATION);
|
|
|
|
boolean isEnum = cl.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM);
|
|
|
|
boolean isDeprecated = mt.getAttributes().containsKey("Deprecated");
|
|
|
|
boolean clinit = false, init = false, dinit = false;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String indentString = InterpreterUtil.getIndentString(indent);
|
|
|
|
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
MethodDescriptor md = MethodDescriptor.parseDescriptor(mt.getDescriptor());
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
int flags = mt.getAccessFlags();
|
|
|
|
if ((flags & CodeConstants.ACC_NATIVE) != 0) {
|
|
|
|
flags &= ~CodeConstants.ACC_STRICT; // compiler bug: a strictfp class sets all methods to strictfp
|
|
|
|
}
|
|
|
|
if ("<clinit>".equals(mt.getName())) {
|
|
|
|
flags &= CodeConstants.ACC_STATIC; // ignore all modifiers except 'static' in a static initializer
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isDeprecated) {
|
|
|
|
appendDeprecation(buffer, indentString, lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (interceptor != null) {
|
|
|
|
String oldName = interceptor.getOldName(cl.qualifiedName + " " + mt.getName() + " " + mt.getDescriptor());
|
|
|
|
appendRenameComment(buffer, oldName, MType.METHOD, indent, lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean isSynthetic = (flags & CodeConstants.ACC_SYNTHETIC) != 0 || mt.getAttributes().containsKey("Synthetic");
|
|
|
|
boolean isBridge = (flags & CodeConstants.ACC_BRIDGE) != 0;
|
|
|
|
if (isSynthetic) {
|
|
|
|
appendComment(buffer, "synthetic method", indentString, lineSeparator);
|
|
|
|
}
|
|
|
|
if (isBridge) {
|
|
|
|
appendComment(buffer, "bridge method", indentString, lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
appendAnnotations(buffer, mt, indent, lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(indentString);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
appendModifiers(buffer, flags, METHOD_ALLOWED, isInterface, METHOD_EXCLUDED);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isInterface && mt.containsCode()) {
|
|
|
|
// 'default' modifier (Java 8)
|
|
|
|
buffer.append("default ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String name = mt.getName();
|
|
|
|
if ("<init>".equals(name)) {
|
|
|
|
if (node.type == ClassNode.CLASS_ANONYMOUS) {
|
|
|
|
name = "";
|
|
|
|
dinit = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
name = node.simpleName;
|
|
|
|
init = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
else if ("<clinit>".equals(name)) {
|
2014-08-28 13:34:14 -04:00
|
|
|
name = "";
|
2014-09-04 06:30:28 -04:00
|
|
|
clinit = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
GenericMethodDescriptor descriptor = null;
|
|
|
|
if (DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES)) {
|
|
|
|
StructGenericSignatureAttribute attr = (StructGenericSignatureAttribute)mt.getAttributes().getWithKey("Signature");
|
|
|
|
if (attr != null) {
|
|
|
|
descriptor = GenericMain.parseMethodSignature(attr.getSignature());
|
|
|
|
int actualParams = md.params.length;
|
|
|
|
if (isEnum && init) actualParams -= 2;
|
|
|
|
if (actualParams != descriptor.params.size()) {
|
|
|
|
String message = "Inconsistent generic signature in method " + mt.getName() + " " + mt.getDescriptor();
|
|
|
|
DecompilerContext.getLogger().writeMessage(message, IFernflowerLogger.WARNING);
|
|
|
|
descriptor = null;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean throwsExceptions = false;
|
|
|
|
int paramCount = 0;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (!clinit && !dinit) {
|
|
|
|
boolean thisVar = !mt.hasModifier(CodeConstants.ACC_STATIC);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (descriptor != null && !descriptor.fparameters.isEmpty()) {
|
|
|
|
appendTypeParameters(buffer, descriptor.fparameters, descriptor.fbounds);
|
|
|
|
buffer.append(' ');
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (!init) {
|
|
|
|
if (descriptor != null) {
|
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(descriptor.ret));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
else {
|
|
|
|
buffer.append(ExprProcessor.getCastTypeName(md.ret));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(' ');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(name);
|
|
|
|
buffer.append('(');
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
// parameters
|
|
|
|
List<VarVersionPaar> signFields = methodWrapper.signatureFields;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
int lastVisibleParameterIndex = -1;
|
|
|
|
for (int i = 0; i < md.params.length; i++) {
|
|
|
|
if (signFields == null || signFields.get(i) == null) {
|
|
|
|
lastVisibleParameterIndex = i;
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean firstParameter = true;
|
|
|
|
int index = isEnum && init ? 3 : thisVar ? 1 : 0;
|
|
|
|
int start = isEnum && init && descriptor == null ? 2 : 0;
|
|
|
|
int params = descriptor == null ? md.params.length : descriptor.params.size();
|
|
|
|
for (int i = start; i < params; i++) {
|
|
|
|
if (signFields == null || signFields.get(i) == null) {
|
|
|
|
if (!firstParameter) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
appendParameterAnnotations(buffer, mt, paramCount);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (methodWrapper.varproc.getVarFinal(new VarVersionPaar(index, 0)) == VarTypeProcessor.VAR_FINALEXPLICIT) {
|
|
|
|
buffer.append("final ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (descriptor != null) {
|
|
|
|
GenericType parameterType = descriptor.params.get(i);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean isVarArg = (i == lastVisibleParameterIndex && mt.hasModifier(CodeConstants.ACC_VARARGS) && parameterType.arraydim > 0);
|
|
|
|
if (isVarArg) {
|
|
|
|
parameterType.arraydim--;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
|
|
|
|
String typeName = GenericMain.getGenericCastTypeName(parameterType);
|
|
|
|
if (ExprProcessor.UNDEFINED_TYPE_STRING.equals(typeName) &&
|
|
|
|
DecompilerContext.getOption(IFernflowerPreferences.UNDEFINED_PARAM_TYPE_OBJECT)) {
|
|
|
|
typeName = ExprProcessor.getCastTypeName(VarType.VARTYPE_OBJECT);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(typeName);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isVarArg) {
|
|
|
|
buffer.append("...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VarType parameterType = md.params[i].copy();
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
boolean isVarArg = (i == lastVisibleParameterIndex && mt.hasModifier(CodeConstants.ACC_VARARGS) && parameterType.arraydim > 0);
|
|
|
|
if (isVarArg) {
|
|
|
|
parameterType.decArrayDim();
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
String typeName = ExprProcessor.getCastTypeName(parameterType);
|
|
|
|
if (ExprProcessor.UNDEFINED_TYPE_STRING.equals(typeName) &&
|
|
|
|
DecompilerContext.getOption(IFernflowerPreferences.UNDEFINED_PARAM_TYPE_OBJECT)) {
|
|
|
|
typeName = ExprProcessor.getCastTypeName(VarType.VARTYPE_OBJECT);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(typeName);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (isVarArg) {
|
|
|
|
buffer.append("...");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(' ');
|
|
|
|
String parameterName = methodWrapper.varproc.getVarName(new VarVersionPaar(index, 0));
|
|
|
|
buffer.append(parameterName == null ? "param" + index : parameterName); // null iff decompiled with errors
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
firstParameter = false;
|
|
|
|
paramCount++;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
index += md.params[i].stack_size;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(')');
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
StructExceptionsAttribute attr = (StructExceptionsAttribute)mt.getAttributes().getWithKey("Exceptions");
|
|
|
|
if ((descriptor != null && !descriptor.exceptions.isEmpty()) || attr != null) {
|
|
|
|
throwsExceptions = true;
|
|
|
|
buffer.append(" throws ");
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
for (int i = 0; i < attr.getThrowsExceptions().size(); i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
|
|
|
if (descriptor != null && !descriptor.exceptions.isEmpty()) {
|
|
|
|
GenericType type = descriptor.exceptions.get(i);
|
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(type));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VarType type = new VarType(attr.getExcClassname(i, cl.getPool()), true);
|
|
|
|
buffer.append(ExprProcessor.getCastTypeName(type));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if ((flags & (CodeConstants.ACC_ABSTRACT | CodeConstants.ACC_NATIVE)) != 0) { // native or abstract method (explicit or interface)
|
|
|
|
if (isAnnotation) {
|
|
|
|
StructAnnDefaultAttribute attr = (StructAnnDefaultAttribute)mt.getAttributes().getWithKey("AnnotationDefault");
|
|
|
|
if (attr != null) {
|
|
|
|
buffer.append(" default ");
|
|
|
|
buffer.append(attr.getDefaultValue().toJava(indent + 1));
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(';');
|
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
else {
|
|
|
|
if (!clinit && !dinit) {
|
|
|
|
buffer.append(' ');
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append('{');
|
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
RootStatement root = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (root != null && !methodWrapper.decompiledWithErrors) { // check for existence
|
|
|
|
try {
|
|
|
|
String code = root.toJava(indent + 1);
|
|
|
|
|
|
|
|
hideMethod = (clinit || dinit || hideConstructor(wrapper, init, throwsExceptions, paramCount)) && code.length() == 0;
|
|
|
|
|
|
|
|
buffer.append(code);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
catch (Throwable ex) {
|
|
|
|
DecompilerContext.getLogger().writeMessage("Method " + mt.getName() + " " + mt.getDescriptor() + " couldn't be written.", ex);
|
|
|
|
methodWrapper.decompiledWithErrors = true;
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
if (methodWrapper.decompiledWithErrors) {
|
|
|
|
buffer.append(InterpreterUtil.getIndentString(indent + 1));
|
|
|
|
buffer.append("// $FF: Couldn't be decompiled");
|
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(indentString);
|
|
|
|
buffer.append('}');
|
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, outerWrapper);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
return !hideMethod;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static boolean hideConstructor(ClassWrapper wrapper, boolean init, boolean throwsExceptions, int paramCount) {
|
|
|
|
if (!init || throwsExceptions || paramCount > 0 || !DecompilerContext.getOption(IFernflowerPreferences.HIDE_DEFAULT_CONSTRUCTOR)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
int count = 0;
|
|
|
|
for (StructMethod mt : wrapper.getClassStruct().getMethods()) {
|
|
|
|
if ("<init>".equals(mt.getName())) {
|
|
|
|
if (++count > 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static void appendDeprecation(StringBuilder buffer, String indentString, String lineSeparator) {
|
|
|
|
buffer.append(indentString).append("/** @deprecated */").append(lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private enum MType {CLASS, FIELD, METHOD}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static void appendRenameComment(StringBuilder buffer, String oldName, MType type, int indent, String lineSeparator) {
|
|
|
|
if (oldName == null) return;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
InterpreterUtil.appendIndent(buffer, indent);
|
|
|
|
buffer.append("// $FF: renamed from: ");
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
switch (type) {
|
|
|
|
case CLASS:
|
|
|
|
buffer.append(ExprProcessor.buildJavaClassName(oldName));
|
|
|
|
break;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
case FIELD:
|
|
|
|
String[] fParts = oldName.split(" ");
|
|
|
|
FieldDescriptor fd = FieldDescriptor.parseDescriptor(fParts[2]);
|
|
|
|
buffer.append(fParts[1]);
|
|
|
|
buffer.append(' ');
|
|
|
|
buffer.append(getTypePrintOut(fd.type));
|
|
|
|
break;
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
default:
|
|
|
|
String[] mParts = oldName.split(" ");
|
|
|
|
MethodDescriptor md = MethodDescriptor.parseDescriptor(mParts[2]);
|
|
|
|
buffer.append(mParts[1]);
|
|
|
|
buffer.append(" (");
|
|
|
|
boolean first = true;
|
|
|
|
for (VarType paramType : md.params) {
|
|
|
|
if (!first) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
buffer.append(getTypePrintOut(paramType));
|
|
|
|
}
|
|
|
|
buffer.append(") ");
|
|
|
|
buffer.append(getTypePrintOut(md.ret));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(lineSeparator);
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static String getTypePrintOut(VarType type) {
|
|
|
|
String typeText = ExprProcessor.getCastTypeName(type, false);
|
|
|
|
if (ExprProcessor.UNDEFINED_TYPE_STRING.equals(typeText) &&
|
|
|
|
DecompilerContext.getOption(IFernflowerPreferences.UNDEFINED_PARAM_TYPE_OBJECT)) {
|
|
|
|
typeText = ExprProcessor.getCastTypeName(VarType.VARTYPE_OBJECT, false);
|
|
|
|
}
|
|
|
|
return typeText;
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static void appendComment(StringBuilder buffer, String comment, String indentString, String lineSeparator) {
|
|
|
|
buffer.append(indentString).append("// $FF: ").append(comment).append(lineSeparator);
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static final String[] ANNOTATION_ATTRIBUTES = {
|
|
|
|
StructGeneralAttribute.ATTRIBUTE_RUNTIME_VISIBLE_ANNOTATIONS, StructGeneralAttribute.ATTRIBUTE_RUNTIME_INVISIBLE_ANNOTATIONS};
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static void appendAnnotations(StringBuilder buffer, StructMember mb, int indent, String lineSeparator) {
|
|
|
|
for (String name : ANNOTATION_ATTRIBUTES) {
|
|
|
|
StructAnnotationAttribute attribute = (StructAnnotationAttribute)mb.getAttributes().getWithKey(name);
|
|
|
|
if (attribute != null) {
|
|
|
|
for (AnnotationExprent annotation : attribute.getAnnotations()) {
|
|
|
|
buffer.append(annotation.toJava(indent)).append(lineSeparator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static final String[] PARAMETER_ANNOTATION_ATTRIBUTES = {
|
|
|
|
StructGeneralAttribute.ATTRIBUTE_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS, StructGeneralAttribute.ATTRIBUTE_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS};
|
|
|
|
|
|
|
|
private static void appendParameterAnnotations(StringBuilder buffer, StructMethod mt, int param) {
|
|
|
|
for (String name : PARAMETER_ANNOTATION_ATTRIBUTES) {
|
|
|
|
StructAnnotationParameterAttribute attribute = (StructAnnotationParameterAttribute)mt.getAttributes().getWithKey(name);
|
|
|
|
if (attribute != null) {
|
|
|
|
List<List<AnnotationExprent>> annotations = attribute.getParamAnnotations();
|
|
|
|
if (param < annotations.size()) {
|
|
|
|
for (AnnotationExprent annotation : annotations.get(param)) {
|
|
|
|
buffer.append(annotation.toJava(0)).append(' ');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static final Map<Integer, String> MODIFIERS = new LinkedHashMap<Integer, String>() {{
|
|
|
|
put(CodeConstants.ACC_PUBLIC, "public");
|
|
|
|
put(CodeConstants.ACC_PROTECTED, "protected");
|
|
|
|
put(CodeConstants.ACC_PRIVATE, "private");
|
|
|
|
put(CodeConstants.ACC_ABSTRACT, "abstract");
|
|
|
|
put(CodeConstants.ACC_STATIC, "static");
|
|
|
|
put(CodeConstants.ACC_FINAL, "final");
|
|
|
|
put(CodeConstants.ACC_STRICT, "strictfp");
|
|
|
|
put(CodeConstants.ACC_TRANSIENT, "transient");
|
|
|
|
put(CodeConstants.ACC_VOLATILE, "volatile");
|
|
|
|
put(CodeConstants.ACC_SYNCHRONIZED, "synchronized");
|
|
|
|
put(CodeConstants.ACC_NATIVE, "native");
|
|
|
|
}};
|
|
|
|
|
|
|
|
private static final int CLASS_ALLOWED =
|
|
|
|
CodeConstants.ACC_PUBLIC | CodeConstants.ACC_PROTECTED | CodeConstants.ACC_PRIVATE | CodeConstants.ACC_ABSTRACT |
|
|
|
|
CodeConstants.ACC_STATIC | CodeConstants.ACC_FINAL | CodeConstants.ACC_STRICT;
|
|
|
|
private static final int FIELD_ALLOWED =
|
|
|
|
CodeConstants.ACC_PUBLIC | CodeConstants.ACC_PROTECTED | CodeConstants.ACC_PRIVATE | CodeConstants.ACC_STATIC |
|
|
|
|
CodeConstants.ACC_FINAL | CodeConstants.ACC_TRANSIENT | CodeConstants.ACC_VOLATILE;
|
|
|
|
private static final int METHOD_ALLOWED =
|
|
|
|
CodeConstants.ACC_PUBLIC | CodeConstants.ACC_PROTECTED | CodeConstants.ACC_PRIVATE | CodeConstants.ACC_ABSTRACT |
|
|
|
|
CodeConstants.ACC_STATIC | CodeConstants.ACC_FINAL | CodeConstants.ACC_SYNCHRONIZED | CodeConstants.ACC_NATIVE | CodeConstants.ACC_STRICT;
|
|
|
|
|
|
|
|
private static final int CLASS_EXCLUDED = CodeConstants.ACC_ABSTRACT | CodeConstants.ACC_STATIC;
|
|
|
|
private static final int FIELD_EXCLUDED = CodeConstants.ACC_PUBLIC | CodeConstants.ACC_STATIC | CodeConstants.ACC_FINAL;
|
|
|
|
private static final int METHOD_EXCLUDED = CodeConstants.ACC_PUBLIC | CodeConstants.ACC_ABSTRACT;
|
|
|
|
|
|
|
|
private static void appendModifiers(StringBuilder buffer, int flags, int allowed, boolean isInterface, int excluded) {
|
|
|
|
flags &= allowed;
|
|
|
|
if (!isInterface) excluded = 0;
|
|
|
|
for (int modifier : MODIFIERS.keySet()) {
|
|
|
|
if ((flags & modifier) == modifier && (modifier & excluded) == 0) {
|
|
|
|
buffer.append(MODIFIERS.get(modifier)).append(' ');
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
private static void appendTypeParameters(StringBuilder buffer, List<String> parameters, List<List<GenericType>> bounds) {
|
|
|
|
buffer.append('<');
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
for (int i = 0; i < parameters.size(); i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
buffer.append(", ");
|
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append(parameters.get(i));
|
2014-08-28 13:34:14 -04:00
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
List<GenericType> parameterBounds = bounds.get(i);
|
|
|
|
if (parameterBounds.size() > 1 || !"java/lang/Object".equals(parameterBounds.get(0).value)) {
|
|
|
|
buffer.append(" extends ");
|
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(parameterBounds.get(0)));
|
|
|
|
for (int j = 1; j < parameterBounds.size(); j++) {
|
|
|
|
buffer.append(" & ");
|
|
|
|
buffer.append(GenericMain.getGenericCastTypeName(parameterBounds.get(j)));
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-09-04 06:30:28 -04:00
|
|
|
}
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 06:30:28 -04:00
|
|
|
buffer.append('>');
|
2014-08-28 13:34:14 -04:00
|
|
|
}
|
2014-03-04 09:13:11 -05:00
|
|
|
}
|