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.*;
00036 import edu.ksu.cis.bandera.jjjc.exception.*;
00037
00038 public class Field implements Named, Typed {
00039 protected int modifiers = 0;
00040 private Type type;
00041 private Name name;
00042 private ClassOrInterfaceType declaringClassOrInterface = null;
00043 protected boolean isArrayLength = false;
00044 protected ArrayType arrayType = null;
00045
00046
00047
00048
00049
00050 public Field(Name name, Type type) throws InvalidNameException {
00051 if (!(name.isSimpleName()))
00052 throw new InvalidNameException("Cannot have a method named '" + name.toString() + "'");
00053
00054 this.name = name;
00055 this.type = type;
00056 }
00057
00058
00059
00060
00061
00062 public static boolean areValidClassFieldModifiers(int modifiers) {
00063 if (Modifier.isPublic(modifiers) && Modifier.isProtected(modifiers))
00064 return false;
00065 else if (Modifier.isPublic(modifiers) && Modifier.isPrivate(modifiers))
00066 return false;
00067 else if (Modifier.isProtected(modifiers) && Modifier.isPrivate(modifiers))
00068 return false;
00069 else return true;
00070 }
00071
00072
00073
00074
00075
00076 public static boolean areValidInterfaceFieldModifiers(int modifiers) {
00077 if (Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)|| Modifier.isAbstract(modifiers)
00078 || Modifier.isNative(modifiers) || Modifier.isSynchronized(modifiers) || Modifier.isTransient(modifiers)
00079 || Modifier.isVolatile(modifiers))
00080 return false;
00081 else return true;
00082 }
00083
00084
00085
00086
00087
00088 public boolean areValidModifiers(int modifiers) {
00089 return areValidModifiers(modifiers, declaringClassOrInterface.isInterface());
00090 }
00091
00092
00093
00094
00095
00096
00097 public static boolean areValidModifiers(int modifiers, boolean isInterface) {
00098 return (isInterface) ? areValidInterfaceFieldModifiers(modifiers) : areValidClassFieldModifiers(modifiers);
00099 }
00100
00101
00102
00103
00104 public ClassOrInterfaceType getDeclaringClassOrInterface() throws NotDeclaredException {
00105 if (declaringClassOrInterface == null)
00106 throw new NotDeclaredException("Field named '" + name.toString() + "' has not been declared yet.");
00107 else return declaringClassOrInterface;
00108 }
00109
00110
00111
00112
00113 public int getModifiers() {
00114 return modifiers;
00115 }
00116
00117
00118
00119
00120 public Name getName() {
00121 return name;
00122 }
00123
00124
00125
00126
00127 public Type getType() {
00128 return type;
00129 }
00130
00131
00132
00133
00134
00135 public boolean isAccessible(ClassOrInterfaceType otherType) {
00136 if (isArrayLength)
00137 return true;
00138
00139 if (declaringClassOrInterface == otherType)
00140 return true;
00141 else if (!declaringClassOrInterface.isAccesible(otherType))
00142 return false;
00143
00144 if (Modifier.isPublic(modifiers))
00145 return true;
00146 else if (Modifier.isPrivate(modifiers))
00147 return false;
00148 else if (Modifier.isProtected(modifiers) && otherType.hasSuperClass(declaringClassOrInterface))
00149 return true;
00150 else return declaringClassOrInterface.getContainingPackage() == otherType.getContainingPackage();
00151 }
00152
00153
00154
00155
00156 public void setDeclaringClassOrInterface(ClassOrInterfaceType declaringClassOrInterface) {
00157 this.declaringClassOrInterface = declaringClassOrInterface;
00158 }
00159
00160
00161
00162
00163 public void setModifiers(int modifiers) throws InvalidModifiersException {
00164 if (areValidModifiers(modifiers))
00165 this.modifiers = modifiers;
00166 else
00167 throw new InvalidModifiersException("Invalid modifiers for field named '" + name
00168 + "' in class or interface type named '" + declaringClassOrInterface.getName() + "'");
00169 }
00170
00171
00172
00173
00174 public String toString() {
00175 return name.toString();
00176 }
00177 }