FileDocCategorySizeDatePackage
Generator.javaAPI DocGlassfish v2 API12422Fri May 04 22:32:58 BST 2007com.sun.ejb.codegen

Generator

public abstract class Generator extends Object
The base class for all code generators.

Fields Summary
private static final Logger
_logger
protected String
ejbClassSymbol
Constructors Summary
Methods Summary
public abstract voidgenerate(java.io.OutputStream out)

protected java.lang.StringgetBaseName(java.lang.String className)

        int dot = className.lastIndexOf('.");
        if (dot == -1)
            return className;
        return className.substring(dot+1);
    
public abstract java.lang.StringgetGeneratedClass()

protected java.lang.StringgetPackageName(java.lang.String className)
Get the package name from the class name.

param
the class name.
return
the package name.


       

        
          


                         
        
        int dot = className.lastIndexOf('.");
        if (dot == -1)
            return null;
        return className.substring(0, dot);
    
protected java.lang.StringgetSecurityAttribute(EjbDescriptor dd, java.lang.reflect.Method m)

	// The SEC_* strings returned MUST match the SEC_* constants in 
	// com.sun.ejb.Container.

	MethodDescriptor thisMethodDesc = new MethodDescriptor(m, ejbClassSymbol);

	Set unchecked = dd.getUncheckedMethodDescriptors();
	if ( unchecked != null ) {
	    Iterator i = unchecked.iterator();
	    while ( i.hasNext() ) {
		MethodDescriptor md = (MethodDescriptor)i.next();
		if ( thisMethodDesc.equals(md) )
		    return "SEC_UNCHECKED";
	    }
	}

	Set excluded = dd.getExcludedMethodDescriptors();
	if ( excluded != null ) {
	    Iterator i = excluded.iterator();
	    while ( i.hasNext() ) {
		MethodDescriptor md = (MethodDescriptor)i.next();
		if ( thisMethodDesc.equals(md) )
		    return "SEC_EXCLUDED";
	    }
	}

	return "SEC_CHECKED";
    
protected java.lang.StringgetTxAttribute(EjbDescriptor dd, java.lang.reflect.Method method)

	// The TX_* strings returned MUST match the TX_* constants in 
	// com.sun.ejb.Container.
        if ( dd instanceof EjbSessionDescriptor
	     && ((EjbSessionDescriptor)dd).getTransactionType().equals("Bean") )
	    return "TX_BEAN_MANAGED";

        String txAttr = null;
	MethodDescriptor mdesc = new MethodDescriptor(method, ejbClassSymbol);
        ContainerTransaction ct = dd.getContainerTransactionFor(mdesc);
        if ( ct != null ) {
            String attr = ct.getTransactionAttribute();
            if ( attr.equals(ContainerTransaction.NOT_SUPPORTED) )
                txAttr = "TX_NOT_SUPPORTED";
            else if ( attr.equals(ContainerTransaction.SUPPORTS) )
                txAttr = "TX_SUPPORTS";
            else if ( attr.equals(ContainerTransaction.REQUIRED) )
                txAttr = "TX_REQUIRED";
            else if ( attr.equals(ContainerTransaction.REQUIRES_NEW) )
                txAttr = "TX_REQUIRES_NEW";
            else if ( attr.equals(ContainerTransaction.MANDATORY) )
                txAttr = "TX_MANDATORY";
            else if ( attr.equals(ContainerTransaction.NEVER) )
                txAttr = "TX_NEVER";
        }

        if ( txAttr == null ) {
            throw new RuntimeException("Transaction Attribute not found for method "+method);
        }
	return txAttr;
    
protected java.lang.StringgetUniqueClassName(DeploymentContext context, java.lang.String origName, java.lang.String origSuffix, java.util.Vector existingClassNames)

	String newClassName = null;
	boolean foundUniqueName = false;
	int count = 0;
	while ( !foundUniqueName ) {
	    String suffix = origSuffix;
	    if ( count > 0 ) {
		suffix = origSuffix + count;
            }
	    newClassName = origName + suffix;
	    if ( !existingClassNames.contains(newClassName) ) {
		foundUniqueName = true;
                existingClassNames.add(newClassName);
            }
	    else {
		count++;
            }
	}
	return newClassName;
    
private java.lang.StringgetUniqueMethodVar(java.lang.String[] existingNames, java.lang.String newName, int count)

	// XXX clean this by using iteration instead of recursion
	boolean exists=false;
	String name = newName;
	if ( count > 0 ) 
	    name = newName + count;
	for ( int i=0; i<existingNames.length; i++ ) {
	    String existingName = existingNames[i];
	    if ( existingName != null && existingName.equals(name) ) {
		exists = true;
		break;
	    }
	}
	if ( exists ) {
	    count++;
	    // "foo1" exists, so try "foo2"
	    return getUniqueMethodVar(existingNames, newName, count);
	}
	return name;
    
protected booleanisEJBIntfMethod(java.lang.Class ejbIntfClz, java.lang.reflect.Method methodToCheck)
Return true if method is on a javax.ejb.EJBObject/EJBLocalObject/ javax.ejb.EJBHome,javax.ejb.EJBLocalHome interface.

        boolean isEJBIntfMethod = false;

        Method[] ejbIntfMethods = ejbIntfClz.getMethods();
        for(int i = 0; i < ejbIntfMethods.length; i++) {
            Method next = ejbIntfMethods[i];
            if(methodCompare(methodToCheck, next)) {
                isEJBIntfMethod = true;

                String ejbIntfClzName  = ejbIntfClz.getName();
                Class methodToCheckClz = methodToCheck.getDeclaringClass();
                if( !methodToCheckClz.getName().equals(ejbIntfClzName) ) {
                    String[] logParams = { next.toString(), 
                                           methodToCheck.toString() };
                    _logger.log(Level.WARNING, 
                                "ejb.illegal_ejb_interface_override",
                                logParams);
                }

                break;
            }
        }

	return isEJBIntfMethod;
    
private booleanmethodCompare(java.lang.reflect.Method factoryMethod, java.lang.reflect.Method homeMethod)


	if(! factoryMethod.getName().equals(homeMethod.getName())) {
	    return false;
        }

	Class[] factoryParamTypes = factoryMethod.getParameterTypes();
	Class[] beanParamTypes = homeMethod.getParameterTypes();
	if (factoryParamTypes.length != beanParamTypes.length) {
	    return false;
        }
	for(int i = 0; i < factoryParamTypes.length; i++) {
	    if (factoryParamTypes[i] != beanParamTypes[i]) {
		return false;
            }
        }

	// NOTE : Exceptions and return types are not part of equality check

	return true;

    
protected java.lang.String[]printStaticMethodInit(sun.rmi.rmic.IndentingWriter p, java.lang.Class genClass, java.lang.reflect.Method[] methods)

	if ( methods==null || methods.length == 0 )
	    return new String[0];
	String[] methodVars = new String[methods.length];
        // static variables for each biz method's Method object
        for(int i = 0; i < methods.length; i++) {
	    String methodVar = getUniqueMethodVar(methodVars, 
					"__Method__" + methods[i].getName(), 0);
	    methodVars[i] = methodVar;
            p.pln("private static java.lang.reflect.Method " + methodVar + ";");
        }

        // static block to initialize Method objects
        p.plnI("static {");
        p.plnI("try {");
        p.pln("java.lang.Class[] cl;");
        for(int i = 0; i < methods.length; i++) {
            Class[] params = methods[i].getParameterTypes();
            // code to create array of parameter types' Classes
            p.pln("cl = new java.lang.Class[" + params.length + "];");
            for(int j = 0; j < params.length; j++) {
                p.pln("cl[" + j + "] = " + printType(params[j]) + ".class;");
            }
            // Foo.class.getMethod(...)
            p.pln(methodVars[i] + " = "
                    + genClass.getName() + ".class.getMethod(\""
                    + methods[i].getName() + "\", cl);");
        }
        p.pOln("} catch(NoSuchMethodException e) {}");
        p.pOln("}");

	return methodVars;
    
protected java.lang.StringprintType(java.lang.Class cls)

        if (cls.isArray()) {
            return printType(cls.getComponentType()) + "[]";
        } else {
            return cls.getName();
        }
    
protected java.lang.reflect.Method[]removeDups(java.lang.reflect.Method[] orig)

        // Remove duplicates from method array.
        // Duplicates will arise if a class/intf and super-class/intf
        // define methods with the same signature. Potentially the
        // throws clauses of the methods may be different (note Java
        // requires that the superclass/intf method have a superset of the
        // exceptions in the derived method).
        Vector nodups = new Vector();
        for ( int i=0; i<orig.length; i++ ) {
            Method m1 = orig[i];
            boolean dup = false;
            for ( Enumeration e=nodups.elements(); e.hasMoreElements(); ) {
                Method m2 = (Method)e.nextElement();

                // m1 and m2 are duplicates if they have the same signature
                // (name and same parameters). 
                if ( !m1.getName().equals(m2.getName()) )
                    continue;

                Class[] m1parms = m1.getParameterTypes();
                Class[] m2parms = m2.getParameterTypes();
                if ( m1parms.length != m2parms.length )
                    continue;

                boolean parmsDup = true;
                for ( int j=0; j<m2parms.length; j++ ) {
                    if ( m1parms[j] != m2parms[j] ) {
                        parmsDup = false;
                        break;
                    }
                }
                if ( parmsDup ) {
                    dup = true;
                    // Select which of the duplicate methods to generate 
                    // code for: choose the one that is lower in the
                    // inheritance hierarchy: this ensures that the generated
                    // method will compile.
                    if ( m2.getDeclaringClass().isAssignableFrom(
                                                    m1.getDeclaringClass()) ) {
                        // m2 is a superclass/intf of m1, so replace m2 with m1
                        nodups.remove(m2);
                        nodups.add(m1);
                    }
                    break;
                }
            }

            if ( !dup )
                nodups.add(m1);
        }
        return (Method[])nodups.toArray(new Method[nodups.size()]);