001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.runtime.support.stubgen.model; 018 019 import java.util.Iterator; 020 import java.util.LinkedHashSet; 021 import java.util.Set; 022 023 /** 024 * Representation of a method definition. 025 * 026 * @version $Id: MethodDef.java 18 2009-07-16 09:39:40Z user57 $ 027 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 028 */ 029 public class MethodDef 030 extends Entity 031 implements ParametersAware, ThrowsAware 032 { 033 private final Type type; 034 035 private ClassDef parent; 036 037 private TypeDef returns; 038 039 private final Set parameters = new LinkedHashSet(); 040 041 private final Set throwz = new LinkedHashSet(); 042 043 public MethodDef() { 044 this(Type.METHOD); 045 } 046 047 protected MethodDef(final Type type) { 048 assert type != null; 049 050 this.type = type; 051 } 052 053 public Type getType() { 054 return type; 055 } 056 057 public boolean isConstructor() { 058 return type == Type.CTOR; 059 } 060 061 public ClassDef getParent() { 062 return parent; 063 } 064 065 public void setParent(final ClassDef parent) { 066 this.parent = parent; 067 } 068 069 public TypeDef getReturns() { 070 return returns; 071 } 072 073 public void setReturns(final TypeDef type) { 074 this.returns = type; 075 } 076 077 public void setReturns(final String type) { 078 assert type != null; 079 080 setReturns(new TypeDef(type)); 081 } 082 083 public void addParameter(final ParameterDef param) { 084 assert param != null; 085 086 param.setParent(this); 087 parameters.add(param); 088 } 089 090 public void addParameter(final TypeDef type, final String name) { 091 assert type != null; 092 assert name != null; 093 094 addParameter(new ParameterDef(type, name)); 095 } 096 097 public void addParameter(final String type, final String name) { 098 assert type != null; 099 assert name != null; 100 101 addParameter(new ParameterDef(type, name)); 102 } 103 104 public Set getParameters() { 105 return parameters; 106 } 107 108 public void addThrows(final TypeDef type) { 109 assert type != null; 110 111 throwz.add(type); 112 } 113 114 public Set getThrows() { 115 return throwz; 116 } 117 118 public Set getSuperParameters() { 119 throw new UnsupportedOperationException(); 120 } 121 122 /** @noinspection UnusedDeclaration*/ 123 public void setSuperParameters(final Set superParameters) { 124 throw new UnsupportedOperationException(); 125 } 126 127 public String signature() { 128 StringBuffer buff = new StringBuffer(); 129 130 buff.append(getName()); 131 buff.append("("); 132 133 Iterator iter = getParameters().iterator(); 134 135 while (iter.hasNext()) { 136 // FIXME: This does not take into account fully defined types, vs. partially defined, vs. aliased 137 138 ParameterDef param = (ParameterDef)iter.next(); 139 buff.append(param.getType()); 140 141 if (iter.hasNext()) { 142 buff.append(","); 143 } 144 } 145 146 buff.append(")"); 147 148 return buff.toString(); 149 } 150 151 // 152 // Type 153 // 154 155 /** 156 * Psuedo-enum for method type. 157 */ 158 public static final class Type 159 { 160 public static final String METHOD_NAME = "method"; 161 162 public static final int METHOD_CODE = 0; 163 164 public static final Type METHOD = new Type(METHOD_NAME, METHOD_CODE); 165 166 public static final String CTOR_NAME = "ctor"; 167 168 public static final int CTOR_CODE = 1; 169 170 public static final Type CTOR = new Type(CTOR_NAME, CTOR_CODE); 171 172 public final String name; 173 174 public final int code; 175 176 private Type(final String name, final int code) { 177 assert name != null; 178 179 this.name = name; 180 this.code = code; 181 } 182 183 public String toString() { 184 return name; 185 } 186 } 187 }