Skip to content

Commit

Permalink
[DEX] Limit the number of smali comment lines
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Dec 14, 2024
1 parent e808eb5 commit 5239dd0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 53 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/reandroid/dex/smali/SmaliWriterSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public class SmaliWriterSetting {
private boolean sequentialLabel;
private boolean commentUnicodeStrings;
private boolean localRegistersCount;
private int maximumCommentLines;

public SmaliWriterSetting() {
this.sequentialLabel = true;
this.commentUnicodeStrings = false;
this.localRegistersCount = true;
this.maximumCommentLines = 500;
}

public boolean isSequentialLabel() {
Expand All @@ -63,6 +65,19 @@ public void setLocalRegistersCount(boolean localRegistersCount) {
this.localRegistersCount = localRegistersCount;
}

public int getMaximumCommentLines() {
return maximumCommentLines;
}

/**
* Sets maximum allowed number of lines for comment.
* default = 500
* unlimited if maximumCommentLines less than zero
* */
public void setMaximumCommentLines(int maximumCommentLines) {
this.maximumCommentLines = maximumCommentLines;
}

public void writeResourceIdComment(SmaliWriter writer, long l) throws IOException {
ResourceIdComment resourceIdComment = getResourceIdComment();
if(resourceIdComment != null){
Expand Down
37 changes: 10 additions & 27 deletions src/main/java/com/reandroid/dex/smali/formatters/ClassComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
import com.reandroid.dex.model.DexClass;
import com.reandroid.dex.model.DexClassRepository;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.CollectionUtil;
import com.reandroid.utils.collection.ComputeIterator;

import java.io.IOException;
import java.util.List;

public interface ClassComment extends SmaliComment {

Expand All @@ -41,17 +37,11 @@ public ClassExtendComment(DexClassRepository classRepository) {
@Override
public void writeComment(SmaliWriter writer, TypeKey typeKey) throws IOException {
DexClass dexClass = classRepository.getDexClass(typeKey);
if(dexClass == null || dexClass.isFinal()) {
return;
}
List<TypeKey> extendingList = CollectionUtil.toList(
ComputeIterator.of(dexClass.getExtending(), DexClass::getKey));
extendingList.sort(CompareUtil.getComparableComparator());

for (TypeKey extending : extendingList) {
writer.appendComment("extended-by: ");
writer.appendComment(extending.getTypeName());
writer.newLine();
if (dexClass != null && !dexClass.isFinal()) {
SmaliComment.writeDeclarationComment(
writer,
"extended-by:",
dexClass.getExtending());
}
}
@Override
Expand Down Expand Up @@ -80,18 +70,11 @@ public ClassImplementComment(DexClassRepository classRepository) {
@Override
public void writeComment(SmaliWriter writer, TypeKey typeKey) throws IOException {
DexClass dexClass = classRepository.getDexClass(typeKey);
if(dexClass == null || !dexClass.isInterface()) {
return;
}
List<TypeKey> implementList = CollectionUtil.toList(
ComputeIterator.of(dexClass.getExtending(), DexClass::getKey));

implementList.sort(CompareUtil.getComparableComparator());

for (TypeKey impl : implementList) {
writer.appendComment("implemented-by: ");
writer.appendComment(impl.getTypeName());
writer.newLine();
if (dexClass != null && dexClass.isInterface()) {
SmaliComment.writeDeclarationComment(
writer,
"implemented-by:",
dexClass.getExtending());
}
}
@Override
Expand Down
40 changes: 14 additions & 26 deletions src/main/java/com/reandroid/dex/smali/formatters/MethodComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
package com.reandroid.dex.smali.formatters;

import com.reandroid.dex.key.MethodKey;
import com.reandroid.dex.key.TypeKey;
import com.reandroid.dex.model.DexClassRepository;
import com.reandroid.dex.model.DexMethod;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.CollectionUtil;
import com.reandroid.utils.collection.ComputeIterator;
import com.reandroid.utils.collection.SingleIterator;

import java.io.IOException;
import java.util.List;

public interface MethodComment extends SmaliComment{

Expand All @@ -43,14 +39,14 @@ public MethodOverrideComment(DexClassRepository classRepository){
@Override
public void writeComment(SmaliWriter writer, MethodKey methodKey) throws IOException {
DexMethod dexMethod = classRepository.getDeclaredMethod(methodKey);
if(dexMethod == null || dexMethod.isDirect()){
return;
}
DexMethod superMethod = CollectionUtil.getFirst(dexMethod.getSuperMethods());
if(superMethod != null){
writer.newLine();
writer.appendComment("overrides: ");
writer.appendComment(superMethod.getKey().getDeclaring().getTypeName());
if (dexMethod != null && !dexMethod.isDirect()) {
DexMethod superMethod = dexMethod.getSuperMethod();
if (superMethod != null) {
SmaliComment.writeDeclarationComment(
writer,
"overrides:",
SingleIterator.of(superMethod));
}
}
}

Expand Down Expand Up @@ -81,19 +77,11 @@ public MethodImplementComment(DexClassRepository classRepository){
@Override
public void writeComment(SmaliWriter writer, MethodKey methodKey) throws IOException {
DexMethod dexMethod = classRepository.getDeclaredMethod(methodKey);
if(dexMethod == null || dexMethod.isDirect() || dexMethod.getDexClass().isFinal()){
return;
}

List<TypeKey> implementList = CollectionUtil.toList(ComputeIterator.of(
dexMethod.getOverriding(), method -> method.getKey().getDeclaring()));

implementList.sort(CompareUtil.getComparableComparator());

for (TypeKey key : implementList) {
writer.newLine();
writer.appendComment("implemented-by: ");
writer.appendComment(key.getTypeName());
if (dexMethod != null && !dexMethod.isDirect() && !dexMethod.getDexClass().isFinal()) {
SmaliComment.writeDeclarationComment(
writer,
"implemented-by:",
dexMethod.getOverriding());
}
}
@Override
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/reandroid/dex/smali/formatters/SmaliComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,52 @@
*/
package com.reandroid.dex.smali.formatters;

import com.reandroid.dex.key.TypeKey;
import com.reandroid.dex.model.DexDeclaration;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.dex.smali.SmaliWriterSetting;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.collection.CollectionUtil;
import com.reandroid.utils.collection.ComputeIterator;

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

public interface SmaliComment {

static void writeDeclarationComment(SmaliWriter writer,
String label,
Iterator<? extends DexDeclaration> iterator) throws IOException {

List<TypeKey> typeKeyList = CollectionUtil.toList(
ComputeIterator.of(iterator, DexDeclaration::getDefining));

typeKeyList.sort(CompareUtil.getComparableComparator());

int size = typeKeyList.size();
int remaining = 0;
SmaliWriterSetting setting = writer.getWriterSetting();
if (setting != null) {
int max = setting.getMaximumCommentLines();
if (max >= 0 && max < size) {
if (max != 0) {
remaining = size - max;
}
size = max;
}
}

for (int i = 0; i < size; i++) {
writer.newLine();
TypeKey typeKey = typeKeyList.get(i);
writer.appendComment(label);
writer.appendComment(typeKey.getTypeName());
}

if (remaining != 0) {
writer.newLine();
writer.appendComment(label + " +" + remaining + " more");
}
}
}

0 comments on commit 5239dd0

Please sign in to comment.