use of org.candle.decompiler.intermediate.expression.Declaration in project candle-decompiler by bradsdavis.
the class ConstantArrayCompressor method extractNextDeclaration.
public Declaration extractNextDeclaration(StatementIntermediate statement) {
List<AbstractIntermediate> successors = Graphs.successorListOf(igc.getGraph(), statement);
if (successors.size() != 1) {
return null;
}
if (!(successors.get(0) instanceof StatementIntermediate)) {
return null;
}
StatementIntermediate si = (StatementIntermediate) successors.get(0);
if (si.getExpression() instanceof Declaration) {
return (Declaration) si.getExpression();
}
return null;
}
use of org.candle.decompiler.intermediate.expression.Declaration in project candle-decompiler by bradsdavis.
the class ArrayForToEnhancedFor method firstMatchesGeneratedVariables.
private boolean firstMatchesGeneratedVariables(StatementIntermediate first, GeneratedVariable generatedArrayRef, GeneratedVariable generatedArrayIterator) {
Declaration childDeclaration = (Declaration) first.getExpression();
Expression right = childDeclaration.getAssignment().getRightHandSide();
if (right instanceof ArrayAccess) {
ArrayAccess apr = (ArrayAccess) right;
if (!(apr.getIndex() instanceof Variable)) {
return false;
}
if (!(apr.getArray() instanceof Variable)) {
return false;
}
// cast both to variable. check the variables match the name and type of the ones found above.
Variable arrayPosition = (Variable) apr.getArray();
Variable arrayRef = (Variable) apr.getArray();
if (!StringUtils.equals(arrayPosition.getName(), generatedArrayIterator.getName())) {
return false;
}
if (!StringUtils.equals(arrayRef.getName(), generatedArrayRef.getName())) {
return false;
}
return true;
}
return true;
}
use of org.candle.decompiler.intermediate.expression.Declaration in project candle-decompiler by bradsdavis.
the class IntermediateTryCatch method generateCatch.
private void generateCatch(TryIntermediate tryIntermediate, CodeExceptionGen ceg) {
LOG.debug("CEG: " + ceg);
// convert the node to catch blocks...
AbstractIntermediate catchDeclaration = igc.getOrderedIntermediate().ceiling(new NullIntermediate(ceg.getHandlerPC()));
LOG.debug("Catch Declaration:" + catchDeclaration);
if (catchDeclaration instanceof StatementIntermediate) {
StatementIntermediate declarationStatement = (StatementIntermediate) catchDeclaration;
if (declarationStatement.getExpression() instanceof Declaration) {
Declaration declaration = (Declaration) declarationStatement.getExpression();
// now, we can convert this into a catch block.
CatchIntermediate catchIntermediate = new CatchIntermediate(declarationStatement.getInstruction(), ceg, declaration.getVariable());
igc.getGraph().addVertex(catchIntermediate);
// redirect statement to catch.
igc.redirectPredecessors(declarationStatement, catchIntermediate);
igc.redirectSuccessors(declarationStatement, catchIntermediate);
// now, we just need to remove the statement.
igc.getGraph().removeVertex(declarationStatement);
// populate the bounds..
// add the link between try and catch.
igc.getGraph().addEdge(tryIntermediate, catchIntermediate);
}
}
}
use of org.candle.decompiler.intermediate.expression.Declaration in project candle-decompiler by bradsdavis.
the class MethodIntermediateVisitor method visitStoreInstruction.
public void visitStoreInstruction(StoreInstruction instruction) {
Expression right = this.context.getExpressions().pop();
Type type = instruction.getType(context.getMethodGen().getConstantPool());
// first, check to see whether the variable currently has been declared.
// this would be the case if we don't get null when looking up the local variable.
int pc = context.getCurrentInstruction().getPosition();
int lvtIndex = instruction.getIndex();
IntermediateVariable iv = context.getVariableResolver().getLocalVariable(lvtIndex, pc);
// if the variable is not null, it is declared.
boolean declared = (iv != null);
Variable variable = null;
if (!declared) {
// look it up from the next phase code.
pc = this.context.getCurrentInstruction().getNext().getPosition();
iv = context.getVariableResolver().getLocalVariable(lvtIndex, pc);
if (iv == null) {
// probably need to create a variable for enhanced loops...
LOG.debug("Adding index: " + instruction.getIndex() + " as: " + type);
// try and resolve the type for the variable from the right hand side.
if (type == Type.OBJECT) {
if (right instanceof TypedExpression) {
type = ((TypedExpression) right).getType();
}
}
// generate variable name...
iv = context.getVariableResolver().addLocalVariable(instruction.getIndex(), context.getCurrentInstruction(), type);
variable = new GeneratedVariable(context.getCurrentInstruction(), iv.getType(), iv.getName());
}
}
// create the variable.
if (variable == null) {
variable = new Variable(context.getCurrentInstruction(), iv.getType(), iv.getName());
}
// create the assignment.
Assignment assignment = new Assignment(context.getCurrentInstruction(), variable, right);
Expression left = null;
if (declared) {
left = assignment;
} else {
// if it is currently not declared... create the declaration.
left = new Declaration(context.getCurrentInstruction(), variable, assignment);
}
StatementIntermediate cl = new StatementIntermediate(context.getCurrentInstruction(), left);
context.pushIntermediateToInstruction(cl);
LOG.debug("Stored.");
}
use of org.candle.decompiler.intermediate.expression.Declaration in project candle-decompiler by bradsdavis.
the class WhileToForLoopIterator method visitWhileIntermediate.
@Override
public void visitWhileIntermediate(WhileIntermediate line) {
if (line.getExpression() instanceof SingleConditional) {
if (((SingleConditional) line.getExpression()).getExpression() instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) ((SingleConditional) line.getExpression()).getExpression();
String iteratorName = null;
if (mi.getTarget() instanceof Variable) {
iteratorName = ((Variable) mi.getTarget()).getName();
} else {
return;
}
if (StringUtils.equals("hasNext", mi.getMethodName())) {
// probably an iterator.
List<AbstractIntermediate> predecessors = Graphs.predecessorListOf(igc.getGraph(), line);
// look at the predecessor lines; validate there are only 2 predecessors.
if (predecessors.size() == 2) {
StatementIntermediate declaration = null;
for (AbstractIntermediate predecessor : predecessors) {
if (comparator.before(predecessor, line)) {
if (predecessor instanceof StatementIntermediate) {
declaration = (StatementIntermediate) predecessor;
break;
}
}
}
// check to see if the declaration is a temporary variable..
if (declaration == null) {
return;
}
// otherwise, let's see if the declaration is an iterator.
if (declaration.getExpression() instanceof Declaration) {
Declaration declarationExpression = (Declaration) declaration.getExpression();
Variable v = (Variable) declarationExpression.getAssignment().getLeftHandSide();
// check to see if the declaration is the same as the iterator's name.
if (StringUtils.equals(iteratorName, v.getName())) {
LOG.debug("Identified Likely Iterator: " + v.getName());
// get the ".next()" statement, which should be the first child.
AbstractIntermediate firstChild = igc.getTrueTarget(line);
// then check the right side to see if it is an invocation.. and the invocation has the method name "next"...
if (firstChild instanceof StatementIntermediate) {
StatementIntermediate nextStatement = (StatementIntermediate) firstChild;
if (nextStatement.getExpression() instanceof Declaration) {
// the statement is indeed a declaration.
Declaration nextDeclaration = (Declaration) nextStatement.getExpression();
if (nextDeclaration.getAssignment().getRightHandSide() instanceof MethodInvocation) {
MethodInvocation nextMethodInvocation = (MethodInvocation) nextDeclaration.getAssignment().getRightHandSide();
if (StringUtils.equals("next", nextMethodInvocation.getMethodName())) {
// check to see if the next method is on the candidate iterator.
if (nextMethodInvocation.getTarget() instanceof Variable) {
Variable nextMethodTarget = (Variable) nextMethodInvocation.getTarget();
if (StringUtils.equals(iteratorName, nextMethodTarget.getName())) {
LOG.info("Definitely an enhanced for loop.");
if (declarationExpression.getAssignment().getRightHandSide() instanceof MethodInvocation) {
MethodInvocation iteratorInvocation = (MethodInvocation) declarationExpression.getAssignment().getRightHandSide();
if (StringUtils.equals("iterator", iteratorInvocation.getMethodName())) {
// now, we are pretty certain this is an enhanced for loop... we can chop up the graph.
EnhancedForIntermediate enhancedFor = new EnhancedForIntermediate(line, nextDeclaration.getVariable(), iteratorInvocation.getTarget());
igc.getGraph().addVertex(enhancedFor);
igc.redirectSuccessors(line, enhancedFor);
igc.redirectPredecessors(line, enhancedFor);
igc.getGraph().removeVertex(line);
igc.redirectPredecessors(declaration, igc.getSingleSuccessor(declaration));
igc.getGraph().removeVertex(declaration);
igc.redirectPredecessors(firstChild, igc.getSingleSuccessor(firstChild));
igc.getGraph().removeVertex(firstChild);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Aggregations