use of org.jf.dexlib2.iface.reference.MethodReference in project atlas by alibaba.
the class MethodDefinition method addInstructionMethodItems.
private void addInstructionMethodItems(List<MethodItem> methodItems) {
int currentCodeAddress = 0;
for (int i = 0; i < effectiveInstructions.size(); i++) {
Instruction instruction = effectiveInstructions.get(i);
MethodItem methodItem = InstructionMethodItemFactory.makeInstructionFormatMethodItem(this, currentCodeAddress, instruction);
methodItems.add(methodItem);
if (i != effectiveInstructions.size() - 1) {
methodItems.add(new BlankMethodItem(currentCodeAddress));
}
if (classDef.options.addCodeOffsets) {
methodItems.add(new MethodItem(currentCodeAddress) {
@Override
public double getSortOrder() {
return -1000;
}
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
writer.write("#@");
writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL);
return true;
}
});
}
if (!classDef.options.noAccessorComments && (instruction instanceof ReferenceInstruction)) {
Opcode opcode = instruction.getOpcode();
if (opcode.referenceType == ReferenceType.METHOD) {
MethodReference methodReference = null;
try {
methodReference = (MethodReference) ((ReferenceInstruction) instruction).getReference();
} catch (InvalidItemIndex ex) {
// just ignore it for now. We'll deal with it later, when processing the instructions
// themselves
}
if (methodReference != null && SyntheticAccessorResolver.looksLikeSyntheticAccessor(methodReference.getName())) {
AccessedMember accessedMember = classDef.options.syntheticAccessorResolver.getAccessedMember(methodReference);
if (accessedMember != null) {
methodItems.add(new SyntheticAccessCommentMethodItem(accessedMember, currentCodeAddress));
}
}
}
}
currentCodeAddress += instruction.getCodeUnits();
}
}
use of org.jf.dexlib2.iface.reference.MethodReference in project atlas by alibaba.
the class ClassReIClassDef method reAnnotation.
@Override
protected Annotation reAnnotation(Annotation annotation) {
String type = annotation.getType();
boolean isArray = false;
if (type.startsWith("[")) {
isArray = true;
}
String newType = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, isArray);
Set<? extends AnnotationElement> sets = annotation.getElements();
Set<ImmutableAnnotationElement> newAnnotationElement = new HashSet<ImmutableAnnotationElement>();
for (AnnotationElement annotationElement : sets) {
String name = annotationElement.getName();
EncodedValue encodedValue = annotationElement.getValue();
if (encodedValue instanceof ArrayEncodedValue) {
List<EncodedValue> lists = new ArrayList<EncodedValue>();
for (EncodedValue encodedValueSub : ((ArrayEncodedValue) encodedValue).getValue()) {
if (encodedValueSub instanceof StringEncodedValue) {
String newValue = null;
boolean isArray1 = false;
String value = ((StringEncodedValue) encodedValueSub).getValue();
if (value.startsWith("[")) {
isArray1 = true;
}
if (basicValue.contains(value)) {
newValue = value;
} else if (value.startsWith("Ljava/util/") || value.startsWith("Ljava/lang/") || !value.endsWith(";")) {
newValue = value;
} else {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray1);
}
ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
lists.add(immutableStringEncodedValue);
} else if (encodedValueSub instanceof TypeEncodedValue) {
String newValueSub = null;
String value = ((TypeEncodedValue) encodedValueSub).getValue();
boolean isArray2 = false;
if (value.startsWith("[")) {
isArray2 = true;
}
if (basicValue.contains(value)) {
newValueSub = value;
} else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
newValueSub = value;
} else {
newValueSub = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
}
ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValueSub);
lists.add(immutableTypeEncodedValue);
} else {
lists.add(encodedValue);
}
}
ImmutableArrayEncodedValue immutableArrayEncodedValue = new ImmutableArrayEncodedValue(lists);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableArrayEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else if (encodedValue instanceof StringEncodedValue) {
String value = ((StringEncodedValue) encodedValue).getValue();
String newValue = null;
isArray = false;
if (value.startsWith("[")) {
isArray = true;
}
if (basicValue.contains(value)) {
newValue = value;
} else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
newValue = value;
} else {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray);
}
ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableStringEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else if (encodedValue instanceof TypeEncodedValue) {
String newValue = null;
String value = ((TypeEncodedValue) encodedValue).getValue();
boolean isArray2 = false;
if (value.startsWith("[")) {
isArray2 = true;
}
if (basicValue.contains(value)) {
newValue = value;
} else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
newValue = value;
} else {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
}
ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValue);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableTypeEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else if (encodedValue instanceof MethodEncodedValue) {
MethodReference methodReference = ((MethodEncodedValue) encodedValue).getValue();
String returnType = methodReference.getReturnType();
boolean isBasic = false;
List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
for (CharSequence charSequence : paramTypes) {
if (basicType.containsKey(charSequence.toString())) {
newParamTypes.add(charSequence);
dalvikParamTypes.add(basicType.get(charSequence.toString()));
continue;
}
dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray));
}
final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, false), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
ImmutableMethodEncodedValue immutableMethodEncodedValue = new ImmutableMethodEncodedValue(immutableReference);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableMethodEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else {
newAnnotationElement.add(ImmutableAnnotationElement.of(annotationElement));
}
}
return new ImmutableAnnotation(annotation.getVisibility(), newType, newAnnotationElement);
}
use of org.jf.dexlib2.iface.reference.MethodReference in project atlas by alibaba.
the class InsTructionsReIClassDef method reInstructions.
@Override
protected Iterable<? extends Instruction> reInstructions(Iterable<? extends Instruction> instructions) {
final List<Instruction> reinstructions = new ArrayList<Instruction>();
for (final Instruction instruction : instructions) {
if (instruction instanceof ReferenceInstruction) {
Opcode opcode = instruction.getOpcode();
if (opcode.referenceType == ReferenceType.METHOD) {
boolean isBasic = false;
MethodReference methodReference = null;
try {
methodReference = (MethodReference) ((ReferenceInstruction) instruction).getReference();
if (methodReference.getDefiningClass().contains("Ljava/lang") || methodReference.getDefiningClass().startsWith("Ljava/util/") || methodReference.getDefiningClass().startsWith("[Ljava/lang")) {
reinstructions.add(ImmutableInstruction.of(instruction));
continue;
}
String returnType = methodReference.getReturnType();
boolean isArray = false;
if (returnType.startsWith("[")) {
isArray = true;
}
String methodName = methodReference.getName();
if (methodName.equals("InitBundleInfoByVersionIfNeed")) {
System.out.println("InitBundleInfoByVersionIfNeed");
}
if (basicType.containsKey(returnType)) {
isBasic = true;
}
List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
for (CharSequence charSequence : paramTypes) {
if (basicType.containsKey(charSequence.toString())) {
newParamTypes.add(charSequence);
dalvikParamTypes.add(basicType.get(charSequence.toString()));
continue;
}
boolean isArray1 = charSequence.toString().startsWith("[");
dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray1));
}
final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, false), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableReference));
}
} catch (Exception e) {
}
} else if (opcode.referenceType == ReferenceType.FIELD) {
FieldReference fieldReference = null;
boolean isBasic = false;
boolean isBasicArray = false;
fieldReference = (FieldReference) ((ReferenceInstruction) instruction).getReference();
if (fieldReference.getDefiningClass().startsWith("Ljava/lang/") || fieldReference.getDefiningClass().startsWith("Ljava/util/") || fieldReference.getDefiningClass().startsWith("[Ljava/lang/")) {
reinstructions.add(ImmutableInstruction.of(instruction));
continue;
}
if (basicType.containsKey(fieldReference.getType())) {
isBasic = true;
}
final ImmutableFieldReference immutableFieldReference = new ImmutableFieldReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(fieldReference.getDefiningClass())).className, false), classProcessor.filedProcess(DefineUtils.getDalvikClassName(fieldReference.getDefiningClass()), isBasic ? basicType.get(fieldReference.getType()) : DefineUtils.getDalvikClassName(fieldReference.getType()), fieldReference.getName()).fieldName, isBasic ? fieldReference.getType() : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(fieldReference.getType())).className, fieldReference.getType().startsWith("[")));
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableFieldReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableFieldReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableFieldReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableFieldReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableFieldReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableFieldReference));
}
} else if (opcode.referenceType == ReferenceType.TYPE) {
TypeReference typeReference = (TypeReference) ((ReferenceInstruction) instruction).getReference();
String type = typeReference.getType();
if (!basicType.containsKey(type) && !type.startsWith("Ljava/lang") && !type.startsWith("Ljava/util/") && !type.startsWith("[Ljava/lang")) {
type = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, type.startsWith("["));
}
ImmutableTypeReference immutableTypeReference = new ImmutableTypeReference(type);
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableTypeReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableTypeReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableTypeReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableTypeReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableTypeReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableTypeReference));
}
} else if (opcode.referenceType == ReferenceType.STRING) {
StringReference stringReference = (StringReference) ((ReferenceInstruction) instruction).getReference();
String type = stringReference.getString();
// if (!basicType.contains(type) && !type.startsWith("[Ljava/lang")) {
// type = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className);
// }
ImmutableStringReference immutableStringReference = new ImmutableStringReference(type);
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableStringReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableStringReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableStringReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableStringReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableStringReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableStringReference));
}
} else {
reinstructions.add(ImmutableInstruction.of(instruction));
}
} else {
reinstructions.add(ImmutableInstruction.of(instruction));
}
}
return new Iterable<Instruction>() {
@Override
public Iterator<Instruction> iterator() {
return reinstructions.iterator();
}
};
}
use of org.jf.dexlib2.iface.reference.MethodReference in project smali by JesusFreke.
the class SyntheticAccessorResolver method getAccessedMember.
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);
AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
if (accessedMember != null) {
return accessedMember;
}
String type = methodReference.getDefiningClass();
ClassDef classDef = classDefMap.get(type);
if (classDef == null) {
return null;
}
Method matchedMethod = null;
MethodImplementation matchedMethodImpl = null;
for (Method method : classDef.getMethods()) {
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl != null) {
if (methodReferenceEquals(method, methodReference)) {
matchedMethod = method;
matchedMethodImpl = methodImpl;
break;
}
}
}
if (matchedMethod == null) {
return null;
}
//A synthetic accessor will be marked synthetic
if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
return null;
}
List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());
int accessType = syntheticAccessorFSM.test(instructions);
if (accessType >= 0) {
AccessedMember member = new AccessedMember(accessType, ((ReferenceInstruction) instructions.get(0)).getReference());
resolvedAccessors.put(methodDescriptor, member);
return member;
}
return null;
}
use of org.jf.dexlib2.iface.reference.MethodReference in project smali by JesusFreke.
the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Static.
@Test
public void testCustomMethodInlineTable_Static() throws IOException {
List<ImmutableInstruction> instructions = Lists.newArrayList(new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID));
ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null);
ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.STATIC.getValue(), null, methodImpl);
ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, ImmutableList.of(method), null);
DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef));
ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile);
ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART);
InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V");
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false);
Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0);
Assert.assertEquals(Opcode.INVOKE_STATIC, deodexedInstruction.getOpcode());
MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
Assert.assertEquals(method, methodReference);
}
Aggregations