use of org.jf.dexlib2.iface.value.TypeEncodedValue in project soot by Sable.
the class DexMethod method getThrownExceptions.
protected List<SootClass> getThrownExceptions(final Method method) {
// the following snippet retrieves all exceptions that this method
// throws by analyzing its annotations
List<SootClass> thrownExceptions = new ArrayList<SootClass>();
for (Annotation a : method.getAnnotations()) {
Type atype = DexType.toSoot(a.getType());
String atypes = atype.toString();
if (!(atypes.equals("dalvik.annotation.Throws")))
continue;
for (AnnotationElement ae : a.getElements()) {
EncodedValue ev = ae.getValue();
if (ev instanceof ArrayEncodedValue) {
for (EncodedValue evSub : ((ArrayEncodedValue) ev).getValue()) {
if (evSub instanceof TypeEncodedValue) {
TypeEncodedValue valueType = (TypeEncodedValue) evSub;
String exceptionName = valueType.getValue();
String dottedName = Util.dottedClassName(exceptionName);
thrownExceptions.add(SootResolver.v().makeClassRef(dottedName));
}
}
}
}
}
return thrownExceptions;
}
use of org.jf.dexlib2.iface.value.TypeEncodedValue in project soot by Sable.
the class DexAnnotation method handleAnnotation.
/**
* @param annotations
* @return
*/
private List<Tag> handleAnnotation(Set<? extends org.jf.dexlib2.iface.Annotation> annotations, String classType) {
if (annotations == null || annotations.size() == 0)
return null;
List<Tag> tags = new ArrayList<Tag>();
// RUNTIME_VISIBLE,
VisibilityAnnotationTag[] vatg = new VisibilityAnnotationTag[3];
for (Annotation a : annotations) {
int v = getVisibility(a.getVisibility());
Tag t = null;
Type atype = DexType.toSoot(a.getType());
String atypes = atype.toString();
int eSize = a.getElements().size();
if (atypes.equals("dalvik.annotation.AnnotationDefault")) {
if (eSize != 1)
throw new RuntimeException("error: expected 1 element for annotation Default. Got " + eSize + " instead.");
// get element
AnnotationElem e = getElements(a.getElements()).get(0);
AnnotationTag adt = new AnnotationTag(a.getType());
adt.addElem(e);
if (vatg[v] == null)
vatg[v] = new VisibilityAnnotationTag(v);
vatg[v].addAnnotation(adt);
} else if (atypes.equals("dalvik.annotation.EnclosingClass")) {
if (eSize != 1)
throw new RuntimeException("error: expected 1 element for annotation EnclosingClass. Got " + eSize + " instead.");
for (AnnotationElement elem : a.getElements()) {
String outerClass = ((TypeEncodedValue) elem.getValue()).getValue();
outerClass = Util.dottedClassName(outerClass);
// repair it
if (outerClass.equals(clazz.getName())) {
if (outerClass.contains("$-")) {
/*
* This is a special case for generated lambda
* classes of jack and jill compiler. Generated
* lambda classes may contain '$' which do not
* indicate an inner/outer class separator if the
* '$' occurs after a inner class with a name
* starting with '-'. Thus we search for '$-' and
* anything after it including '-' is the inner
* classes name and anything before it is the outer
* classes name.
*/
outerClass = outerClass.substring(0, outerClass.indexOf("$-"));
} else if (outerClass.contains("$")) {
// remove everything after the last '$' including
// the last '$'
outerClass = outerClass.substring(0, outerClass.lastIndexOf("$"));
}
}
deps.typesToSignature.add(RefType.v(outerClass));
clazz.setOuterClass(SootResolver.v().makeClassRef(outerClass));
assert clazz.getOuterClass() != clazz;
}
// annotation.
continue;
} else if (atypes.equals("dalvik.annotation.EnclosingMethod")) {
// ignore the annotation
if (eSize == 0)
continue;
// If the pointer is ambiguous, we are in trouble
if (eSize != 1)
throw new RuntimeException("error: expected 1 element for annotation EnclosingMethod. Got " + eSize + " instead.");
AnnotationStringElem e = (AnnotationStringElem) getElements(a.getElements()).get(0);
String[] split1 = e.getValue().split("\\ \\|");
String classString = split1[0];
String methodString = split1[1];
String parameters = split1[2];
String returnType = split1[3];
String methodSigString = "(" + parameters + ")" + returnType;
t = new EnclosingMethodTag(classString, methodString, methodSigString);
String outerClass = classString.replace("/", ".");
deps.typesToSignature.add(RefType.v(outerClass));
clazz.setOuterClass(SootResolver.v().makeClassRef(outerClass));
assert clazz.getOuterClass() != clazz;
} else if (atypes.equals("dalvik.annotation.InnerClass")) {
// access flags of the inner class
int accessFlags = -1;
// name of the inner class
String name = null;
for (AnnotationElem ele : getElements(a.getElements())) {
if (ele instanceof AnnotationIntElem && ele.getName().equals("accessFlags"))
accessFlags = ((AnnotationIntElem) ele).getValue();
else if (ele instanceof AnnotationStringElem && ele.getName().equals("name"))
name = ((AnnotationStringElem) ele).getValue();
else
throw new RuntimeException("Unexpected inner class annotation element");
}
// outer class name
String outerClass;
if (clazz.hasOuterClass()) {
// If we have already set an outer class from some other
// annotation, we use that
// one.
outerClass = clazz.getOuterClass().getName();
} else if (classType.contains("$-")) {
/*
* This is a special case for generated lambda classes of
* jack and jill compiler. Generated lambda classes may
* contain '$' which do not indicate an inner/outer class
* separator if the '$' occurs after a inner class with a
* name starting with '-'. Thus we search for '$-' and
* anything after it including '-' is the inner classes name
* and anything before it is the outer classes name.
*/
outerClass = classType.substring(0, classType.indexOf("$-"));
if (Util.isByteCodeClassName(classType))
outerClass += ";";
} else if (classType.contains("$")) {
// remove everything after the last '$' including the last
// '$'
outerClass = classType.substring(0, classType.lastIndexOf("$")) + ";";
if (Util.isByteCodeClassName(classType))
outerClass += ";";
} else {
// Make sure that no funny business is going on if the
// annotation is broken and does not end in $nn.
outerClass = null;
}
Tag innerTag = new InnerClassTag(DexType.toSootICAT(classType), outerClass == null ? null : DexType.toSootICAT(outerClass), name, accessFlags);
tags.add(innerTag);
if (outerClass != null && !clazz.hasOuterClass()) {
String sootOuterClass = Util.dottedClassName(outerClass);
deps.typesToSignature.add(RefType.v(sootOuterClass));
clazz.setOuterClass(SootResolver.v().makeClassRef(sootOuterClass));
assert clazz.getOuterClass() != clazz;
}
continue;
} else if (atypes.equals("dalvik.annotation.MemberClasses")) {
AnnotationArrayElem e = (AnnotationArrayElem) getElements(a.getElements()).get(0);
for (AnnotationElem ae : e.getValues()) {
AnnotationClassElem c = (AnnotationClassElem) ae;
String innerClass = c.getDesc();
String outerClass;
String name;
if (innerClass.contains("$-")) {
/*
* This is a special case for generated lambda classes
* of jack and jill compiler. Generated lambda classes
* may contain '$' which do not indicate an inner/outer
* class separator if the '$' occurs after a inner class
* with a name starting with '-'. Thus we search for
* '$-' and anything after it including '-' is the inner
* classes name and anything before it is the outer
* classes name.
*/
int i = innerClass.indexOf("$-");
outerClass = innerClass.substring(0, i);
name = innerClass.substring(i + 2).replaceAll(";$", "");
} else if (innerClass.contains("$")) {
// remove everything after the last '$' including the
// last '$'
int i = innerClass.lastIndexOf("$");
outerClass = innerClass.substring(0, i);
name = innerClass.substring(i + 1).replaceAll(";$", "");
} else {
// Make sure that no funny business is going on if the
// annotation is broken and does not end in $nn.
outerClass = null;
name = null;
}
if (// anonymous or
name != null && name.matches("^\\d*$"))
// local inner
// classes
name = null;
// seems like this information is lost
int accessFlags = 0;
// during the .class -- dx --> .dex
// process.
Tag innerTag = new InnerClassTag(DexType.toSootICAT(innerClass), outerClass == null ? null : DexType.toSootICAT(outerClass), name, accessFlags);
tags.add(innerTag);
}
continue;
} else if (atypes.equals("dalvik.annotation.Signature")) {
if (eSize != 1)
throw new RuntimeException("error: expected 1 element for annotation Signature. Got " + eSize + " instead.");
AnnotationArrayElem e = (AnnotationArrayElem) getElements(a.getElements()).get(0);
String sig = "";
for (AnnotationElem ae : e.getValues()) {
AnnotationStringElem s = (AnnotationStringElem) ae;
sig += s.getValue();
}
t = new SignatureTag(sig);
} else if (atypes.equals("dalvik.annotation.Throws")) {
// this is handled in soot.dexpler.DexMethod
continue;
} else if (atypes.equals("java.lang.Deprecated")) {
if (eSize != 0)
throw new RuntimeException("error: expected 1 element for annotation Deprecated. Got " + eSize + " instead.");
t = new DeprecatedTag();
AnnotationTag adt = new AnnotationTag("Ljava/lang/Deprecated;");
if (vatg[v] == null)
vatg[v] = new VisibilityAnnotationTag(v);
vatg[v].addAnnotation(adt);
} else {
if (vatg[v] == null)
vatg[v] = new VisibilityAnnotationTag(v);
AnnotationTag tag = new AnnotationTag(a.getType());
for (AnnotationElem e : getElements(a.getElements())) tag.addElem(e);
vatg[v].addAnnotation(tag);
}
tags.add(t);
}
for (VisibilityAnnotationTag vat : vatg) if (vat != null)
tags.add(vat);
return tags;
}
use of org.jf.dexlib2.iface.value.TypeEncodedValue 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.startsWith("L")) {
newValue = value;
} else {
if (value.endsWith(";")) {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray1);
} else {
newValue = DefineUtils.getDotDefineClassName(classProcessor.classProcess(DefineUtils.getDotDalvikClassName(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.startsWith("Ljava/lang/") || !value.startsWith("L")) {
newValue = value;
} else {
if (value.endsWith(";")) {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray);
} else {
newValue = DefineUtils.getDotDefineClassName(classProcessor.classProcess(DefineUtils.getDotDalvikClassName(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 isArray3 = false;
if (returnType.startsWith("[")) {
isArray3 = true;
}
boolean isBasic = basicType.containsKey(returnType);
List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
for (CharSequence charSequence : paramTypes) {
boolean isArray1 = false;
if (charSequence.toString().startsWith("[")) {
isArray1 = true;
}
if (basicType.containsKey(charSequence.toString())) {
newParamTypes.add(charSequence);
dalvikParamTypes.add(basicType.get(charSequence.toString()));
continue;
}
dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray1 ? "[]" : ""));
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()) + (isArray3 ? "[]" : ""), 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.value.TypeEncodedValue in project atlas by alibaba.
the class ApkPatch method getMethodAnnotaionPrepareClasses.
public static void getMethodAnnotaionPrepareClasses(DexDiffInfo dexDiffInfo, Set<String> prepareclasses) {
for (DexBackedMethod method : dexDiffInfo.getModifiedMethods()) {
Set<? extends Annotation> annotations = method.getAnnotations();
if (annotations == null) {
continue;
}
for (Annotation annotation : annotations) {
String type = annotation.getType();
if (type != null && type.startsWith("L") && type.endsWith(";")) {
prepareclasses.add(type.substring(1, type.length() - 1).replace('/', '.'));
System.out.println("prepare class: " + type);
}
Set<? extends AnnotationElement> elements = annotation.getElements();
for (AnnotationElement dexBackedAnnotationElement : elements) {
if (dexBackedAnnotationElement.getValue() instanceof DexBackedArrayEncodedValue) {
List<? extends EncodedValue> values = ((DexBackedArrayEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
for (EncodedValue encodedValue : values) {
if (encodedValue instanceof TypeEncodedValue) {
prepareclasses.add(((TypeEncodedValue) encodedValue).getValue().substring(1, ((TypeEncodedValue) encodedValue).getValue().length() - 1).replace('/', '.'));
System.out.println("prepare class: " + ((TypeEncodedValue) encodedValue).getValue());
}
}
} else if (dexBackedAnnotationElement.getValue() instanceof DexBackedTypeEncodedValue) {
String value = ((DexBackedTypeEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
prepareclasses.add(value.substring(1, value.length() - 1).replace('/', '.'));
System.out.println("prepare class: " + value);
}
}
}
}
}
use of org.jf.dexlib2.iface.value.TypeEncodedValue in project atlas by alibaba.
the class ApkPatch method getClassAnnotaionPrepareClasses.
public static void getClassAnnotaionPrepareClasses(DexBackedClassDef classDef, Set<String> prepareclasses, DexDiffInfo dexDiffInfo) {
for (DexBackedClassDef modifyClasses : dexDiffInfo.getModifiedClasses()) {
if (classDef.getType().equals(modifyClasses.getType())) {
if (classDef.getAnnotations() != null) {
Set<? extends DexBackedAnnotation> annotations = classDef.getAnnotations();
for (DexBackedAnnotation annotation : annotations) {
String type = annotation.getType();
if (type != null && type.startsWith("L") && type.endsWith(";")) {
prepareclasses.add(type.substring(1, type.length() - 1).replace('/', '.'));
System.out.println("prepare class: " + type);
}
Set<? extends DexBackedAnnotationElement> elements = annotation.getElements();
for (DexBackedAnnotationElement dexBackedAnnotationElement : elements) {
if (dexBackedAnnotationElement.getValue() instanceof DexBackedArrayEncodedValue) {
List<? extends EncodedValue> values = ((DexBackedArrayEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
for (EncodedValue encodedValue : values) {
if (encodedValue instanceof TypeEncodedValue) {
prepareclasses.add(((TypeEncodedValue) encodedValue).getValue().substring(1, ((TypeEncodedValue) encodedValue).getValue().length() - 1).replace('/', '.'));
System.out.println("prepare class: " + ((TypeEncodedValue) encodedValue).getValue());
}
}
} else if (dexBackedAnnotationElement.getValue() instanceof DexBackedTypeEncodedValue) {
String value = ((DexBackedTypeEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
prepareclasses.add(value.substring(1, value.length() - 1).replace('/', '.'));
System.out.println("prepare class: " + value);
} else if (dexBackedAnnotationElement.getValue() instanceof DexBackedAnnotationEncodedValue) {
String value = ((DexBackedAnnotationEncodedValue) dexBackedAnnotationElement.getValue()).getType();
}
}
}
}
}
}
}
Aggregations