use of soot.tagkit.LongConstantValueTag in project soot by Sable.
the class SootClassBuilder method visitField.
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
soot.Type type = AsmUtil.toJimpleType(desc);
addDep(type);
SootField field = Scene.v().makeSootField(name, type, access);
Tag tag;
if (value instanceof Integer)
tag = new IntegerConstantValueTag((Integer) value);
else if (value instanceof Float)
tag = new FloatConstantValueTag((Float) value);
else if (value instanceof Long)
tag = new LongConstantValueTag((Long) value);
else if (value instanceof Double)
tag = new DoubleConstantValueTag((Double) value);
else if (value instanceof String)
tag = new StringConstantValueTag(value.toString());
else
tag = null;
if (tag != null)
field.addTag(tag);
if (signature != null)
field.addTag(new SignatureTag(signature));
field = klass.getOrAddField(field);
return new FieldBuilder(field, this);
}
use of soot.tagkit.LongConstantValueTag in project soot by Sable.
the class ConstantFieldValueFinder method valuesForPrimTypeFields.
/*
* This method gives values to all the fields in all the classes if they can be determined statically
* We only care about fields which have primitive types
*/
private void valuesForPrimTypeFields() {
// go through all the classes
Iterator classIt = appClasses.iterator();
while (classIt.hasNext()) {
SootClass s = (SootClass) classIt.next();
debug("\nvaluesforPrimTypeFields", "Processing class " + s.getName());
String declaringClass = s.getName();
Iterator fieldIt = s.getFields().iterator();
while (fieldIt.hasNext()) {
SootField f = (SootField) fieldIt.next();
String fieldName = f.getName();
Type fieldType = f.getType();
if (!(fieldType instanceof PrimType))
continue;
String combined = declaringClass + combiner + fieldName;
classNameFieldNameToSootFieldMapping.put(combined, f);
Object value = null;
// check for constant value tags
if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
value = new Double(val);
} else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
value = new Float(val);
} else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
value = new Long(val);
} else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
value = new Integer(val);
} else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
if (val == 0)
value = new Boolean(false);
else
value = new Boolean(true);
} else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
value = new Integer(val);
}
// if there was a constant value tag we have its value now
if (value != null) {
debug("TAGGED value found for field: " + combined);
primTypeFieldValueToUse.put(combined, value);
// continue with next field
continue;
}
// see if the field was never assigned in which case it gets default values
Object temp = fieldToValues.get(combined);
if (temp == null) {
if (fieldType instanceof DoubleType)
value = new Double(0);
else if (fieldType instanceof FloatType)
value = new Float(0);
else if (fieldType instanceof LongType)
value = new Long(0);
else if (fieldType instanceof BooleanType)
value = new Boolean(false);
else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) || fieldType instanceof CharType) {
value = new Integer(0);
} else
throw new DecompilationException("Unknown primitive type...please report to developer");
primTypeFieldValueToUse.put(combined, value);
debug("DEFAULT value for field: " + combined);
// continue with next field
continue;
}
// havent got a tag with value and havent use default since SOME method did define the field atleast once
// there was some value assigned!!!!!!!!!
debug("CHECKING USER ASSIGNED VALUES FOR: " + combined);
ArrayList values = (ArrayList) temp;
// check if they are all constants and that too the same constant
Iterator it = values.iterator();
NumericConstant tempConstant = null;
while (it.hasNext()) {
Value val = (Value) it.next();
if (!(val instanceof NumericConstant)) {
tempConstant = null;
debug("Not numeric constant hence giving up");
break;
}
if (tempConstant == null) {
tempConstant = (NumericConstant) val;
} else {
// check that this value is the same as previous
if (!tempConstant.equals(val)) {
tempConstant = null;
break;
}
}
}
if (tempConstant == null) {
// continue with next field cant do anything about this one
continue;
}
if (tempConstant instanceof LongConstant) {
Long tempVal = new Long(((LongConstant) tempConstant).value);
if (tempVal.compareTo(new Long(0)) == 0)
primTypeFieldValueToUse.put(combined, tempVal);
else
debug("Not assigning the agreed value since that is not the default value for " + combined);
} else if (tempConstant instanceof DoubleConstant) {
Double tempVal = new Double(((DoubleConstant) tempConstant).value);
if (tempVal.compareTo(new Double(0)) == 0)
primTypeFieldValueToUse.put(combined, tempVal);
else
debug("Not assigning the agreed value since that is not the default value for " + combined);
} else if (tempConstant instanceof FloatConstant) {
Float tempVal = new Float(((FloatConstant) tempConstant).value);
if (tempVal.compareTo(new Float(0)) == 0)
primTypeFieldValueToUse.put(combined, tempVal);
else
debug("Not assigning the agreed value since that is not the default value for " + combined);
} else if (tempConstant instanceof IntConstant) {
Integer tempVal = new Integer(((IntConstant) tempConstant).value);
if (tempVal.compareTo(new Integer(0)) == 0) {
SootField tempField = classNameFieldNameToSootFieldMapping.get(combined);
if (tempField.getType() instanceof BooleanType) {
primTypeFieldValueToUse.put(combined, new Boolean(false));
// System.out.println("puttingvalue false for"+combined);
} else {
primTypeFieldValueToUse.put(combined, tempVal);
// System.out.println("puttingvalue 0 for"+combined);
}
} else
debug("Not assigning the agreed value since that is not the default value for " + combined);
} else {
throw new DecompilationException("Un handled Numberic Constant....report to programmer");
}
}
// all fields of the class
}
// all classes
}
use of soot.tagkit.LongConstantValueTag in project soot by Sable.
the class ConstantValueToInitializerTransformer method transformClass.
public void transformClass(SootClass sc) {
SootMethod smInit = null;
Set<SootField> alreadyInitialized = new HashSet<SootField>();
for (SootField sf : sc.getFields()) {
// different constructors might assign different values.
if (!sf.isStatic() || !sf.isFinal())
continue;
// generate a second one
if (alreadyInitialized.contains(sf))
continue;
// Look for constant values
for (Tag t : sf.getTags()) {
Stmt initStmt = null;
if (t instanceof DoubleConstantValueTag) {
double value = ((DoubleConstantValueTag) t).getDoubleValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), DoubleConstant.v(value));
} else if (t instanceof FloatConstantValueTag) {
float value = ((FloatConstantValueTag) t).getFloatValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), FloatConstant.v(value));
} else if (t instanceof IntegerConstantValueTag) {
int value = ((IntegerConstantValueTag) t).getIntValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), IntConstant.v(value));
} else if (t instanceof LongConstantValueTag) {
long value = ((LongConstantValueTag) t).getLongValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), LongConstant.v(value));
} else if (t instanceof StringConstantValueTag) {
String value = ((StringConstantValueTag) t).getStringValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), StringConstant.v(value));
}
if (initStmt != null) {
if (smInit == null)
smInit = getOrCreateInitializer(sc, alreadyInitialized);
if (smInit != null)
smInit.getActiveBody().getUnits().addFirst(initStmt);
}
}
}
if (smInit != null) {
Chain<Unit> units = smInit.getActiveBody().getUnits();
if (units.isEmpty() || !(units.getLast() instanceof ReturnVoidStmt))
units.add(Jimple.v().newReturnVoidStmt());
}
}
use of soot.tagkit.LongConstantValueTag in project soot by Sable.
the class DexPrinter method makeConstantItem.
private EncodedValue makeConstantItem(SootField sf, Tag t) {
if (!(t instanceof ConstantValueTag))
throw new RuntimeException("error: t not ConstantValueTag.");
if (t instanceof IntegerConstantValueTag) {
Type sft = sf.getType();
IntegerConstantValueTag i = (IntegerConstantValueTag) t;
if (sft instanceof BooleanType) {
int v = i.getIntValue();
if (v == 0) {
return ImmutableBooleanEncodedValue.FALSE_VALUE;
} else if (v == 1) {
return ImmutableBooleanEncodedValue.TRUE_VALUE;
} else {
throw new RuntimeException("error: boolean value from int with value != 0 or 1.");
}
} else if (sft instanceof CharType) {
return new ImmutableCharEncodedValue((char) i.getIntValue());
} else if (sft instanceof ByteType) {
return new ImmutableByteEncodedValue((byte) i.getIntValue());
} else if (sft instanceof IntType) {
return new ImmutableIntEncodedValue(i.getIntValue());
} else if (sft instanceof ShortType) {
return new ImmutableShortEncodedValue((short) i.getIntValue());
} else {
throw new RuntimeException("error: unexpected constant tag type: " + t + " for field " + sf);
}
} else if (t instanceof LongConstantValueTag) {
LongConstantValueTag l = (LongConstantValueTag) t;
return new ImmutableLongEncodedValue(l.getLongValue());
} else if (t instanceof DoubleConstantValueTag) {
DoubleConstantValueTag d = (DoubleConstantValueTag) t;
return new ImmutableDoubleEncodedValue(d.getDoubleValue());
} else if (t instanceof FloatConstantValueTag) {
FloatConstantValueTag f = (FloatConstantValueTag) t;
return new ImmutableFloatEncodedValue(f.getFloatValue());
} else if (t instanceof StringConstantValueTag) {
StringConstantValueTag s = (StringConstantValueTag) t;
if (sf.getType().equals(RefType.v("java.lang.String")))
return new ImmutableStringEncodedValue(s.getStringValue());
else
// Results in "Bogus static initialization"
return null;
} else
throw new RuntimeException("Unexpected constant type");
}
use of soot.tagkit.LongConstantValueTag in project robovm by robovm.
the class MethodCompiler method initializeClassFields.
private void initializeClassFields() {
for (SootField field : sootMethod.getDeclaringClass().getFields()) {
if (!field.isStatic()) {
continue;
}
for (Tag tag : field.getTags()) {
Value value = null;
if (tag instanceof DoubleConstantValueTag) {
DoubleConstantValueTag dtag = (DoubleConstantValueTag) tag;
value = new FloatingPointConstant(dtag.getDoubleValue());
} else if (tag instanceof FloatConstantValueTag) {
FloatConstantValueTag ftag = (FloatConstantValueTag) tag;
value = new FloatingPointConstant(ftag.getFloatValue());
} else if (tag instanceof IntegerConstantValueTag) {
IntegerConstantValueTag itag = (IntegerConstantValueTag) tag;
value = new IntegerConstant(itag.getIntValue());
IntegerType type = (IntegerType) getType(field.getType());
if (type.getBits() < 32) {
value = new ConstantTrunc((Constant) value, type);
}
} else if (tag instanceof LongConstantValueTag) {
LongConstantValueTag ltag = (LongConstantValueTag) tag;
value = new IntegerConstant(ltag.getLongValue());
} else if (tag instanceof StringConstantValueTag) {
String s = ((StringConstantValueTag) tag).getStringValue();
value = call(ldcString(s), env);
}
if (value != null) {
FunctionRef fn = FunctionBuilder.setter(field).ref();
call(fn, env, value);
}
}
}
}
Aggregations