Search in sources :

Example 1 with Variable

use of org.candle.decompiler.intermediate.expression.Variable 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;
}
Also used : ArrayAccess(org.candle.decompiler.intermediate.expression.ArrayAccess) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) Variable(org.candle.decompiler.intermediate.expression.Variable) Expression(org.candle.decompiler.intermediate.expression.Expression) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Example 2 with Variable

use of org.candle.decompiler.intermediate.expression.Variable in project candle-decompiler by bradsdavis.

the class MethodIntermediateVisitor method visitPUTSTATIC.

public void visitPUTSTATIC(PUTSTATIC instruction) {
    ConstantPoolGen cpg = context.getMethodGen().getConstantPool();
    String fieldName = instruction.getFieldName(cpg);
    Type fieldType = instruction.getFieldType(cpg);
    Expression right = context.getExpressions().pop();
    Variable variable = new Variable(context.getCurrentInstruction(), fieldType, fieldName);
    Assignment assignment = new Assignment(context.getCurrentInstruction(), variable, right);
    if (LOG.isDebugEnabled()) {
        for (Field field : context.getJavaClass().getFields()) {
            LOG.debug(field);
        }
    }
    StatementIntermediate complete = new StatementIntermediate(context.getCurrentInstruction(), assignment);
    context.pushIntermediateToInstruction(complete);
}
Also used : Assignment(org.candle.decompiler.intermediate.expression.Assignment) Field(org.apache.bcel.classfile.Field) OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) Variable(org.candle.decompiler.intermediate.expression.Variable) IntermediateVariable(org.candle.decompiler.intermediate.IntermediateVariable) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate)

Example 3 with Variable

use of org.candle.decompiler.intermediate.expression.Variable in project candle-decompiler by bradsdavis.

the class MethodIntermediateVisitor method visitLoadInstruction.

public void visitLoadInstruction(LoadInstruction instruction) {
    int index = instruction.getIndex();
    if (index >= 0) {
        IntermediateVariable localVar = context.getVariableResolver().getLocalVariable(index, context.getCurrentInstruction().getPosition());
        if (localVar == null) {
            LOG.debug("Did not find local variable: " + index + " for position: " + context.getCurrentInstruction().getPosition());
        }
        Variable variable = null;
        if (localVar == null) {
            //probably need to create a variable for enhanced loops...
            Type type = instruction.getType(context.getMethodGen().getConstantPool());
            localVar = context.getVariableResolver().addLocalVariable(index, context.getCurrentInstruction(), type);
            variable = new GeneratedVariable(context.getCurrentInstruction(), localVar.getType(), localVar.getName());
        } else {
            variable = new Variable(context.getCurrentInstruction(), localVar.getType(), localVar.getName());
        }
        //Variable variable = new Variable(context.getCurrentInstruction(), localVar.getType(), localVar.getName());
        context.getExpressions().push(variable);
        LOG.debug("Loaded: " + variable);
    } else {
        LOG.warn("Did not load because index: " + index);
    }
}
Also used : OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) IntermediateVariable(org.candle.decompiler.intermediate.IntermediateVariable) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) Variable(org.candle.decompiler.intermediate.expression.Variable) IntermediateVariable(org.candle.decompiler.intermediate.IntermediateVariable) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable)

Example 4 with Variable

use of org.candle.decompiler.intermediate.expression.Variable in project candle-decompiler by bradsdavis.

the class CatchBlock method write.

@Override
public void write(Writer builder) throws IOException {
    final String indent = buildIndent();
    builder.append(indent);
    builder.append("catch(");
    Variable catchVariable = intermediate.getCatchVariable();
    String type = SignatureUtility.signatureToString(catchVariable.getType().getSignature());
    builder.append(type);
    builder.append(" ");
    builder.append(catchVariable.getName());
    builder.append(") ");
    super.write(builder);
}
Also used : Variable(org.candle.decompiler.intermediate.expression.Variable)

Example 5 with Variable

use of org.candle.decompiler.intermediate.expression.Variable 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);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) Variable(org.candle.decompiler.intermediate.expression.Variable) EnhancedForIntermediate(org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) SingleConditional(org.candle.decompiler.intermediate.expression.SingleConditional) MethodInvocation(org.candle.decompiler.intermediate.expression.MethodInvocation) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Aggregations

Variable (org.candle.decompiler.intermediate.expression.Variable)8 GeneratedVariable (org.candle.decompiler.intermediate.expression.GeneratedVariable)6 IntermediateVariable (org.candle.decompiler.intermediate.IntermediateVariable)4 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)4 Declaration (org.candle.decompiler.intermediate.expression.Declaration)4 Expression (org.candle.decompiler.intermediate.expression.Expression)4 ArithmeticType (org.candle.decompiler.intermediate.expression.ArithmeticType)3 OperationType (org.candle.decompiler.intermediate.expression.OperationType)3 TypedExpression (org.candle.decompiler.intermediate.expression.TypedExpression)3 Assignment (org.candle.decompiler.intermediate.expression.Assignment)2 Field (org.apache.bcel.classfile.Field)1 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)1 EnhancedForIntermediate (org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate)1 ArrayAccess (org.candle.decompiler.intermediate.expression.ArrayAccess)1 Increment (org.candle.decompiler.intermediate.expression.Increment)1 MethodInvocation (org.candle.decompiler.intermediate.expression.MethodInvocation)1 SingleConditional (org.candle.decompiler.intermediate.expression.SingleConditional)1