Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package soot.dava.toolkits.base.AST.transformations;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2006 Nomair A. Naeem ([email protected])
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import soot.BooleanType;
import soot.ByteType;
import soot.CharType;
import soot.DoubleType;
import soot.FloatType;
import soot.IntType;
import soot.Local;
import soot.LongType;
import soot.RefType;
import soot.ShortType;
import soot.SootClass;
import soot.SootField;
import soot.SootFieldRef;
import soot.SootMethod;
import soot.Type;
import soot.Value;
import soot.dava.DavaBody;
import soot.dava.DecompilationException;
import soot.dava.internal.AST.ASTMethodNode;
import soot.dava.internal.AST.ASTNode;
import soot.dava.internal.AST.ASTStatementSequenceNode;
import soot.dava.internal.AST.ASTTryNode;
import soot.dava.internal.asg.AugmentedStmt;
import soot.dava.internal.javaRep.DInstanceFieldRef;
import soot.dava.internal.javaRep.DIntConstant;
import soot.dava.internal.javaRep.DStaticFieldRef;
import soot.dava.internal.javaRep.DVariableDeclarationStmt;
import soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter;
import soot.dava.toolkits.base.AST.structuredAnalysis.MustMayInitialize;
import soot.dava.toolkits.base.AST.traversals.AllVariableUses;
import soot.grimp.internal.GAssignStmt;
import soot.jimple.DefinitionStmt;
import soot.jimple.DoubleConstant;
import soot.jimple.FloatConstant;
import soot.jimple.InvokeExpr;
import soot.jimple.LongConstant;
import soot.jimple.NullConstant;
import soot.jimple.Stmt;
import soot.jimple.internal.JimpleLocal;
/**
* Maintained by: Nomair A. Naeem
*/
/**
* CHANGE LOG: 30th January 2006: Class was created to get rid of the field might not be initialized error that used to show
* up when recompiling decompiled code Will be throughly covered in "Programmer Friendly Code" Sable Tech Report (2006)
*
*/
/*
* This class makes sure there is an initialization of all final variables (static or non static). If we cant guarantee
* initialization (may be initialized on multiple paths but not all) then we remove the final keyword
*/
public class FinalFieldDefinition {
// extends DepthFirstAdapter{
SootClass sootClass;
SootMethod sootMethod;
DavaBody davaBody;
List cancelFinalModifier;
public FinalFieldDefinition(ASTMethodNode node) {
davaBody = node.getDavaBody();
sootMethod = davaBody.getMethod();
sootClass = sootMethod.getDeclaringClass();
String subSignature = sootMethod.getName();
if (!(subSignature.compareTo("") == 0 || subSignature.compareTo("") == 0)) {
// dont care about these since we want only static block and
// constructors
// System.out.println("\n\nName"+sootMethod.getName()+"
// SubSignature:"+sootMethod.getSubSignature());
return;
}
// create a list of interesting vars
ArrayList interesting = findFinalFields();
if (interesting.size() == 0) {
// no final fields of interest
return;
}
cancelFinalModifier = new ArrayList();
analyzeMethod(node, interesting);
Iterator it = cancelFinalModifier.iterator();
while (it.hasNext()) {
SootField field = it.next();
field.setModifiers((soot.Modifier.FINAL ^ 0xFFFF) & field.getModifiers());
}
}
/*
* this method finds all the final fields in this class and assigns them to the finalFields list
*
* Note this stores a list of SootFields!!!
*
* Fields which are initialized in their declaration should not be added
*/
public ArrayList findFinalFields() {
// first thing is to get a list of all final fields in the class
ArrayList interestingFinalFields = new ArrayList();
Iterator fieldIt = sootClass.getFields().iterator();
while (fieldIt.hasNext()) {
SootField tempField = (SootField) fieldIt.next();
if (tempField.isFinal()) {
// if its static final and method is static add
if (tempField.isStatic() && sootMethod.getName().compareTo("") == 0) {
interestingFinalFields.add(tempField);
}
// if its non static and final and method is constructor add
if ((!tempField.isStatic()) && sootMethod.getName().compareTo("") == 0) {
interestingFinalFields.add(tempField);
}
}
}
return interestingFinalFields;
}
public void analyzeMethod(ASTMethodNode node, List varsOfInterest) {
MustMayInitialize must = new MustMayInitialize(node, MustMayInitialize.MUST);
Iterator it = varsOfInterest.iterator();
while (it.hasNext()) {
SootField interest = it.next();
// check for constant value tags
Type fieldType = interest.getType();
if (fieldType instanceof DoubleType && interest.hasTag("DoubleConstantValueTag")) {
continue;
} else if (fieldType instanceof FloatType && interest.hasTag("FloatConstantValueTag")) {
continue;
} else if (fieldType instanceof LongType && interest.hasTag("LongConstantValueTag")) {
continue;
} else if (fieldType instanceof CharType && interest.hasTag("IntegerConstantValueTag")) {
continue;
} else if (fieldType instanceof BooleanType && interest.hasTag("IntegerConstantValueTag")) {
continue;
} else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType)
&& interest.hasTag("IntegerConstantValueTag")) {
continue;
} else if (interest.hasTag("StringConstantValueTag")) {
continue;
}
if (must.isMustInitialized(interest)) {
// was initialized on all paths couldnt ask for more
continue;
}
// System.out.println("SootField: "+interest+" not initialized.
// checking may analysis");
MustMayInitialize may = new MustMayInitialize(node, MustMayInitialize.MAY);
if (may.isMayInitialized(interest)) {
// System.out.println("It is initialized on some path just not
// all paths\n");
List defs = must.getDefs(interest);
if (defs == null) {
throw new RuntimeException("Sootfield: " + interest + " is mayInitialized but the defs is null");
}
handleAssignOnSomePaths(node, interest, defs);
} else {
// not initialized on any path., assign default
// System.out.println("Final field is not initialized on any
// path--------ASSIGN DEFAULT VALUE");
assignDefault(node, interest);
}
}
}
/*
* One gets to this method only if there was NO definition of a static final field in the static body At the same time no
* TAG with a constant value matched, so we know the static final was not initialized at declaration time If this happens:
* though it shouldnt unless u come from non-java compilers...insert default value initialization into the static
* method...right at the end to make things easy
*/
public void assignDefault(ASTMethodNode node, SootField f) {
// create initialization stmt
AugmentedStmt defaultStmt = createDefaultStmt(f);
if (defaultStmt == null) {
return;
}
List