1 24 25 package org.aspectj.compiler.crosscuts.ast; 26 import org.aspectj.compiler.base.ast.*; 27 import org.aspectj.compiler.crosscuts.joinpoints.*; 28 29 import org.aspectj.compiler.base.JavaCompiler; 30 31 import java.util.*; 32 import org.aspectj.util.FuzzyBoolean; 33 34 41 public class MethodPattern extends CodePattern { 42 public String toShortString() { 43 String mods = modifiers.toShortString(); 45 if (mods.length() > 0) mods += " "; 46 mods += returnTypeName.toShortString()+" "; 47 String ret = mods + makeDeclaringTypeString() + id.toShortString() + formalsPattern.toShortString(); 48 49 if (throwsPattern != null) { 50 ret += " throws ..."; 51 } 52 53 return ret; 54 } 55 56 public void checkSpec() { 57 if (id.getSimpleName() != null && id.getSimpleName().equals("new")) { 58 this.showError("constructor patterns don't have a return type"); 59 } 60 } 61 62 public String getLookupId() { return id.getLookupString(); } 63 64 public FuzzyBoolean matches(JoinPoint jp) { 65 if (! (jp.getTargetSO() instanceof Method) ) { 66 return FuzzyBoolean.NO; 67 } 68 69 Method m = (Method)jp.getTargetSO(); 70 71 if (!id.matches(m.getName())) return FuzzyBoolean.NO; 72 if (!returnTypeName.matches(m.getReturnType())) return FuzzyBoolean.NO; 73 if (!formalsPattern.matches(m.getFormals())) return FuzzyBoolean.NO; 74 if (throwsPattern != null && !throwsPattern.matches(m.getThrows())) return FuzzyBoolean.NO; 75 76 return super.matches(jp); 77 } 78 79 protected GenTypeName returnTypeName; 81 public GenTypeName getReturnTypeName() { return returnTypeName; } 82 public void setReturnTypeName(GenTypeName _returnTypeName) { 83 if (_returnTypeName != null) _returnTypeName.setParent(this); 84 returnTypeName = _returnTypeName; 85 } 86 87 protected NamePattern id; 88 public NamePattern getId() { return id; } 89 public void setId(NamePattern _id) { id = _id; } 90 91 protected FormalsPattern formalsPattern; 92 public FormalsPattern getFormalsPattern() { return formalsPattern; } 93 public void setFormalsPattern(FormalsPattern _formalsPattern) { 94 if (_formalsPattern != null) _formalsPattern.setParent(this); 95 formalsPattern = _formalsPattern; 96 } 97 98 protected NameTypeDsPattern throwsPattern; 99 public NameTypeDsPattern getThrowsPattern() { return throwsPattern; } 100 public void setThrowsPattern(NameTypeDsPattern _throwsPattern) { 101 if (_throwsPattern != null) _throwsPattern.setParent(this); 102 throwsPattern = _throwsPattern; 103 } 104 105 public MethodPattern(SourceLocation location, Modifiers _modifiers, GenTypeName _declaringTypeName, GenTypeName _returnTypeName, NamePattern _id, FormalsPattern _formalsPattern, NameTypeDsPattern _throwsPattern) { 106 super(location, _modifiers, _declaringTypeName); 107 setReturnTypeName(_returnTypeName); 108 setId(_id); 109 setFormalsPattern(_formalsPattern); 110 setThrowsPattern(_throwsPattern); 111 } 112 protected MethodPattern(SourceLocation source) { 113 super(source); 114 } 115 116 public ASTObject copyWalk(CopyWalker walker) { 117 MethodPattern ret = new MethodPattern(getSourceLocation()); 118 ret.preCopy(walker, this); 119 if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) ); 120 if (declaringTypeName != null) ret.setDeclaringTypeName( (GenTypeName)walker.process(declaringTypeName) ); 121 if (returnTypeName != null) ret.setReturnTypeName( (GenTypeName)walker.process(returnTypeName) ); 122 ret.id = id; 123 if (formalsPattern != null) ret.setFormalsPattern( (FormalsPattern)walker.process(formalsPattern) ); 124 if (throwsPattern != null) ret.setThrowsPattern( (NameTypeDsPattern)walker.process(throwsPattern) ); 125 return ret; 126 } 127 128 public ASTObject getChildAt(int childIndex) { 129 switch(childIndex) { 130 case 2: return returnTypeName; 131 case 3: return formalsPattern; 132 case 4: return throwsPattern; 133 default: return super.getChildAt(childIndex); 134 } 135 } 136 public String getChildNameAt(int childIndex) { 137 switch(childIndex) { 138 case 2: return "returnTypeName"; 139 case 3: return "formalsPattern"; 140 case 4: return "throwsPattern"; 141 default: return super.getChildNameAt(childIndex); 142 } 143 } 144 public void setChildAt(int childIndex, ASTObject child) { 145 switch(childIndex) { 146 case 2: setReturnTypeName((GenTypeName)child); return; 147 case 3: setFormalsPattern((FormalsPattern)child); return; 148 case 4: setThrowsPattern((NameTypeDsPattern)child); return; 149 default: super.setChildAt(childIndex, child); return; 150 } 151 } 152 public int getChildCount() { 153 return 5; 154 } 155 156 public String getDefaultDisplayName() { 157 return "MethodPattern(id: "+id+")"; 158 } 159 160 } 162 163 | Popular Tags |