123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- /* *******************************************************************
- * Copyright (c) 2004 IBM Corporation.
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Common Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * ******************************************************************/
-
- package org.aspectj.weaver.patterns;
-
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Set;
-
- import org.aspectj.util.FuzzyBoolean;
- import org.aspectj.weaver.AnnotatedElement;
- import org.aspectj.weaver.ISourceContext;
- import org.aspectj.weaver.IntMap;
- import org.aspectj.weaver.Member;
- import org.aspectj.weaver.NameMangler;
- import org.aspectj.weaver.ResolvedMember;
- import org.aspectj.weaver.ResolvedTypeX;
- import org.aspectj.weaver.Shadow;
- import org.aspectj.weaver.ShadowMunger;
- import org.aspectj.weaver.TypeX;
- import org.aspectj.weaver.VersionedDataInputStream;
- import org.aspectj.weaver.ast.Literal;
- import org.aspectj.weaver.ast.Test;
- import org.aspectj.weaver.ast.Var;
-
- /**
- * @annotation(@Foo) or @annotation(foo)
- *
- * Matches any join point where the subject of the join point has an
- * annotation matching the annotationTypePattern:
- *
- * Join Point Kind Subject
- * ================================
- * method call the target method
- * method execution the method
- * constructor call the constructor
- * constructor execution the constructor
- * get the target field
- * set the target field
- * adviceexecution the advice
- * initialization the constructor
- * preinitialization the constructor
- * staticinitialization the type being initialized
- * handler the declared type of the handled exception
- */
- public class AnnotationPointcut extends NameBindingPointcut {
-
- private ExactAnnotationTypePattern annotationTypePattern;
- private ShadowMunger munger = null; // only set after concretization
-
- public AnnotationPointcut(ExactAnnotationTypePattern type) {
- super();
- this.annotationTypePattern = type;
- this.pointcutKind = Pointcut.ANNOTATION;
- }
-
- public AnnotationPointcut(ExactAnnotationTypePattern type, ShadowMunger munger) {
- this(type);
- this.munger = munger;
- }
-
- public Set couldMatchKinds() {
- return Shadow.ALL_SHADOW_KINDS;
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
- */
- public FuzzyBoolean fastMatch(FastMatchInfo info) {
- if (info.getKind() == Shadow.StaticInitialization) {
- return annotationTypePattern.fastMatches(info.getType());
- } else {
- return FuzzyBoolean.MAYBE;
- }
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
- */
- protected FuzzyBoolean matchInternal(Shadow shadow) {
- AnnotatedElement toMatchAgainst = null;
- Member member = shadow.getSignature();
- ResolvedMember rMember = member.resolve(shadow.getIWorld());
-
- if (rMember == null) {
- if (member.getName().startsWith(NameMangler.PREFIX)) {
- return FuzzyBoolean.NO;
- }
- shadow.getIWorld().getLint().unresolvableMember.signal(member.toString(), getSourceLocation());
- return FuzzyBoolean.NO;
- }
-
- Shadow.Kind kind = shadow.getKind();
- if (kind == Shadow.StaticInitialization) {
- toMatchAgainst = rMember.getType();
- } else if ( (kind == Shadow.ExceptionHandler)) {
- toMatchAgainst = TypeX.forName(rMember.getSignature()).resolve(shadow.getIWorld());
- } else {
- toMatchAgainst = rMember;
- }
-
- annotationTypePattern.resolve(shadow.getIWorld());
- return annotationTypePattern.matches(toMatchAgainst);
- }
-
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope, org.aspectj.weaver.patterns.Bindings)
- */
- protected void resolveBindings(IScope scope, Bindings bindings) {
- annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope,bindings,true);
- // must be either a Var, or an annotation type pattern
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.Pointcut#resolveBindingsFromRTTI()
- */
- protected void resolveBindingsFromRTTI() {
- // TODO Auto-generated method stub
-
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedTypeX, org.aspectj.weaver.IntMap)
- */
- protected Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
- ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
- Pointcut ret = new AnnotationPointcut(newType, bindings.getEnclosingAdvice());
- ret.copyLocationFrom(this);
- return ret;
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
- */
- protected Test findResidueInternal(Shadow shadow, ExposedState state) {
-
- if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
- BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern)annotationTypePattern;
- TypeX annotationType = btp.annotationType;
- Var var = shadow.getKindedAnnotationVar(annotationType);
- if (var == null) return Literal.FALSE;
- // Check if we have already bound something to this formal
- if ((state.get(btp.getFormalIndex())!=null) &&(lastMatchedShadowId == shadow.shadowId)) {
- // ISourceLocation pcdSloc = getSourceLocation();
- // ISourceLocation shadowSloc = shadow.getSourceLocation();
- // Message errorMessage = new Message(
- // "Cannot use @pointcut to match at this location and bind a formal to type '"+var.getType()+
- // "' - the formal is already bound to type '"+state.get(btp.getFormalIndex()).getType()+"'"+
- // ". The secondary source location points to the problematic binding.",
- // shadowSloc,true,new ISourceLocation[]{pcdSloc});
- // shadow.getIWorld().getMessageHandler().handleMessage(errorMessage);
- state.setErroneousVar(btp.getFormalIndex());
- }
- state.set(btp.getFormalIndex(),var);
- }
- return Literal.TRUE;
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
- */
- public List getBindingAnnotationTypePatterns() {
- if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
- List l = new ArrayList();
- l.add(annotationTypePattern);
- return l;
- } else return Collections.EMPTY_LIST;
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
- */
- public List getBindingTypePatterns() {
- return Collections.EMPTY_LIST;
- }
-
- /* (non-Javadoc)
- * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
- */
- public void write(DataOutputStream s) throws IOException {
- s.writeByte(Pointcut.ANNOTATION);
- annotationTypePattern.write(s);
- writeLocation(s);
- }
-
- public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
- AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
- AnnotationPointcut ret = new AnnotationPointcut((ExactAnnotationTypePattern)type);
- ret.readLocation(context, s);
- return ret;
- }
-
- public boolean equals(Object other) {
- if (!(other instanceof AnnotationPointcut)) return false;
- AnnotationPointcut o = (AnnotationPointcut)other;
- return o.annotationTypePattern.equals(this.annotationTypePattern);
- }
-
- public int hashCode() {
- int result = 17;
- result = 37*result + annotationTypePattern.hashCode();
- return result;
- }
-
- public String toString() {
- StringBuffer buf = new StringBuffer();
- buf.append("@annotation(");
- buf.append(annotationTypePattern.toString());
- buf.append(")");
- return buf.toString();
- }
-
- }
|