Package org.codehaus.janino.IClass

Examples of org.codehaus.janino.IClass.IMethod


        // Check that all methods are implemented.
        if ((cd.getModifiers() & Mod.ABSTRACT) == 0) {
            IMethod[] ms = iClass.getIMethods();
            for (int i = 0; i < ms.length; ++i) {
                IMethod base = ms[i];
                if (base.isAbstract()) {
                    IMethod override = iClass.findIMethod(base.getName(), base.getParameterTypes());
                    if (
                        override == null           // It wasn't overridden
                        || override.isAbstract()   // It was overridden with an abstract method
                                                   // The override does not provide a covariant return type
                        || !base.getReturnType().isAssignableFrom(override.getReturnType())
                    ) {
                        this.compileError(
                            "Non-abstract class \"" + iClass + "\" must implement method \"" + base + "\"",
                            cd.getLocation()
                        );
                    }
                }
            }
        }

        // 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);
                    }
                }
            }
               
View Full Code Here


                    iMethod = null;
                    for (Iterator it = l.iterator(); it.hasNext();) {
                        Object o = it.next();
                        if (o instanceof IMethod) {
                            IClass declaringIClass = ((IMethod) o).getDeclaringIClass();
                            IMethod im = this.findIMethod(
                                declaringIClass, // targetType
                                mi               // invocable
                            );
                            if (im != null) {
                                if (iMethod != null && iMethod != im) {
                                    UnitCompiler.this.compileError(
                                        "Ambiguous static method import: \""
                                        + iMethod.toString()
                                        + "\" vs. \""
                                        + im.toString()
                                        + "\""
                                    );
                                }
                                iMethod = im;
                            }
                        }
                    }
                    if (iMethod != null) break FIND_METHOD;
                }
            }

            // Static method declared through static-import-on-demand?
            iMethod = null;
            for (Iterator it = this.staticImportsOnDemand.iterator(); it.hasNext();) {
                IClass iClass = (IClass) it.next();
                IMethod im = this.findIMethod(
                    iClass,         // targetType
                    mi              // invocable
                );
                if (im != null) {
                    if (iMethod != null) {
                        UnitCompiler.this.compileError(
                            "Ambiguous static method import: \""
                            + iMethod.toString()
                            + "\" vs. \""
                            + im.toString()
                            + "\""
                        );
                    }
                    iMethod = im;
                }
View Full Code Here

    }

    private IMethod fakeIMethod(IClass targetType, final String name, Rvalue[] arguments) throws CompileException {
        final IClass[] pts = new IClass[arguments.length];
        for (int i = 0; i < arguments.length; ++i) pts[i] = this.getType(arguments[i]);
        return targetType.new IMethod() {
            public String   getName()                                     { return name; }
            public IClass   getReturnType() throws CompileException       { return IClass.INT; }
            public boolean  isStatic()                                    { return false; }
            public boolean  isAbstract()                                  { return false; }
            public IClass[] getParameterTypes() throws CompileException   { return pts; }
View Full Code Here

                declaringClass = (Java.ClassDeclaration) s;
                break;
            }
        }
        IClass superclass = this.resolve(declaringClass).getSuperclass();
        IMethod iMethod = this.findIMethod(
            superclass,       // targetType
            scmi              // invocation
        );
        if (iMethod == null) {
            this.compileError(
View Full Code Here

                public Access   getAccess()           { return Access.PUBLIC; }
                public IClass[] getThrownExceptions() { return new IClass[0]; }
            };
        } else
        if (iInvocables[0] instanceof IClass.IMethod) {
            return iInvocables[0].getDeclaringIClass().new IMethod() {
                public boolean  isStatic()            { return true; }
                public boolean  isAbstract()          { return false; }
                public IClass   getReturnType()       { return IClass.INT; }
                public String   getName()             { return ((IClass.IMethod) iInvocables[0]).getName(); }
                public Access   getAccess()           { return Access.PUBLIC; }
View Full Code Here

            }

            // Return a "dummy" method.
            final IClass.IMethod im = (IClass.IMethod) maximallySpecificIInvocables.get(0);
            final IClass[] tes = (IClass[]) s.toArray(new IClass[s.size()]);
            return im.getDeclaringIClass().new IMethod() {
                public String   getName()                                   { return im.getName(); }
                public IClass   getReturnType() throws CompileException     { return im.getReturnType(); }
                public boolean  isAbstract()                                { return im.isAbstract(); }
                public boolean  isStatic()                                  { return im.isStatic(); }
                public Access   getAccess()                                 { return im.getAccess(); }
View Full Code Here

        return cd.iConstructor;
    }

    public IClass.IMethod toIMethod(final Java.MethodDeclarator md) {
        if (md.iMethod != null) return md.iMethod;
        md.iMethod = this.resolve((Java.AbstractTypeDeclaration) md.getDeclaringType()).new IMethod() {

            // Implement IMember.
            public Access getAccess() {
                switch (md.modifiers & Mod.PPP) {
                case Mod.PRIVATE:
View Full Code Here

TOP

Related Classes of org.codehaus.janino.IClass.IMethod

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.