Java Code Examples for org.benf.cfr.reader.state.DCCommonState#getClassFile()

The following examples show how to use org.benf.cfr.reader.state.DCCommonState#getClassFile() . 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: ConstructorInvokationAnonymousInner.java    From cfr with MIT License 6 votes vote down vote up
public ConstructorInvokationAnonymousInner(MemberFunctionInvokation constructorInvokation,
                                           InferredJavaType inferredJavaType, List<Expression> args,
                                           DCCommonState dcCommonState, JavaTypeInstance anonymousTypeInstance) {
    super((inferredJavaType), constructorInvokation.getFunction(), args);
    this.constructorInvokation = constructorInvokation;
    this.anonymousTypeInstance = anonymousTypeInstance;
    /*
     * As much as I'd rather not tie this to its use, we have to make sure that the target variables etc
     * are available at the time of usage, so we can hide anonymous inner member clones.
     */
    ClassFile classFile = null;
    try {
        classFile = dcCommonState.getClassFile(
                constructorInvokation.getMethodPrototype().getReturnType().getDeGenerifiedType()
        );
    } catch (CannotLoadClassException e) {
        // Can't find class - live with it.
    }
    this.classFile = classFile;
}
 
Example 2
Source File: CFRDecompiler.java    From java-disassembler with GNU General Public License v3.0 4 votes vote down vote up
public static String doClass(DCCommonState dcCommonState, byte[] content1) {
    Options options = dcCommonState.getOptions();
    IllegalIdentifierDump illegalIdentifierDump = IllegalIdentifierDump.Factory.get(options);
    Dumper d = new ToStringDumper();

    try {
        StackTraceElement[] arr$;
        int len$;
        int i$;
        StackTraceElement x;
        try {
            SummaryDumper summaryDumper = new NopSummaryDumper();
            BaseByteData data = new BaseByteData(content1);
            ClassFile c = new ClassFile(data, "", dcCommonState);

            dcCommonState.configureWith(c);

            try {
                c = dcCommonState.getClassFile(c.getClassType());
            } catch (CannotLoadClassException var20) {
            }

            if (options.getOption(OptionsImpl.DECOMPILE_INNER_CLASSES)) {
                c.loadInnerClasses(dcCommonState);
            }

            if (options.getOption(OptionsImpl.RENAME_DUP_MEMBERS)) {
                MemberNameResolver.resolveNames(dcCommonState, ListFactory.newList(dcCommonState.getClassCache().getLoadedTypes()));
            }

            c.analyseTop(dcCommonState);
            TypeUsageCollector collectingDumper = new TypeUsageCollector(c);
            c.collectTypeUsages(collectingDumper);
            String methname = options.getOption(OptionsImpl.METHODNAME);
            if (methname == null) {
                c.dump(d);
            } else {
                try {
                    Iterator i$$ = c.getMethodByName(methname).iterator();

                    while(i$$.hasNext()) {
                        Method method = (Method)i$$.next();
                        method.dump(d, true);
                    }
                } catch (NoSuchMethodException var21) {
                    throw new IllegalArgumentException("No such method '" + methname + "'.");
                }
            }

            d.print("");
            return d.toString();
        } catch (ConfusedCFRException var22) {
            System.err.println(var22.toString());
            arr$ = var22.getStackTrace();
            len$ = arr$.length;

            for(i$ = 0; i$ < len$; ++i$) {
                x = arr$[i$];
                System.err.println(x);
            }
        } catch (CannotLoadClassException var23) {
            System.out.println("Can't load the class specified:");
            System.out.println(var23.toString());
        } catch (RuntimeException var24) {
            System.err.println(var24.toString());
            arr$ = var24.getStackTrace();
            len$ = arr$.length;

            for(i$ = 0; i$ < len$; ++i$) {
                x = arr$[i$];
                System.err.println(x);
            }
        }
    } finally {
        if (d != null) {
            d.close();
        }
    }
    return "";
}
 
