Java Code Examples for org.benf.cfr.reader.entities.Method#getAnalysis()

The following examples show how to use org.benf.cfr.reader.entities.Method#getAnalysis() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecordRewriter.java    From cfr with MIT License 5 votes vote down vote up
private static StructuredStatement getSingleCodeLine(Method method) {
    if (method == null) return null;
    if (method.getCodeAttribute() == null) return null;
    Op04StructuredStatement code = method.getAnalysis();
    StructuredStatement topCode = code.getStatement();
    if (!(topCode instanceof Block)) return null;
    Block block = (Block)topCode;
    Optional<Op04StructuredStatement> content = block.getMaybeJustOneStatement();
    if (!content.isSet()) return null;
    return content.getValue().getStatement();
}
 
Example 2
Source File: RecordRewriter.java    From cfr with MIT License 5 votes vote down vote up
private static void hideConstructorIfEmpty(Method canonicalCons) {
    if (canonicalCons.getCodeAttribute() == null) return;
    Op04StructuredStatement code = canonicalCons.getAnalysis();
    if (code.getStatement().isEffectivelyNOP()) {
        canonicalCons.hideDead();
    }
}
 
Example 3
Source File: RecordRewriter.java    From cfr with MIT License 5 votes vote down vote up
private static void removeImplicitAssignments(Method canonicalCons, List<ClassFileField> instances, JavaRefTypeInstance thisType) {
    if (canonicalCons.getCodeAttribute() == null) return;
    Op04StructuredStatement code = canonicalCons.getAnalysis();

    instances = ListFactory.newList(instances);

    List<LocalVariable> args = canonicalCons.getMethodPrototype().getComputedParameters();
    // We expect a block.  The last N statements should be assignments
    StructuredStatement topCode = code.getStatement();
    if (!(topCode instanceof Block)) return;
    Block block = (Block)topCode;

    List<Op04StructuredStatement> statements = block.getBlockStatements();
    for (int x=statements.size()-1;x>=0;x--) {
        Op04StructuredStatement stm = statements.get(x);
        StructuredStatement statement = stm.getStatement();
        // this is very messy - refactor using wildcardmatch.
        if (statement.isEffectivelyNOP()) continue;
        if (!(statement instanceof StructuredAssignment)) return;
        LValue lhs = ((StructuredAssignment) statement).getLvalue();
        ClassFileField field = getCFF(lhs, thisType);
        if (field == null) return;
        int idx = instances.indexOf(field);
        if (idx == -1) return;
        instances.set(idx, null);

        Expression rhs = ((StructuredAssignment) statement).getRvalue();
        if (!(rhs instanceof LValueExpression)) return;
        LValue rlv = ((LValueExpression) rhs).getLValue();
        LocalVariable expected = args.get(idx);
        if (rlv != expected) return;
        stm.nopOut();
    }
}
 
