Skip to content

Commit

Permalink
Renaming thinger
Browse files Browse the repository at this point in the history
  • Loading branch information
cpw committed Nov 6, 2015
1 parent 2971389 commit f02eb60
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 177 deletions.
10 changes: 2 additions & 8 deletions src/org/jetbrains/java/decompiler/main/ClassWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ else if (CodeConstants.CLINIT_NAME.equals(name)) {
buffer.append(' ');
String parameterName = methodWrapper.varproc.getVarName(new VarVersionPair(index, 0));
if ((flags & (CodeConstants.ACC_ABSTRACT | CodeConstants.ACC_NATIVE)) != 0) {
parameterName = renameParameterIfPossible(parameterName, index, methodWrapper, flags);
parameterName = methodWrapper.methodStruct.renamer.renameAbstractParameter(parameterName);
}
buffer.append(parameterName == null ? "param" + index : parameterName); // null iff decompiled with errors

Expand Down Expand Up @@ -839,13 +839,7 @@ else if (CodeConstants.CLINIT_NAME.equals(name)) {
return !hideMethod;
}

private String renameParameterIfPossible(String parameterName, int index, MethodWrapper methodWrapper, int flags) {
if (DecompilerContext.getProperty("abstractparamrenamer")==null) return parameterName;
IAbstractParameterRenamer property = (IAbstractParameterRenamer) DecompilerContext.getProperty("abstractparamrenamer");
return property.renameParameter(parameterName, index, methodWrapper, flags);
}

private static void mapLines(TextBuffer code, StructLineNumberTableAttribute table, BytecodeMappingTracer tracer, int startLine) {
private static void mapLines(TextBuffer code, StructLineNumberTableAttribute table, BytecodeMappingTracer tracer, int startLine) {
// build line start offsets map
HashMap<Integer, Set<Integer>> lineStartOffsets = new HashMap<Integer, Set<Integer>>();
for (Map.Entry<Integer, Integer> entry : tracer.getMapping().entrySet()) {
Expand Down
19 changes: 19 additions & 0 deletions src/org/jetbrains/java/decompiler/main/DecompilerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.jetbrains.java.decompiler.main.collectors.VarNamesCollector;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.main.extern.IVariableNamingFactory;
import org.jetbrains.java.decompiler.modules.renamer.PoolInterceptor;
import org.jetbrains.java.decompiler.struct.StructContext;
import org.jetbrains.java.decompiler.util.JADNameProvider;

import java.util.HashMap;
import java.util.Locale;
Expand All @@ -36,6 +38,7 @@ public class DecompilerContext {
public static final String CURRENT_METHOD_DESCRIPTOR = "CURRENT_METHOD_DESCRIPTOR";
public static final String CURRENT_METHOD_WRAPPER = "CURRENT_METHOD_WRAPPER";
public static final String CURRENT_VAR_PROCESSOR = "CURRENT_VAR_PROCESSOR";
public static final String RENAMER_FACTORY = "RENAMER_FACTORY";

private static final ThreadLocal<DecompilerContext> currentContext = new ThreadLocal<DecompilerContext>();

Expand All @@ -48,6 +51,7 @@ public class DecompilerContext {
private PoolInterceptor poolInterceptor;
private IFernflowerLogger logger;
private BytecodeSourceMapper bytecodeSourceMapper;
private IVariableNamingFactory renamerFactory;

private DecompilerContext(Map<String, Object> properties) {
this.properties = properties;
Expand All @@ -59,6 +63,17 @@ public static void initContext(Map<String, Object> propertiesCustom) {
properties.putAll(propertiesCustom);
}
currentContext.set(new DecompilerContext(properties));
// Default a no-op renamer factory if none is provided
if (DecompilerContext.getProperty(RENAMER_FACTORY) != null) {
try {
currentContext.get().renamerFactory = Class.forName((String) DecompilerContext.getProperty(RENAMER_FACTORY)).asSubclass(IVariableNamingFactory.class).newInstance();
} catch (Exception e) {

}
}
if (DecompilerContext.getNamingFactory() == null) {
currentContext.get().renamerFactory = new JADNameProvider.JADNameProviderFactory();
}
}

public static DecompilerContext getCurrentContext() {
Expand Down Expand Up @@ -141,6 +156,10 @@ public static IFernflowerLogger getLogger() {
return getCurrentContext().logger;
}

public static IVariableNamingFactory getNamingFactory() {
return getCurrentContext().renamerFactory;
}

public static void setLogger(IFernflowerLogger logger) {
if (logger != null) {
String level = (String)getProperty(IFernflowerPreferences.LOG_LEVEL);
Expand Down
1 change: 1 addition & 0 deletions src/org/jetbrains/java/decompiler/main/Fernflower.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jetbrains.java.decompiler.struct.StructClass;
import org.jetbrains.java.decompiler.struct.StructContext;
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;
import org.jetbrains.java.decompiler.util.JADNameProvider;

import java.io.File;
import java.util.HashSet;
Expand Down
28 changes: 28 additions & 0 deletions src/org/jetbrains/java/decompiler/main/IdentityRenamerFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jetbrains.java.decompiler.main;

import java.util.Map;

import org.jetbrains.java.decompiler.main.extern.IVariableNameProvider;
import org.jetbrains.java.decompiler.main.extern.IVariableNamingFactory;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair;
import org.jetbrains.java.decompiler.struct.StructMethod;

public class IdentityRenamerFactory implements IVariableNamingFactory, IVariableNameProvider {
@Override
public IVariableNameProvider createFactory(StructMethod method) {
return this;
}

@Override
public String renameAbstractParameter(String abstractParam) {
return abstractParam;
}

@Override
public Map<VarVersionPair, String> rename(Map<VarVersionPair, String> variables) {
return null;
}
@Override
public void addParentContext(IVariableNameProvider renamer) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.java.decompiler.main.extern;

import java.util.Map;

import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair;

public interface IVariableNameProvider {
public Map<VarVersionPair,String> rename(Map<VarVersionPair,String> variables);
public String renameAbstractParameter(String abstractParam);
public void addParentContext(IVariableNameProvider renamer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jetbrains.java.decompiler.main.extern;

import org.jetbrains.java.decompiler.struct.StructMethod;

public interface IVariableNamingFactory {
public IVariableNameProvider createFactory(StructMethod structMethod);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.jetbrains.java.decompiler.main.rels;

import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.collectors.CounterContainer;
import org.jetbrains.java.decompiler.main.extern.IVariableNameProvider;
import org.jetbrains.java.decompiler.modules.decompiler.sforms.DirectGraph;
import org.jetbrains.java.decompiler.modules.decompiler.sforms.FlattenStatementsHelper;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ private static void insertLocalVars(final ClassNode parent, final ClassNode chil
for (final MethodWrapper meth : child.getWrapper().getMethods()) {

if (meth.root != null) { // neither abstract nor native

if (encmeth != null) { meth.methodStruct.renamer.addParentContext(encmeth.methodStruct.renamer); }
// local var names
HashMap<VarVersionPair, String> mapNewNames = new HashMap<VarVersionPair, String>();
// local var types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.collectors.VarNamesCollector;
import org.jetbrains.java.decompiler.main.rels.MethodWrapper;
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.exps.AssignmentExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent;
Expand Down Expand Up @@ -424,35 +425,47 @@ private static boolean setDefinition(Exprent expr, Integer index) {

private void propogateLVTs(Statement stat) {
MethodDescriptor md = MethodDescriptor.parseDescriptor(mt.getDescriptor());
Map<VarVersionPair, VarInfo> types = new HashMap<VarVersionPair, VarInfo>();
Map<VarVersionPair, VarInfo> types = new LinkedHashMap<VarVersionPair, VarInfo>();

int index = 0;
if (!mt.hasModifier(CodeConstants.ACC_STATIC)) {
types.put(new VarVersionPair(index, 0), new VarInfo(varproc.getLVT().getCandidates(index++).get(0), new VarType(mt.getClassStruct().qualifiedName)));
types.put(new VarVersionPair(index++, 0), new VarInfo(null,null));
}

for (VarType var : md.params) {
List<LVTVariable> vars = varproc.getLVT().getCandidates(index);
if (vars != null) {
types.put(new VarVersionPair(index, 0), new VarInfo(vars.get(0), var));
types.put(new VarVersionPair(index, 0), new VarInfo(null,null));
}
index += var.stackSize;
}

findTypes(stat, types);

//renameTypes(types);

Map<VarVersionPair,String> typeNames = new LinkedHashMap<VarVersionPair,String>();
for (Entry<VarVersionPair, VarInfo> e : types.entrySet()) {
if (e.getValue().lvt != null) {
varproc.setVarLVT(e.getKey(), e.getValue().lvt);
}
typeNames.put(e.getKey(), e.getValue().typeName());
}

StructMethod current_meth = (StructMethod)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD);
Map<VarVersionPair, String> renames = current_meth.renamer.rename(typeNames);
Map<VarVersionPair, LVTVariable> lvts = new HashMap<VarVersionPair, LVTVariable>();

for (Entry<VarVersionPair, VarInfo> e : types.entrySet()) {
if (e.getValue().lvt != null) {
lvts.put(e.getKey(), e.getValue().lvt);
VarVersionPair idx = e.getKey();
// skip this. we can't rename it
if (idx.var == 0 && !mt.hasModifier(CodeConstants.ACC_STATIC)) {
continue;
}
LVTVariable lvt = e.getValue().lvt;
if (renames!=null) {
varproc.setVarName(idx, renames.get(idx));
}
if (lvt != null) {
if (renames!=null) {
lvt = lvt.rename(renames.get(idx));
}
varproc.setVarLVT(idx, lvt);
lvts.put(idx, lvt);
}
}

Expand Down Expand Up @@ -508,14 +521,21 @@ private static class VarInfo {
String cast;
private VarInfo(LVTVariable lvt, VarType type) {
if (lvt != null && lvt.getSig() != null) {
cast = ExprProcessor.getCastTypeName(GenericType.parse(lvt.getSig()));
cast = ExprProcessor.getCastTypeName(GenericType.parse(lvt.getSig()),false);
}
else if (lvt != null) {
cast = ExprProcessor.getCastTypeName(lvt.getVarType());
cast = ExprProcessor.getCastTypeName(lvt.getVarType(),false);
}
else if (type != null) {
cast = ExprProcessor.getCastTypeName(type,false);
}
else {
cast = ExprProcessor.getCastTypeName(type);
cast = "this";
}
this.lvt = lvt;
}
public String typeName() {
return cast;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/org/jetbrains/java/decompiler/struct/StructMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jetbrains.java.decompiler.code.*;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.main.extern.IVariableNameProvider;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructGenericSignatureAttribute;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class StructMethod extends StructMember {
private InstructionSequence seq;
private boolean expanded = false;
private VBStyleCollection<StructGeneralAttribute, String> codeAttributes;
public final IVariableNameProvider renamer;

public StructMethod(DataInputFullStream in, StructClass clStruct) throws IOException {
classStruct = clStruct;
Expand All @@ -78,6 +80,7 @@ public StructMethod(DataInputFullStream in, StructClass clStruct) throws IOExcep
attributes.addAllWithKey(codeAttributes);
codeAttributes = null;
}
this.renamer = DecompilerContext.getNamingFactory().createFactory(this);
}

@Override
Expand Down
Loading

0 comments on commit f02eb60

Please sign in to comment.