Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Method.java

00001 package edu.ksu.cis.bandera.jjjc.symboltable;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1999, 2000   Robby (robby@cis.ksu.edu)              *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
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  * @param name edu.ksu.cis.bandera.jjjc.symboltable.Name
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  * @param exceptionType edu.ksu.cis.bandera.jjjc.symboltable.ClassOrInterfaceType
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     // check later
00069     /*
00070     if (exceptionType.isInterface() || !exceptionType.hasSuperClass(new Name("java.lang.Throwable")))
00071         throw new NotThrowableException("Method '" + name.toString() + "' in class named '"
00072                     + declaringClassOrInterface + "' cannot have un-throwable exception type '" + exceptionType.toString() + "'");
00073     */
00074     exceptions.addElement(exceptionType);
00075 }
00076 /**
00077  * 
00078  * @param variable edu.ksu.cis.bandera.jjjc.symboltable.Variable
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  * @return boolean
00095  * @param modifiers int
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  * @return boolean
00114  * @param modifiers int
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  * @return boolean
00126  * @param modifiers int
00127  */
00128 public boolean areValidModifiers(int modifiers) {
00129     return areValidModifiers(modifiers, declaringClassOrInterface.isInterface());
00130 }
00131 /**
00132  * 
00133  * @return boolean
00134  * @param modifiers int
00135  * @param isInterface boolean
00136  */
00137 public static boolean areValidModifiers(int modifiers, boolean isInterface) {
00138     return (isInterface) ? areValidInterfaceMethodModifiers(modifiers) : areValidClassMethodModifiers(modifiers);
00139 }
00140 /**
00141  * 
00142  * @return edu.ksu.cis.bandera.jjjc.symboltable.ClassOrInterfaceType
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  * @return java.util.Enumeration
00152  */
00153 public Enumeration getExceptions() {
00154     return exceptions.elements();
00155 }
00156 /**
00157  * 
00158  * @return int
00159  */
00160 public int getModifiers() {
00161     return modifiers;
00162 }
00163 /**
00164  * 
00165  * @return edu.ksu.cis.bandera.jjjc.symboltable.Name
00166  */
00167 public Name getName() {
00168     return name;
00169 }
00170 /**
00171  * 
00172  * @return java.util.Vector
00173  */
00174 public Vector getParameters() {
00175     return parameters;
00176 }
00177 /**
00178  * 
00179  * @return java.util.Vector
00180  */
00181 public Vector getParameterTypes() {
00182     return parameterTypes;
00183 }
00184 /**
00185  * 
00186  * @return edu.ksu.cis.bandera.jjjc.symboltable.Type
00187  */
00188 public Type getReturnType() {
00189     return returnType;
00190 }
00191 /**
00192  * 
00193  * @return edu.ksu.cis.bandera.jjjc.symboltable.Type
00194  */
00195 public Type getType() {
00196     return returnType;
00197 }
00198 /**
00199  * 
00200  * @return boolean
00201  * @param otherMethod edu.ksu.cis.bandera.jjjc.symboltable.Method
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  * @return boolean
00212  * @param name edu.ksu.cis.bandera.jjjc.symboltable.Name
00213  * @param parameterTypes java.util.Vector
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  * @return boolean
00233  * @param otherType edu.ksu.cis.bandera.jjjc.symboltable.ClassOrInterfaceType
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  * @return boolean
00252  * @param argumentTypes java.util.Vector
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  * @return boolean
00268  * @param otherMethod edu.ksu.cis.bandera.jjjc.symboltable.Method
00269  */
00270 public boolean isMoreSpecific(Method otherMethod) {
00271     return declaringClassOrInterface.isValidMethodInvocationConversion(otherMethod.declaringClassOrInterface) &&
00272             otherMethod.isApplicable(parameterTypes);
00273 }
00274 /**
00275  * 
00276  * @param classOrInterface edu.ksu.cis.bandera.jjjc.symboltable.ClassOrInterfaceType
00277  */
00278 public void setDeclaringClassOrInterface(ClassOrInterfaceType classOrInterface) {
00279     declaringClassOrInterface = classOrInterface;
00280 }
00281 /**
00282  * 
00283  * @param modifiers int
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  * @param type edu.ksu.cis.bandera.jjjc.symboltable.Type
00295  */
00296 public void setReturnType(Type type) {
00297     returnType = type;
00298 }
00299 /**
00300  * 
00301  * @return java.lang.String
00302  */
00303 public String toString() {
00304     String parm = parameterTypes.toString();
00305     return name.toString() + "(" + parm.substring(1, parm.length() - 1) + ")";
00306 }
00307 }

Generated at Thu Feb 7 06:50:19 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001