Example 4
Source File: AnonymousClassConstructorRewriter.java    From cfr with MIT License 5 votes vote down vote up
@Override
public Expression rewriteExpression(Expression expression, SSAIdentifiers ssaIdentifiers, StatementContainer statementContainer, ExpressionRewriterFlags flags) {
    expression = super.rewriteExpression(expression, ssaIdentifiers, statementContainer, flags);
    if (expression instanceof ConstructorInvokationAnonymousInner) {
        ClassFile classFile = ((ConstructorInvokationAnonymousInner) expression).getClassFile();
        if (classFile != null) {
            for (Method constructor : classFile.getConstructors()) {
                Op04StructuredStatement analysis = constructor.getAnalysis();
                /*
                 * nop out initial super call, if present.
                 */
                if (!(analysis.getStatement() instanceof Block)) continue;
                Block block = (Block) analysis.getStatement();
                List<Op04StructuredStatement> statements = block.getBlockStatements();
                for (Op04StructuredStatement stmCont : statements) {
                    StructuredStatement stm = stmCont.getStatement();
                    if (stm instanceof StructuredComment) continue;
                    if (stm instanceof StructuredExpressionStatement) {
                        Expression e = ((StructuredExpressionStatement) stm).getExpression();
                        if (e instanceof SuperFunctionInvokation) {
                            stmCont.nopOut();
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }
    return expression;
}
 
Example 5
Source File: FakeMethodRewriter.java    From cfr with MIT License 5 votes vote down vote up
public static void rewrite(ClassFile classFile, TypeUsageCollectingDumper typeUsage) {
    ExpressionRewriterTransformer trans = new ExpressionRewriterTransformer(new Rewriter(classFile, typeUsage));
    for (Method method : classFile.getMethods()) {
        if (method.hasCodeAttribute()) {
            Op04StructuredStatement code = method.getAnalysis();
            trans.transform(code);
        }
    }
}
 
Example 6
Source File: DeadMethodRemover.java    From cfr with MIT License 5 votes vote down vote up
public static void removeDeadMethod(ClassFile classFile, Method method) {
    Op04StructuredStatement code = method.getAnalysis();
    StructuredStatement statement = code.getStatement();
    if (!(statement instanceof Block)) return;

    Block block = (Block) statement;
    for (Op04StructuredStatement inner : block.getBlockStatements()) {
        StructuredStatement innerStatement = inner.getStatement();
        if (!(innerStatement instanceof StructuredComment)) {
            return;
        }
    }
    method.hideDead();
}
 
Example 7
Source File: SyntheticAccessorRewriter.java    From cfr with MIT License 4 votes vote down vote up
private Expression rewriteFunctionExpression2(final StaticFunctionInvokation functionInvokation) {
        JavaTypeInstance tgtType = functionInvokation.getClazz();
        // Does tgtType have an inner relationship with this?
        if (!validRelationship(thisClassType, tgtType)) return null;

        ClassFile otherClass = state.getClassFile(tgtType);
        JavaTypeInstance otherType = otherClass.getClassType();
        MethodPrototype otherPrototype = functionInvokation.getFunction().getMethodPrototype();
        List<Expression> appliedArgs = functionInvokation.getArgs();

        /*
         * Look at the code for the referenced method.
         *
         * It will either be
         *
         *  * a static getter (0 args)
         *  * a static mutator (1 arg)
         *  * an instance getter (1 arg)
         *  * an instance mutator (2 args)
         *
         * Either way, the accessor method SHOULD be a static synthetic.
         */
        Method otherMethod;
        try {
            otherMethod = otherClass.getMethodByPrototype(otherPrototype);
        } catch (NoSuchMethodException e) {
            // Ignore and return.
            return null;
        }
        if (!otherMethod.testAccessFlag(AccessFlagMethod.ACC_STATIC)) return null;
        if (!otherMethod.testAccessFlag(AccessFlagMethod.ACC_SYNTHETIC)) return null;
//                getParameters(otherMethod.getConstructorFlag());

        /* Get the linearized, comment stripped code for the block. */
        if (!otherMethod.hasCodeAttribute()) return null;
        Op04StructuredStatement otherCode = otherMethod.getAnalysis();
        if (otherCode == null) return null;
        List<LocalVariable> methodArgs = otherMethod.getMethodPrototype().getComputedParameters();

        List<StructuredStatement> structuredStatements = MiscStatementTools.linearise(otherCode);

        Expression res = tryRewriteAccessor(structuredStatements, otherType, appliedArgs, methodArgs);
        if (res == null) {
            res = tryRewriteFunctionCall(structuredStatements, otherType, appliedArgs, methodArgs);
        }
        if (res != null) {
            otherMethod.hideSynthetic();
            return visbilityRewriter.rewriteExpression(res, null, null, null);
        }

        return null;
    }
 
Example 8
Source File: UnreachableStaticRewriter.java    From cfr with MIT License 4 votes vote down vote up
public static void rewrite(ClassFile classFile, TypeUsageCollectingDumper typeUsage) {
    // It is worth considering this, if we have made use of a class that also exists
    // as an inner class, AND as a 'synthetic full package' class.
    // (see https://github.com/leibnitz27/cfr/issues/102 )
    TypeUsageInformation info = typeUsage.getRealTypeUsageInformation();
    final JavaRefTypeInstance thisType = classFile.getRefClassType();
    if (thisType == null) return;

    Pair<List<JavaRefTypeInstance>, List<JavaRefTypeInstance>> split = Functional.partition(info.getUsedClassTypes(), new Predicate<JavaRefTypeInstance>() {
        @Override
        public boolean test(JavaRefTypeInstance in) {
            return in.getInnerClassHereInfo().isTransitiveInnerClassOf(thisType);
        }
    });
    List<JavaRefTypeInstance> inners = split.getFirst();
    /*
     * We are interested in inner classes where we clash with the fqn.
     * (What monster would do that? See test).
     */
    Map<String, JavaRefTypeInstance> potentialClashes = MapFactory.newMap();
    for (JavaRefTypeInstance inner : inners) {
        StringBuilder sb = new StringBuilder();
        inner.getInnerClassHereInfo().getFullInnerPath(sb);
        sb.append(inner.getRawShortName());
        String name = sb.toString();
        potentialClashes.put(name, inner);
    }

    List<JavaRefTypeInstance> others = split.getSecond();

    Map<JavaTypeInstance, Inaccessible> inaccessibles = MapFactory.newMap();
    for (JavaRefTypeInstance type : others) {
        JavaRefTypeInstance clashFqn = potentialClashes.get(type.getRawName());
        JavaRefTypeInstance clashShort = potentialClashes.get(type.getRawShortName());
        // Strictly speaking, if EITHER of these is true we have a problem,
        // that's a todo.
        if (clashFqn == null || clashShort == null) continue;
        // Ok, we can't import this guy.  We could import static if it's just used as a method,
        // but even then, only if we don't collide with any *LOCAL* methods.
        inaccessibles.put(type, new Inaccessible(type, clashShort, clashFqn));
    }

    if (inaccessibles.isEmpty()) return;

    Rewriter usr = new Rewriter(thisType, typeUsage, inaccessibles);
    ExpressionRewriterTransformer trans = new ExpressionRewriterTransformer(usr);

    for (Method method : classFile.getMethods()) {
        if (method.hasCodeAttribute()) {
            Op04StructuredStatement code = method.getAnalysis();
            trans.transform(code);
        }
    }
}