Package org.codehaus.janino.util

Examples of org.codehaus.janino.util.ClassFile


            id.interfaces[i] = this.getType(id.extendedTypes[i]);
            interfaceDescriptors[i] = id.interfaces[i].getDescriptor();
        }

        // Create "ClassFile" object.
        ClassFile cf = new ClassFile(
            (short) (                         // accessFlags
                id.getModifiers() |
                Mod.SUPER |
                Mod.INTERFACE |
                Mod.ABSTRACT
            ),
            iClass.getDescriptor(),           // thisClassFD
            Descriptor.OBJECT,                // superClassFD
            interfaceDescriptors              // interfaceFDs
        );

        // Set "SourceFile" attribute.
        if (this.debugSource) {
            String sourceFileName;
            {
                String s = id.getLocation().getFileName();
                if (s != null) {
                    sourceFileName = new File(s).getName();
                } else {
                    sourceFileName = id.getName() + ".java";
                }
            }
            cf.addSourceFileAttribute(sourceFileName);
        }

        // Add "Deprecated" attribute (JVMS 4.7.10)
        if (id.hasDeprecatedDocTag()) cf.addDeprecatedAttribute();

        // Interface initialization method.
        if (!id.constantDeclarations.isEmpty()) {
            List statements = new ArrayList(); // BlockStatements
            statements.addAll(id.constantDeclarations);
View Full Code Here


        });
    }

    /** Make the variable name and class name Constant Pool names used by local variables. */
    private void makeLocalVariableNames(final CodeContext cc, final ClassFile.MethodInfo mi) {
        ClassFile       cf = mi.getClassFile();
        Iterator        iter = cc.getAllLocalVars().iterator();

        cf.addConstantUtf8Info("LocalVariableTable");

        while (iter.hasNext()) {
            Java.LocalVariableSlot slot = (Java.LocalVariableSlot) iter.next();

            if (slot.getName() != null) {
                String typeName = slot.getType().getDescriptor();

                cf.addConstantUtf8Info(typeName);
                cf.addConstantUtf8Info(slot.getName());
            }
        }
    }
