1 24 25 package org.aspectj.compiler.crosscuts.ast; 26 27 import org.aspectj.compiler.crosscuts.joinpoints.TypeDecPlanner; 28 29 import org.aspectj.compiler.base.ast.*; 30 import org.aspectj.compiler.base.cst.*; 31 32 import org.aspectj.compiler.base.JavaCompiler; 33 import org.aspectj.compiler.crosscuts.*; 34 35 41 42 public class IntroducedSuperDec extends Dec implements TypeDecPlanner { 43 44 public Modifiers getModifiers() { return getAST().makeModifiers(0); } 45 46 49 public void walkScope(ScopeWalker walker) { 50 super.walkScope(walker); 51 getWorld().addTypeDecPlanner(this); 52 } 53 54 62 63 protected void addSuperType(Type toType, Type newSuperType) { 64 if (toType.isSubtypeOf(newSuperType)) return; 65 66 if (toType.isInterface()) { 67 addSuperTypeToInterfaceDec((InterfaceDec)toType.getTypeDec(), newSuperType); 68 } else if (toType.isClass()) { 69 addSuperTypeToClassDec((ClassDec)toType.getTypeDec(), newSuperType); 70 } else { 71 showWarning("can't apply to a " + toType.getTypeDec().getClass().getName()); 72 } 73 } 74 75 boolean checkExistingSubtypes(Type thisType, Type newSuperType) { 76 if (thisType.isSubtypeOf(newSuperType)) return true; 77 if (newSuperType.isSubtypeOf(thisType)) { 78 showError("circular inheritance: " + newSuperType.toShortString() + 79 " is a subtype of " + thisType.toShortString()); 80 return true; 81 } 82 return false; 83 } 84 85 protected void addSuperTypeToInterfaceDec(InterfaceDec typeDec, Type newSuperType) { 86 if (!newSuperType.isInterface()) { 87 showError("interface can't extend a class"); 88 return; 89 } 90 if (checkExistingSubtypes(typeDec.getType(), newSuperType)) return; 91 typeDec.addSuperInterfaceType(newSuperType); 92 typeDec.getType().addDirectSuperType(newSuperType); 93 } 94 95 protected void addSuperTypeToClassDec(ClassDec typeDec, Type newSuperType) { 96 if (checkExistingSubtypes(typeDec.getType(), newSuperType)) return; 97 98 if (newSuperType.isInterface()) { 99 typeDec.addSuperInterfaceType(newSuperType); 100 typeDec.getType().addDirectSuperType(newSuperType); 101 } else if (newSuperType.isClass()) { 102 Type oldParent = typeDec.getSuperClassType(); 103 if (oldParent == null) return; 104 105 if (newSuperType.isSubtypeOf(oldParent)) { 106 typeDec.setSuperClass(newSuperType.makeTypeD()); 107 typeDec.getType().addDirectSuperType(newSuperType); 108 } else { 109 showError("introduced superclass must extend old superclass: " + 110 oldParent.getPrettyString()); 111 } 112 } else { 113 showError("classes can't extend " + newSuperType.getTypeDec().getClass().getName()); 114 } 115 } 116 117 118 119 public void plan(TypeDec typeDec, int phase) { 120 if (phase != TypeDecPlanner.TYPE) return; 121 122 if (!getTargets().matches(typeDec.getType())) return; 123 124 final int N = typeDs.size(); 125 for (int i = 0; i < N; i++) { 126 TypeD name = typeDs.get(i); 127 getCompiler().showMessage(" introducing " + name.toShortString() + 128 " on " + typeDec.toShortString()); 129 130 addSuperType(typeDec.getType(), name.getType()); 131 } 132 ((AspectJCompiler)getCompiler()).getCorrespondences().addPointsTo(this, typeDec); 133 } 134 135 public String getKind() { 136 return "declare parents"; 137 } 138 139 public String getId() { 140 return targets.toShortString(); 141 } 142 143 public String toShortString() { 144 return "declare parents: " + targets.toShortString(); 145 } 146 147 protected GenTypeName targets; 149 public GenTypeName getTargets() { return targets; } 150 public void setTargets(GenTypeName _targets) { 151 if (_targets != null) _targets.setParent(this); 152 targets = _targets; 153 } 154 155 protected TypeDs typeDs; 156 public TypeDs getTypeDs() { return typeDs; } 157 public void setTypeDs(TypeDs _typeDs) { 158 if (_typeDs != null) _typeDs.setParent(this); 159 typeDs = _typeDs; 160 } 161 162 protected boolean isImplements; 163 public boolean getIsImplements() { return isImplements; } 164 public void setIsImplements(boolean _isImplements) { isImplements = _isImplements; } 165 166 public IntroducedSuperDec(SourceLocation location, GenTypeName _targets, TypeDs _typeDs, boolean _isImplements) { 167 super(location); 168 setTargets(_targets); 169 setTypeDs(_typeDs); 170 setIsImplements(_isImplements); 171 } 172 protected IntroducedSuperDec(SourceLocation source) { 173 super(source); 174 } 175 176 public ASTObject copyWalk(CopyWalker walker) { 177 IntroducedSuperDec ret = new IntroducedSuperDec(getSourceLocation()); 178 ret.preCopy(walker, this); 179 if (targets != null) ret.setTargets( (GenTypeName)walker.process(targets) ); 180 if (typeDs != null) ret.setTypeDs( (TypeDs)walker.process(typeDs) ); 181 ret.isImplements = isImplements; 182 return ret; 183 } 184 185 public ASTObject getChildAt(int childIndex) { 186 switch(childIndex) { 187 case 0: return targets; 188 case 1: return typeDs; 189 default: return super.getChildAt(childIndex); 190 } 191 } 192 public String getChildNameAt(int childIndex) { 193 switch(childIndex) { 194 case 0: return "targets"; 195 case 1: return "typeDs"; 196 default: return super.getChildNameAt(childIndex); 197 } 198 } 199 public void setChildAt(int childIndex, ASTObject child) { 200 switch(childIndex) { 201 case 0: setTargets((GenTypeName)child); return; 202 case 1: setTypeDs((TypeDs)child); return; 203 default: super.setChildAt(childIndex, child); return; 204 } 205 } 206 public int getChildCount() { 207 return 2; 208 } 209 210 public String getDefaultDisplayName() { 211 return "IntroducedSuperDec(isImplements: "+isImplements+")"; 212 } 213 214 } 216 217 218 219 | Popular Tags |