00001 package edu.ksu.cis.bandera.jjjc.symboltable;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.lang.reflect.Modifier;
00036 import java.util.*;
00037 import edu.ksu.cis.bandera.jjjc.exception.*;
00038
00039 public class Method implements Named, Typed {
00040 private int modifiers = 0;
00041 private Type returnType = null;
00042 private Name name;
00043 private Vector parameters = new Vector();
00044 private Vector parameterTypes = new Vector();
00045 private Vector exceptions = new Vector();
00046 private ClassOrInterfaceType declaringClassOrInterface = null;
00047
00048
00049
00050
00051 public Method(Name name) throws InvalidNameException {
00052 if (!name.isSimpleName())
00053 throw new InvalidNameException("Cannot have a method named '" + name.toString() + "'");
00054 this.name = name;
00055 }
00056
00057
00058
00059
00060 public void addException(ClassOrInterfaceType exceptionType) throws AlreadyDeclaredException,
00061 ClassOrInterfaceTypeNotFoundException {
00062 for (int i = 0; i < exceptions.size(); i++) {
00063 if (exceptionType.getName().equals(((ClassOrInterfaceType) exceptions.elementAt(i)).getName()))
00064 throw new AlreadyDeclaredException("Method '" + name.toString() + "' in class named '"
00065 + declaringClassOrInterface + "' already has exception type '" + exceptionType.toString() + "'");
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 exceptions.addElement(exceptionType);
00075 }
00076
00077
00078
00079
00080 public void addParameterVariable(Variable variable) throws AlreadyDeclaredException {
00081 Name varName = variable.getName();
00082
00083 for (int i = 0; i < parameters.size(); i++) {
00084 if (varName.equals(((Variable) parameters.elementAt(i)).getName()))
00085 throw new AlreadyDeclaredException("Method '" + name.toString() + "' already has a parameter named '"
00086 + variable.getName().toString() + "'");
00087 }
00088
00089 parameters.addElement(variable);
00090 parameterTypes.addElement(variable.getType());
00091 }
00092
00093
00094
00095
00096
00097 public static boolean areValidClassMethodModifiers(int modifiers) {
00098 if (Modifier.isTransient(modifiers) || Modifier.isVolatile(modifiers))
00099 return false;
00100 else if (Modifier.isPublic(modifiers) && Modifier.isPrivate(modifiers))
00101 return false;
00102 else if (Modifier.isProtected(modifiers) && Modifier.isPrivate(modifiers))
00103 return false;
00104 else if (Modifier.isPublic(modifiers) && Modifier.isProtected(modifiers))
00105 return false;
00106 else if (Modifier.isAbstract(modifiers) && (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers)
00107 || Modifier.isNative(modifiers) || Modifier.isSynchronized(modifiers)))
00108 return false;
00109 else return true;
00110 }
00111
00112
00113
00114
00115
00116 public static boolean areValidInterfaceMethodModifiers(int modifiers) {
00117 if (Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)|| Modifier.isStatic(modifiers)
00118 || Modifier.isNative(modifiers) || Modifier.isSynchronized(modifiers) || Modifier.isTransient(modifiers)
00119 || Modifier.isVolatile(modifiers) || Modifier.isFinal(modifiers))
00120 return false;
00121 else return true;
00122 }
00123
00124
00125
00126
00127
00128 public boolean areValidModifiers(int modifiers) {
00129 return areValidModifiers(modifiers, declaringClassOrInterface.isInterface());
00130 }
00131
00132
00133
00134
00135
00136
00137 public static boolean areValidModifiers(int modifiers, boolean isInterface) {
00138 return (isInterface) ? areValidInterfaceMethodModifiers(modifiers) : areValidClassMethodModifiers(modifiers);
00139 }
00140
00141
00142
00143
00144 public ClassOrInterfaceType getDeclaringClassOrInterface() throws NotDeclaredException {
00145 if (declaringClassOrInterface == null)
00146 throw new NotDeclaredException("Method named '" + name.toString() + "' has not been declared yet.");
00147 else return declaringClassOrInterface;
00148 }
00149
00150
00151
00152
00153 public Enumeration getExceptions() {
00154 return exceptions.elements();
00155 }
00156
00157
00158
00159
00160 public int getModifiers() {
00161 return modifiers;
00162 }
00163
00164
00165
00166
00167 public Name getName() {
00168 return name;
00169 }
00170
00171
00172
00173
00174 public Vector getParameters() {
00175 return parameters;
00176 }
00177
00178
00179
00180
00181 public Vector getParameterTypes() {
00182 return parameterTypes;
00183 }
00184
00185
00186
00187
00188 public Type getReturnType() {
00189 return returnType;
00190 }
00191
00192
00193
00194
00195 public Type getType() {
00196 return returnType;
00197 }
00198
00199
00200
00201
00202
00203 public boolean hasSameSignature(Method otherMethod) {
00204 if (otherMethod == null)
00205 return false;
00206
00207 return hasSignature(otherMethod.name, otherMethod.parameterTypes);
00208 }
00209
00210
00211
00212
00213
00214
00215 public boolean hasSignature(Name name, Vector parameterTypes) {
00216 if ((name == null) || (parameterTypes == null))
00217 return false;
00218 else if (!(name.equals(name)))
00219 return false;
00220 else if (this.parameterTypes.size() != parameterTypes.size())
00221 return false;
00222
00223 for (int i = 0; i < this.parameterTypes.size(); i++) {
00224 if (!(this.parameterTypes.elementAt(i).equals(parameterTypes.elementAt(i))))
00225 return false;
00226 }
00227
00228 return true;
00229 }
00230
00231
00232
00233
00234
00235 public boolean isAccessible(ClassOrInterfaceType otherType) {
00236 if (declaringClassOrInterface == otherType)
00237 return true;
00238 else if (!declaringClassOrInterface.isAccesible(otherType))
00239 return false;
00240
00241 if (Modifier.isPublic(modifiers))
00242 return true;
00243 else if (Modifier.isPrivate(modifiers))
00244 return false;
00245 else if (Modifier.isProtected(modifiers) && otherType.hasSuperClass(declaringClassOrInterface))
00246 return true;
00247 else return declaringClassOrInterface.getContainingPackage() == otherType.getContainingPackage();
00248 }
00249
00250
00251
00252
00253
00254 public boolean isApplicable(Vector argumentTypes) {
00255 if (parameterTypes.size() != argumentTypes.size())
00256 return false;
00257
00258 for (int i = 0; i < parameterTypes.size(); i++) {
00259 if (!((Type) argumentTypes.elementAt(i)).isValidMethodInvocationConversion((Type) parameterTypes.elementAt(i)))
00260 return false;
00261 }
00262
00263 return true;
00264 }
00265
00266
00267
00268
00269
00270 public boolean isMoreSpecific(Method otherMethod) {
00271 return declaringClassOrInterface.isValidMethodInvocationConversion(otherMethod.declaringClassOrInterface) &&
00272 otherMethod.isApplicable(parameterTypes);
00273 }
00274
00275
00276
00277
00278 public void setDeclaringClassOrInterface(ClassOrInterfaceType classOrInterface) {
00279 declaringClassOrInterface = classOrInterface;
00280 }
00281
00282
00283
00284
00285 public void setModifiers(int modifiers) throws InvalidModifiersException {
00286 if (areValidModifiers(modifiers))
00287 this.modifiers = modifiers;
00288 else
00289 throw new InvalidModifiersException("Invalid modifiers for method named '" + name
00290 + "' in class or interface type named '" + declaringClassOrInterface.getName() + "'");
00291 }
00292
00293
00294
00295
00296 public void setReturnType(Type type) {
00297 returnType = type;
00298 }
00299
00300
00301
00302
00303 public String toString() {
00304 String parm = parameterTypes.toString();
00305 return name.toString() + "(" + parm.substring(1, parm.length() - 1) + ")";
00306 }
00307 }