Skip to content

Commit

Permalink
[DEX] Clean redundant methods and fix number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 28, 2024
1 parent b4a7b86 commit 3aff898
Show file tree
Hide file tree
Showing 31 changed files with 782 additions and 404 deletions.
25 changes: 15 additions & 10 deletions src/main/java/com/reandroid/dex/common/DexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.reandroid.dex.common;

import com.reandroid.dex.key.TypeKey;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.NumbersUtil;
import com.reandroid.utils.StringsUtil;
import com.reandroid.utils.collection.ArrayCollection;
Expand Down Expand Up @@ -277,6 +278,10 @@ private static void escapeChar(Appendable appendable, char c) throws IOException
appendable.append(Character.forDigit((c >> 4) & 0x0f, 16));
appendable.append(Character.forDigit(c & 0x0f, 16));
}
/**
* Use StringKey.decodeEscapedString
* */
@Deprecated
public static String decodeString(String text) {
if(text.indexOf('\\') < 0){
return text;
Expand Down Expand Up @@ -324,19 +329,19 @@ private static char getEscaped(char ch){
private static Character nextHex(String text, int start){
int length = text.length();
int end = start + 4;
if(end > length){
if (end > length) {
return null;
}
StringBuilder builder = new StringBuilder(4);
for(int i = start; i < end; i++){
builder.append(text.charAt(i));
}
try{
int i = Integer.parseInt(builder.toString(), 16);
return (char) i;
}catch (NumberFormatException ignored){
return null;
int value = 0;
for (int i = start; i < end; i++) {
int v = HexUtil.decodeHexChar(text.charAt(i));
if (v == -1) {
return null;
}
value = value << 4;
value = value | v;
}
return (char) value;
}
public static boolean isJavaFramework(String name){
return name.startsWith("Ljava/");
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/reandroid/dex/data/AnnotationElement.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.reandroid.dex.data;

import com.reandroid.arsc.base.Creator;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/data/FieldDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void appendStaticValue(SmaliWriter writer) throws IOException {
private boolean isNonDefaultValue(Key key) {
if (key instanceof PrimitiveKey) {
PrimitiveKey primitiveKey = (PrimitiveKey) key;
return primitiveKey.asLongValue() != 0;
return primitiveKey.getValueAsLong() != 0;
}
return !(key instanceof NullKey);
}
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/com/reandroid/dex/id/FieldId.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.CombiningIterator;
import com.reandroid.utils.collection.SingleIterator;

import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;

public class FieldId extends IdItem implements Comparable<FieldId>{
private final IdItemIndirectReference<TypeId> defining;
Expand All @@ -53,7 +53,13 @@ public Iterator<IdItem> usedIds(){
public String getName() {
return nameReference.getString();
}
public void setName(String name){
public StringKey getNameKey() {
return nameReference.getKey();
}
public void setName(StringKey name) {
this.nameReference.setKey(name);
}
public void setName(String name) {
this.nameReference.setString(name);
}
public StringId getNameId(){
Expand Down Expand Up @@ -113,8 +119,9 @@ public SectionType<FieldId> getSectionType(){
return SectionType.FIELD_ID;
}
@Override
public FieldKey getKey(){
return checkKey(FieldKey.create(this));
public FieldKey getKey() {
return checkKey(FieldKey.create(getDefining(),
getNameKey(), getFieldType()));
}

@Override
Expand All @@ -123,11 +130,11 @@ public void setKey(Key key){
}
public void setKey(FieldKey key){
FieldKey old = getKey();
if(Objects.equals(key, old)){
if (ObjectsUtil.equals(key, old)) {
return;
}
defining.setKey(key.getDeclaring());
nameReference.setString(key.getName());
nameReference.setKey(key.getNameKey());
fieldType.setKey(key.getType());
keyChanged(old);
}
Expand Down Expand Up @@ -161,9 +168,9 @@ public int compareTo(FieldId fieldId) {
return CompareUtil.compare(getFieldTypeId(), fieldId.getFieldTypeId());
}
@Override
public String toString(){
public String toString() {
FieldKey key = getKey();
if(key != null){
if(key != null) {
return key.toString();
}
return getDefiningId() + "->" + getNameId() + ":" + getFieldTypeId();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/reandroid/dex/ins/InsArrayDataList.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public ArrayDataEntry(int width) {
}

public void fromSmali(SmaliValueX smaliValueX) {
set(smaliValueX.asLongValue());
set(smaliValueX.getValueAsLong());
}
public SmaliValueX toSmali() {
return new SmaliValueX(width(), getLong());
Expand Down Expand Up @@ -322,7 +322,7 @@ public String toString() {
builder.append('t');
} else if (width == 2) {
builder.append('S');
} else if (width == 8 && hex.length() > 10) {
} else if (width == 8 && (getLong() & 0xffffffff80000000L) != 0) {
builder.append('L');
}
return builder.toString();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/reandroid/dex/ins/InsConstWide32.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public InsConstWide32(){

@Override
public void set(long value) {
setLong(value);
setData(value);
}
@Override
public long getLong() {
Expand All @@ -36,7 +36,7 @@ public long getDataAsLong() {
}
@Override
public void setData(long data) {
set(data);
setInteger((int) data);
}

@Override
Expand Down
Loading

0 comments on commit 3aff898

Please sign in to comment.