use of org.candle.decompiler.intermediate.expression.MethodInvocation in project candle-decompiler by bradsdavis.
the class MethodIntermediateVisitor method processInvoke.
public void processInvoke(InvokeInstruction instruction, int nparams) {
//collect all parameters from the stack.
final List<Expression> parameters = new ArrayList<Expression>(nparams);
for (int i = 0; i < nparams; i++) {
Expression param = context.getExpressions().pop();
LOG.debug("Parameter: " + param);
parameters.add(param);
}
//now, get the target that we are calling the method on.
Expression target = context.getExpressions().pop();
//collect the method name we are calling.
String methodName = instruction.getMethodName(context.getMethodGen().getConstantPool());
//create the expression..
MethodInvocation methodInvocation = new MethodInvocation(context.getCurrentInstruction(), target, methodName, parameters);
Type returned = instruction.getReturnType(context.getMethodGen().getConstantPool());
if (returned == BasicType.VOID) {
StatementIntermediate completeLine = new StatementIntermediate(context.getCurrentInstruction(), methodInvocation);
context.pushIntermediateToInstruction(completeLine);
LOG.debug("Pushed complete line: " + completeLine.toString());
} else {
context.getExpressions().push(methodInvocation);
LOG.debug("Pushed expression: " + methodInvocation);
}
}
use of org.candle.decompiler.intermediate.expression.MethodInvocation 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);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
use of org.candle.decompiler.intermediate.expression.MethodInvocation in project candle-decompiler by bradsdavis.
the class MethodIntermediateVisitor method visitINVOKESTATIC.
public void visitINVOKESTATIC(INVOKESTATIC instruction) {
//collect all parameters from the stack.
ConstantPoolGen cpg = context.getMethodGen().getConstantPool();
Type[] types = instruction.getArgumentTypes(context.getMethodGen().getConstantPool());
final List<Expression> parameters = new ArrayList<Expression>(types.length);
for (int i = 0, j = types.length; i < j; i++) {
Expression param = context.getExpressions().pop();
LOG.debug("Parameter: " + param);
parameters.add(param);
}
Resolved resolvedType = new Resolved(context.getCurrentInstruction(), Type.CLASS, instruction.getLoadClassType(cpg).getClassName());
String methodName = instruction.getMethodName(cpg);
//create the expression..
MethodInvocation methodInvocation = new MethodInvocation(context.getCurrentInstruction(), resolvedType, methodName, parameters);
Type returned = instruction.getReturnType(context.getMethodGen().getConstantPool());
if (returned == BasicType.VOID) {
LOG.debug("This is a void return type!!");
StatementIntermediate completeLine = new StatementIntermediate(context.getCurrentInstruction(), methodInvocation);
context.pushIntermediateToInstruction(completeLine);
} else {
context.getExpressions().push(methodInvocation);
}
}
use of org.candle.decompiler.intermediate.expression.MethodInvocation in project candle-decompiler by bradsdavis.
the class MethodIntermediateVisitor method visitINVOKEINTERFACE.
public void visitINVOKEINTERFACE(INVOKEINTERFACE instruction) {
ConstantPoolGen cpg = context.getMethodGen().getConstantPool();
//collect all parameters from the stack.
Type[] types = instruction.getArgumentTypes(cpg);
final List<Expression> parameters = new ArrayList<Expression>(types.length);
for (int i = 0, j = types.length; i < j; i++) {
Expression param = context.getExpressions().pop();
LOG.debug("Parameter: " + param);
parameters.add(param);
}
//collect the method name we are calling.
String methodName = instruction.getMethodName(context.getMethodGen().getConstantPool());
Expression left = context.getExpressions().pop();
//create the expression..
MethodInvocation methodInvocation = new MethodInvocation(context.getCurrentInstruction(), left, methodName, parameters);
Type returned = instruction.getReturnType(context.getMethodGen().getConstantPool());
if (returned == BasicType.VOID) {
StatementIntermediate completeLine = new StatementIntermediate(context.getCurrentInstruction(), methodInvocation);
context.pushIntermediateToInstruction(completeLine);
LOG.debug("Pushed complete line: " + completeLine.toString());
} else {
context.getExpressions().push(methodInvocation);
LOG.debug("Pushed expression: " + methodInvocation);
}
}
use of org.candle.decompiler.intermediate.expression.MethodInvocation in project candle-decompiler by bradsdavis.
the class MethodIntermediateVisitor method visitINVOKESPECIAL.
public void visitINVOKESPECIAL(INVOKESPECIAL instruction) {
LOG.debug("Invoking special.");
Type[] types = instruction.getArgumentTypes(context.getMethodGen().getConstantPool());
final List<Expression> parameters = new ArrayList<Expression>(types.length);
for (int i = 0, j = types.length; i < j; i++) {
Expression param = context.getExpressions().pop();
LOG.debug("Parameter: " + param);
parameters.add(param);
}
//now, get the target that we are calling the method on.
Expression target = context.getExpressions().pop();
//collect the method name we are calling.
String methodName = instruction.getMethodName(context.getMethodGen().getConstantPool());
MethodInvocation methodInvocation = null;
//create the expression..
if (StringUtils.equals(Constants.CONSTRUCTOR_NAME, methodName)) {
LOG.debug(target.getClass());
//TODO: Figure out why the dup / new causes issues.
if (target instanceof NewInstance) {
//get rid of this.
context.getExpressions().pop();
}
methodInvocation = new ConstructorInvocation(context.getCurrentInstruction(), target, methodName, parameters);
} else {
methodInvocation = new MethodInvocation(context.getCurrentInstruction(), target, methodName, parameters);
}
LOG.debug("Pushing: " + methodInvocation);
context.getExpressions().push(methodInvocation);
}
Aggregations