001    package org.mesopotamia.lang.java;
002    
003    import org.mesopotamia.MesopotamiaException;
004    import org.mesopotamia.NodeData;
005    import org.mesopotamia.RepositoryLanguage;
006    import org.mesopotamia.Scan;
007    import org.mesopotamia.lang.java.ref.MethodInfo;
008    import org.mesopotamia.lang.java.ref.TypeInfo;
009    import org.mesopotamia.lang.java.ref.TypeSpecInfo;
010    import org.w3c.dom.Element;
011    
012    import biz.hammurapi.util.Visitor;
013    
014    public class MethodDefinition extends Operation implements MethodInfo {
015    
016            public MethodDefinition(NodeData xData, Class<?> context, Scan scan,
017                            RepositoryLanguage language, Object environment) throws MesopotamiaException {
018                    super(xData, context, scan, language, environment);
019    
020                    // Select attributes
021                    Name = selectSingleElementText(Identifier.class, "IDENT|DOT");
022                    ReturnType = selectSingleElement(TypeSpecification.class, "TYPE");
023    
024            }
025    
026            public void toDom(Element holder) {
027                    super.toDom(holder);
028    
029                    // Serialize attributes
030                    setAttribute(holder, "Name", Name);
031            }
032    
033            // Attributes
034            private String Name;
035    
036            private TypeSpecification ReturnType;
037    
038            // Accessors
039            public String getName() {
040                    return Name;
041            }
042    
043            public TypeSpecification getReturnType() {
044                    return ReturnType;
045            }
046    
047            protected void acceptChildren(Visitor visitor) {
048                    super.acceptChildren(visitor);
049                    object2visitor(ReturnType, visitor);
050            }
051    
052            public String getFcn() {
053                    if (!fcnCalculated) {
054                            fcnCalculated = true;
055                            TypeInfo dt = getDeclaringType();
056                            if (dt==null) {
057                                    return null;
058                            }
059                            StringBuffer ret = new StringBuffer();
060                            TypeSpecification rt = getReturnType();
061                            if (rt==null) {
062                                    return null;
063                            }
064                            TypeInfo returnTypeInfo = rt.getTypeInfo();
065                            if (returnTypeInfo==null) {
066                                    return null;
067                            }
068                            ret.append(returnTypeInfo.getFcn());
069                            for (int i=0; i<rt.getDimensions(); ++i) {
070                                    ret.append(TypeSpecInfo.ARRAY_BRACKETS);
071                            }       
072                            ret.append(" ");
073                            ret.append(dt.getFcn());
074                            ret.append(".");
075                            ret.append(getName());
076                            ret.append("(");
077                            boolean first = true;
078                            for (TypeSpecInfo pd: getParameterTypes()) {
079                                    if (pd==null) {
080                                            return null;
081                                    }
082                                    if (first) {
083                                            first = false;
084                                    } else {
085                                            ret.append(", ");
086                                    }
087                                    TypeInfo pTypeInfo = pd.getTypeInfo();
088                                    if (pTypeInfo == null) {
089                                            return null;
090                                    }
091                                    ret.append(pTypeInfo.getFcn());
092                                    for (int i=0; i<pd.getDimensions(); ++i) {
093                                            ret.append(TypeSpecInfo.ARRAY_BRACKETS);
094                                    }
095                            }
096                            ret.append(")");
097                            first = true;
098                            for (TypeInfo et: getExceptionTypes()) {
099                                    if (et==null) {
100                                            return null;
101                                    }
102                                    if (first) {
103                                            ret.append(" throws ");
104                                            first = false;
105                                    } else {
106                                            ret.append(", ");
107                                    }
108                                    ret.append(et.getFcn());
109                            }
110                            fcn = ret.toString();                   
111                    }
112                    return fcn;
113            }
114    
115    }