Example 3
Source File: ClassFile.java    From cfr with MIT License 4 votes vote down vote up
public void loadInnerClasses(DCCommonState dcCommonState) {
        List<InnerClassAttributeInfo> innerClassAttributeInfoList = getInnerClassAttributeInfos(dcCommonState);
        if (innerClassAttributeInfoList == null) return;

        final JavaTypeInstance thisType = thisClass.getTypeInstance();

        for (InnerClassAttributeInfo innerClassAttributeInfo : innerClassAttributeInfoList) {
            JavaTypeInstance innerType = innerClassAttributeInfo.getInnerClassInfo();
            if (innerType == null) continue;

            if (innerType == thisType) {
                accessFlags.addAll(innerClassAttributeInfo.getAccessFlags());
                // But if it's J9+, we're crippled by https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8034044
                // which removes the static marker on anonymous classes.
                // Why am I checking j8+? BECAUSE java 9.0.4 SAYS J8. FFS.
                if (!accessFlags.contains(AccessFlag.ACC_STATIC)) {
                    if (isInferredAnonymousStatic(dcCommonState, thisType, innerType)) {
                        accessFlags.add(AccessFlag.ACC_STATIC);
                    }
                }
                // A public class can be marked as protected "as an inner class".
                sanitiseAccessPermissions();
            }

            /*
             * Inner classes can be referred to when they are not direct inner classes.
             * We even refer to inner classes which belong to entirely different classes!
             */
            if (!innerType.getInnerClassHereInfo().isInnerClassOf(thisType)) continue;

            /* If we're loading inner classes, then we definitely want to recursively apply that
             */
            try {
                ClassFile innerClass = dcCommonState.getClassFile(innerType);
                innerClass.loadInnerClasses(dcCommonState);
                // This is a fallback mechanism incase the access flags above aren't working - do we need it?
//                markInnerClassAsStatic(options, innerClass, thisType);

                innerClassesByTypeInfo.put(innerType, new Pair<InnerClassAttributeInfo, ClassFile>(innerClassAttributeInfo, innerClass));
            } catch (CannotLoadClassException ignore) {
            }

        }
    }
 
Example 4
Source File: Op02WithProcessedDataAndRefs.java    From cfr with MIT License 4 votes vote down vote up
private JavaTypeInstance determineDynamicGeneric(final JavaTypeInstance callsiteReturn, MethodPrototype proto, Expression instantiated, DCCommonState dcCommonState) {

        ClassFile classFile = null;
        try {
            classFile = dcCommonState.getClassFile(proto.getReturnType());
        } catch (CannotLoadClassException ignore) {
            // ignore.
        }
        if (classFile == null) return callsiteReturn;

        // Note - we need to examine the methods, but NOT look at their code.
        List<Method> methods = Functional.filter(classFile.getMethods(), new Predicate<Method>() {
            @Override
            public boolean test(Method in) {
                return !in.hasCodeAttribute();
            }
        });
        if (methods.size() != 1) return callsiteReturn;
        Method method = methods.get(0);
        MethodPrototype genericProto = method.getMethodPrototype();

        MethodPrototype boundProto = LambdaUtils.getLiteralProto(instantiated);
        GenericTypeBinder gtb = genericProto.getTypeBinderForTypes(boundProto.getArgs());

        JavaTypeInstance unboundReturn = genericProto.getReturnType();
        JavaTypeInstance boundReturn = boundProto.getReturnType();
        if (unboundReturn instanceof JavaGenericBaseInstance) {
            GenericTypeBinder gtb2 = GenericTypeBinder.extractBindings((JavaGenericBaseInstance) unboundReturn, boundReturn);
            gtb = gtb.mergeWith(gtb2, true);
        }

        JavaTypeInstance classType = classFile.getClassType();
        BindingSuperContainer b = classFile.getBindingSupers();
        classType = b.getBoundSuperForBase(classType);
        if (classType == null) return callsiteReturn;

        if (!callsiteReturn.getDeGenerifiedType().equals(classType.getDeGenerifiedType())) {
            // Something's gone wrong.
            return callsiteReturn;
        }

        JavaTypeInstance alternateCallSite = gtb.getBindingFor(classType);
        return alternateCallSite;
    }