Skip to content

Commit

Permalink
fix: improve ClassNode reloading and revert some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jan 3, 2020
1 parent 1eca2b6 commit 0c4b807
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 75 deletions.
2 changes: 0 additions & 2 deletions jadx-core/src/main/java/jadx/api/ICodeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ public interface ICodeCache {

@Nullable
ICodeInfo get(String clsFullName);

void remove(String clsFullName);
}
5 changes: 3 additions & 2 deletions jadx-core/src/main/java/jadx/api/JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public void decompile() {
cls.decompile();
}

public void refresh() {
cls.refresh();
public synchronized void refresh() {
listsLoaded = false;
cls.reloadCode();
}

public synchronized String getSmali() {
Expand Down
5 changes: 0 additions & 5 deletions jadx-core/src/main/java/jadx/api/impl/InMemoryCodeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,4 @@ public void add(String clsFullName, ICodeInfo codeInfo) {
public @Nullable ICodeInfo get(String clsFullName) {
return storage.get(clsFullName);
}

@Override
public void remove(String clsFullName) {
storage.remove(clsFullName);
}
}
4 changes: 0 additions & 4 deletions jadx-core/src/main/java/jadx/api/impl/NoOpCodeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ public void add(String clsFullName, ICodeInfo codeInfo) {
public @Nullable ICodeInfo get(String clsFullName) {
return null;
}

@Override
public void remove(String clsFullName) {
}
}
6 changes: 0 additions & 6 deletions jadx-core/src/main/java/jadx/core/dex/attributes/AType.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,5 @@ public class AType<T extends IAttribute> {
public static final Set<AType<?>> SKIP_ON_UNLOAD = new HashSet<>(Arrays.asList(
FIELD_REPLACE,
METHOD_INLINE,
COMMENTS,
RENAME_REASON,
JADX_WARN,
JADX_ERROR,
FIELD_INIT,
SOURCE_FILE,
SKIP_MTH_ARGS));
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public RenameReasonAttr notPrintable() {
public RenameReasonAttr append(String reason) {
if (description.isEmpty()) {
description += reason;
} else if (!description.contains(reason)) {
} else {
description += " and " + reason;
}
return this;
Expand Down
6 changes: 2 additions & 4 deletions jadx-core/src/main/java/jadx/core/dex/info/ConstStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ConstStorage(JadxArgs args) {
this.replaceEnabled = args.isReplaceConsts();
}

public void processConstFields(ClassNode cls, List<FieldNode> staticFields, boolean isRefresh) {
public void processConstFields(ClassNode cls, List<FieldNode> staticFields) {
if (!replaceEnabled || staticFields.isEmpty()) {
return;
}
Expand All @@ -76,9 +76,7 @@ public void processConstFields(ClassNode cls, List<FieldNode> staticFields, bool
&& fv.getValue() != null
&& fv.getValueType() == FieldInitAttr.InitType.CONST
&& fv != FieldInitAttr.NULL_VALUE) {
if (!isRefresh) {
addConstField(cls, f, fv.getValue(), accFlags.isPublic());
}
addConstField(cls, f, fv.getValue(), accFlags.isPublic());
}
}
}
Expand Down
51 changes: 15 additions & 36 deletions jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class ClassNode extends LineAttrNode implements ILoadable, ICodeNode {
private List<ArgType> interfaces;
private List<GenericInfo> generics = Collections.emptyList();

private final List<MethodNode> methods;
private final List<FieldNode> fields;
private List<MethodNode> methods;
private List<FieldNode> fields;
private List<ClassNode> innerClasses = Collections.emptyList();

// store smali
Expand All @@ -74,6 +74,10 @@ public ClassNode(DexNode dex, ClassDef cls) {
this.cls = cls;
this.clsDefOffset = cls.getOffset();
this.clsInfo = ClassInfo.fromDex(dex, cls.getTypeIndex());
initialLoad();
}

private void initialLoad() {
try {
if (cls.getSupertypeIndex() == DexNode.NO_INDEX) {
this.superClass = null;
Expand Down Expand Up @@ -102,7 +106,7 @@ public ClassNode(DexNode dex, ClassDef cls) {
for (Field f : clsData.getStaticFields()) {
fields.add(new FieldNode(this, f));
}
loadStaticValues(cls, fields, false);
loadStaticValues(cls, fields);
for (Field f : clsData.getInstanceFields()) {
fields.add(new FieldNode(this, f));
}
Expand Down Expand Up @@ -169,7 +173,7 @@ private void loadAnnotations(ClassDef cls) {
}
}

private void loadStaticValues(ClassDef cls, List<FieldNode> staticFields, boolean isRefresh) throws DecodeException {
private void loadStaticValues(ClassDef cls, List<FieldNode> staticFields) throws DecodeException {
for (FieldNode f : staticFields) {
AccessInfo flags = f.getAccessFlags();
if (flags.isStatic() && flags.isFinal()) {
Expand All @@ -186,7 +190,7 @@ private void loadStaticValues(ClassDef cls, List<FieldNode> staticFields, boolea
parser.processFields(staticFields);

// process const fields
root().getConstValues().processConstFields(this, staticFields, isRefresh);
root().getConstValues().processConstFields(this, staticFields);
}

private void parseClassSignature() {
Expand Down Expand Up @@ -275,7 +279,11 @@ public ICodeInfo getCode() {
return decompile(true);
}

public ICodeInfo reloadCode() {
public synchronized ICodeInfo reloadCode() {
unload();
clearAttributes();
initialLoad();
load();
return decompile(false);
}

Expand All @@ -294,36 +302,6 @@ private synchronized ICodeInfo decompile(boolean searchInCache) {
return codeInfo;
}

public synchronized ICodeInfo refresh() {
reloadRecursive();
return decompile(false);
}

private void reloadRecursive() {
load();
int sfIdx = cls.getSourceFileIndex();
if (sfIdx != DexNode.NO_INDEX) {
String fileName = dex.getString(sfIdx);
addSourceFilenameAttr(fileName);
}
for (ClassNode innerCls : getInnerClasses()) {
innerCls.reloadRecursive();
}
loadStaticInfo();
loadAnnotations(cls);
}

private void loadStaticInfo() {
try {
if (cls != null) {
loadStaticValues(cls, fields, true);
}
} catch (DecodeException e) {
LOG.error("Got DecodeException in loadStaticValues() for class {}", getRawName());
e.printStackTrace();
}
}

@Override
public void load() {
for (MethodNode mth : getMethods()) {
Expand All @@ -346,6 +324,7 @@ public void unload() {
fields.forEach(FieldNode::unloadAttributes);
unloadAttributes();
setState(NOT_LOADED);
this.smali = null;
}

private void buildCache() {
Expand Down
4 changes: 0 additions & 4 deletions jadx-core/src/main/java/jadx/core/dex/nodes/MethodNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ public void load() throws DecodeException {

public void checkInstructions() {
List<RegisterArg> list = new ArrayList<>();
if (instructions == null) {
LOG.debug("instructions == null, reloading method {}.{}", getClass().getName(), getName());
reload();
}
for (InsnNode insnNode : instructions) {
if (insnNode == null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static boolean removeFieldUsageFromConstructor(MethodNode mth, FieldNode
return false;
}
List<RegisterArg> args = mth.getArgRegs();
if (args.isEmpty()) {
if (args.isEmpty() || mth.contains(AFlag.SKIP_FIRST_ARG)) {
return false;
}
RegisterArg arg = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.jetbrains.annotations.Nullable;

import com.android.dx.rop.code.AccessFlags;

import jadx.core.codegen.TypeGen;
import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag;
Expand Down Expand Up @@ -45,7 +47,8 @@ public boolean visit(ClassNode cls) throws JadxException {
if (!convertToEnum(cls)) {
AccessInfo accessFlags = cls.getAccessFlags();
if (accessFlags.isEnum()) {
cls.addAttr(AType.COMMENTS, "'enum' modifier should be removed");
cls.setAccessFlags(accessFlags.remove(AccessFlags.ACC_ENUM));
cls.addAttr(AType.COMMENTS, "'enum' modifier removed");
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ public void visit(MethodNode mth) throws JadxException {

private void processDebugInfo(MethodNode mth, int debugOffset) {
InsnNode[] insnArr = mth.getInstructions();
if (insnArr == null) {
LOG.debug("insnArr == null, reloading method {}.{}", getClass().getName(), mth.getName());
mth.reload();
insnArr = mth.getInstructions();
}
if (insnArr == null) {
LOG.error("insnArr == null even after reloading method {}.{} - bailing", getClass().getName(), mth.getName());
return;
}
DebugInfoParser debugInfoParser = new DebugInfoParser(mth, debugOffset, insnArr);
List<LocalVar> localVars = debugInfoParser.process();
attachDebugInfo(mth, localVars, insnArr);
Expand Down

0 comments on commit 0c4b807

Please sign in to comment.