View Full Code Here

                }
            }
        }

        // Create "ClassFile" object.
        ClassFile cf = new ClassFile(
            (short) (cd.getModifiers() | Mod.SUPER),      // accessFlags
            iClass.getDescriptor(),                       // thisClassFD
            iClass.getSuperclass().getDescriptor(),       // superClassFD
            IClass.getDescriptors(iClass.getInterfaces()) // interfaceFDs
        );

        // Add InnerClasses attribute entry for this class declaration.
        if (cd.getEnclosingScope() instanceof Java.CompilationUnit) {
            ;
        } else
        if (cd.getEnclosingScope() instanceof Java.Block) {
            short innerClassInfoIndex = cf.addConstantClassInfo(iClass.getDescriptor());
            short innerNameIndex = (
                this instanceof Java.NamedTypeDeclaration ?
                cf.addConstantUtf8Info(((Java.NamedTypeDeclaration) this).getName()) :
                (short) 0
            );
            cf.addInnerClassesAttributeEntry(new ClassFile.InnerClassesAttribute.Entry(
                innerClassInfoIndex, // innerClassInfoIndex
                (short) 0,           // outerClassInfoIndex
                innerNameIndex,      // innerNameIndex
                cd.getModifiers()    // innerClassAccessFlags
            ));
        } else
        if (cd.getEnclosingScope() instanceof Java.TypeDeclaration) {
            short innerClassInfoIndex = cf.addConstantClassInfo(iClass.getDescriptor());
            short outerClassInfoIndex = cf.addConstantClassInfo(
                this.resolve(((Java.TypeDeclaration) cd.getEnclosingScope())).getDescriptor()
            );
            short innerNameIndex = cf.addConstantUtf8Info(((Java.MemberTypeDeclaration) cd).getName());
            cf.addInnerClassesAttributeEntry(new ClassFile.InnerClassesAttribute.Entry(
                innerClassInfoIndex, // innerClassInfoIndex
                outerClassInfoIndex, // outerClassInfoIndex
                innerNameIndex,      // innerNameIndex
                cd.getModifiers()    // innerClassAccessFlags
            ));
        }

        // Set "SourceFile" attribute.
        if (this.debugSource) {
            String sourceFileName;
            {
                String s = cd.getLocation().getFileName();
                if (s != null) {
                    sourceFileName = new File(s).getName();
                } else if (cd instanceof Java.NamedTypeDeclaration) {
                    sourceFileName = ((Java.NamedTypeDeclaration) cd).getName() + ".java";
                } else {
                    sourceFileName = "ANONYMOUS.java";
                }
            }
            cf.addSourceFileAttribute(sourceFileName);
        }

        // Add "Deprecated" attribute (JVMS 4.7.10)
        if (cd instanceof Java.DocCommentable) {
            if (((Java.DocCommentable) cd).hasDeprecatedDocTag()) cf.addDeprecatedAttribute();
        }

        // Optional: Generate and compile class initialization method.
        {
            List/*<BlockStatement>*/ statements = new ArrayList();
            for (Iterator it = cd.variableDeclaratorsAndInitializers.iterator(); it.hasNext();) {
                Java.TypeBodyDeclaration tbd = (Java.TypeBodyDeclaration) it.next();
                if (tbd.isStatic()) statements.add((Java.BlockStatement) tbd);
            }

            this.maybeCreateInitMethod(cd, cf, statements);
        }

        this.compileDeclaredMethods(cd, cf);

        // Compile declared constructors.
        // As a side effect of compiling methods and constructors, synthetic "class-dollar"
        // methods (which implement class literals) are generated on-the fly.
        // We need to note how many we have here so we can compile the extras.
        int declaredMethodCount = cd.getMethodDeclarations().size();
        {
            int syntheticFieldCount = cd.syntheticFields.size();
            Java.ConstructorDeclarator[] cds = cd.getConstructors();
            for (int i = 0; i < cds.length; ++i) {
                this.compile(cds[i], cf);
                if (syntheticFieldCount != cd.syntheticFields.size()) {
                    throw new JaninoRuntimeException(
                        "SNO: Compilation of constructor \""
                        + cds[i]
                        + "\" ("
                        + cds[i].getLocation()
                        + ") added synthetic fields!?"
                    );
                }
            }
        }

        // A side effect of this call may create synthetic functions to access
        // protected parent variables
        this.compileDeclaredMemberTypes(cd, cf);
       
        // Compile the aforementioned extras.
        this.compileDeclaredMethods(cd, cf, declaredMethodCount);
       
        {
            // for every method look for bridge methods that need to be supplied
            // this is used to correctly dispatch into covariant return types
            // from existing code
            IMethod[] ms = iClass.getIMethods();
            for (int i = 0; i < ms.length; ++i) {
                IMethod base = ms[i];
                if (! base.isStatic()) {
                    IMethod override = iClass.findIMethod(base.getName(), base.getParameterTypes());
                    // if we overrode the method but with a DIFFERENT return type
                    if (override != null &&
                        ! base.getReturnType().equals(override.getReturnType())) {
                        this.compileBridgeMethod(cf, base, override);
                    }
                }
            }
               
        }


        // Class and instance variables.
        for (Iterator it = cd.variableDeclaratorsAndInitializers.iterator(); it.hasNext();) {
            Java.TypeBodyDeclaration tbd = (Java.TypeBodyDeclaration) it.next();
            if (!(tbd instanceof Java.FieldDeclaration)) continue;
            this.addFields((Java.FieldDeclaration) tbd, cf);
        }

        // Synthetic fields.
        for (Iterator it = cd.syntheticFields.values().iterator(); it.hasNext();) {
            IClass.IField f = (IClass.IField) it.next();
            cf.addFieldInfo(
                Mod.PACKAGE,                 // modifiers,
                f.getName(),                 // fieldName,
                f.getType().getDescriptor(), // fieldTypeFD,
                null                         // optionalConstantValue
            );
View Full Code Here

         */
        private IClass defineIClassFromClassFileResource(Resource classFileResource) throws ClassNotFoundException {
            Compiler.this.benchmark.beginReporting("Loading class file \"" + classFileResource.getFileName() + "\"");
            try {
                InputStream is = null;
                ClassFile cf;
                try {
                    is = classFileResource.open();
                    cf = new ClassFile(new BufferedInputStream(is));
                } catch (IOException ex) {
                    throw new ClassNotFoundException("Opening class file resource \"" + classFileResource + "\"", ex);
                } finally {
                    if (is != null) try { is.close(); } catch (IOException e) { }
                }
View Full Code Here

    protected ClassFile.AttributeInfo storeLocalVariableTable(
            DataOutputStream dos,
            short localVariableTableAttributeNameIndex
    ) {
        ClassFile       cf = getClassFile();
        Iterator        iter = getAllLocalVars().iterator();
        final List      entryList = new ArrayList();

        while (iter.hasNext()) {
            Java.LocalVariableSlot slot = (Java.LocalVariableSlot) iter.next();

            if (slot.getName() != null) {
                String typeName = slot.getType().getDescriptor();
                short  classSlot = cf.addConstantUtf8Info(typeName);
                short  varNameSlot = cf.addConstantUtf8Info(slot.getName());

//                System.out.println("slot: " + slot + ", typeSlot: " + classSlot + ", varSlot: " + varNameSlot);

                ClassFile.LocalVariableTableAttribute.Entry entry = new ClassFile.LocalVariableTableAttribute.Entry(
                        (short) slot.getStart().offset,
View Full Code Here

        } catch (IOException ex) {
            throw new ClassNotFoundException("Opening resource \"" + classFileResource.getFileName() + "\"", ex);
        }

        // Load the IClass from the class file.
        ClassFile cf;
        try {
            cf = new ClassFile(is);
        } catch (IOException e) {
            throw new ClassNotFoundException("Reading resource \"" + classFileResource.getFileName() + "\"", e);
        } finally {
            try { is.close(); } catch (IOException e) { }
        }
View Full Code Here

TOP

Related Classes of org.codehaus.janino.util.ClassFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.