Fork of the Fernflower decompiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fernflower/src/org/jetbrains/java/decompiler/modules/decompiler/vars/VarProcessor.java

124 lines
4.2 KiB

10 years ago
/*
* Copyright 2000-2016 JetBrains s.r.o.
10 years ago
*
* 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
10 years ago
*
* http://www.apache.org/licenses/LICENSE-2.0
10 years ago
*
* 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.
10 years ago
*/
package org.jetbrains.java.decompiler.modules.decompiler.vars;
10 years ago
import org.jetbrains.java.decompiler.main.collectors.VarNamesCollector;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.util.TextUtil;
10 years ago
import java.util.*;
import java.util.Map.Entry;
10 years ago
public class VarProcessor {
private final StructMethod method;
private final MethodDescriptor methodDescriptor;
private Map<VarVersionPair, String> mapVarNames = new HashMap<VarVersionPair, String>();
private VarVersionsProcessor varVersions;
private final Map<VarVersionPair, String> thisVars = new HashMap<VarVersionPair, String>();
private final Set<VarVersionPair> externalVars = new HashSet<VarVersionPair>();
public VarProcessor(StructMethod mt, MethodDescriptor md) {
method = mt;
methodDescriptor = md;
}
public void setVarVersions(RootStatement root) {
varVersions = new VarVersionsProcessor(method, methodDescriptor);
varVersions.setVarVersions(root);
}
public void setVarDefinitions(Statement root) {
mapVarNames = new HashMap<VarVersionPair, String>();
new VarDefinitionHelper(root, method, this).setVarDefinitions();
}
public void setDebugVarNames(Map<Integer, String> mapDebugVarNames) {
if (varVersions == null) {
return;
}
Map<Integer, Integer> mapOriginalVarIndices = varVersions.getMapOriginalVarIndices();
List<VarVersionPair> listVars = new ArrayList<VarVersionPair>(mapVarNames.keySet());
Collections.sort(listVars, (o1, o2) -> o1.var - o2.var);
Map<String, Integer> mapNames = new HashMap<String, Integer>();
for (VarVersionPair pair : listVars) {
String name = mapVarNames.get(pair);
Integer index = mapOriginalVarIndices.get(pair.var);
if (index != null) {
String debugName = mapDebugVarNames.get(index);
if (debugName != null && TextUtil.isValidIdentifier(debugName, method.getClassStruct().getBytecodeVersion())) {
name = debugName;
}
}
Integer counter = mapNames.get(name);
mapNames.put(name, counter == null ? counter = new Integer(0) : ++counter);
if (counter > 0) {
name += String.valueOf(counter);
}
mapVarNames.put(pair, name);
}
}
public void refreshVarNames(VarNamesCollector vc) {
Map<VarVersionPair, String> tempVarNames = new HashMap<VarVersionPair, String>(mapVarNames);
for (Entry<VarVersionPair, String> ent : tempVarNames.entrySet()) {
mapVarNames.put(ent.getKey(), vc.getFreeName(ent.getValue()));
}
}
public VarType getVarType(VarVersionPair pair) {
return varVersions == null ? null : varVersions.getVarType(pair);
}
public void setVarType(VarVersionPair pair, VarType type) {
varVersions.setVarType(pair, type);
}
public String getVarName(VarVersionPair pair) {
return mapVarNames == null ? null : mapVarNames.get(pair);
}
public void setVarName(VarVersionPair pair, String name) {
mapVarNames.put(pair, name);
}
public int getVarFinal(VarVersionPair pair) {
return varVersions == null ? VarTypeProcessor.VAR_FINAL : varVersions.getVarFinal(pair);
}
public void setVarFinal(VarVersionPair pair, int finalType) {
varVersions.setVarFinal(pair, finalType);
}
public Map<VarVersionPair, String> getThisVars() {
return thisVars;
}
10 years ago
public Set<VarVersionPair> getExternalVars() {
return externalVars;
}
}