12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427 |
- /*******************************************************************************
- * Copyright (c) 2005 Contributors.
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution and is available at
- * http://eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * initial implementation Alexandre Vasseur
- *******************************************************************************/
- package org.aspectj.weaver.bcel;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.List;
-
- import org.aspectj.apache.bcel.classfile.Attribute;
- import org.aspectj.apache.bcel.classfile.Field;
- import org.aspectj.apache.bcel.classfile.JavaClass;
- import org.aspectj.apache.bcel.classfile.LocalVariable;
- import org.aspectj.apache.bcel.classfile.LocalVariableTable;
- import org.aspectj.apache.bcel.classfile.Method;
- import org.aspectj.apache.bcel.classfile.annotation.Annotation;
- import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair;
- import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations;
- import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations;
- import org.aspectj.apache.bcel.generic.Type;
- import org.aspectj.bridge.IMessage;
- import org.aspectj.bridge.IMessageHandler;
- import org.aspectj.bridge.ISourceLocation;
- import org.aspectj.bridge.Message;
- import org.aspectj.bridge.SourceLocation;
- import org.aspectj.weaver.Advice;
- import org.aspectj.weaver.AdviceKind;
- import org.aspectj.weaver.AjAttribute;
- import org.aspectj.weaver.AjcMemberMaker;
- import org.aspectj.weaver.IHasPosition;
- import org.aspectj.weaver.ISourceContext;
- import org.aspectj.weaver.NameMangler;
- import org.aspectj.weaver.ResolvedPointcutDefinition;
- import org.aspectj.weaver.ResolvedTypeX;
- import org.aspectj.weaver.TypeX;
- import org.aspectj.weaver.patterns.AndPointcut;
- import org.aspectj.weaver.patterns.DeclareErrorOrWarning;
- import org.aspectj.weaver.patterns.DeclarePrecedence;
- import org.aspectj.weaver.patterns.FormalBinding;
- import org.aspectj.weaver.patterns.IScope;
- import org.aspectj.weaver.patterns.IdentityPointcutVisitor;
- import org.aspectj.weaver.patterns.IfPointcut;
- import org.aspectj.weaver.patterns.NotPointcut;
- import org.aspectj.weaver.patterns.OrPointcut;
- import org.aspectj.weaver.patterns.ParserException;
- import org.aspectj.weaver.patterns.PatternParser;
- import org.aspectj.weaver.patterns.PerCflow;
- import org.aspectj.weaver.patterns.PerClause;
- import org.aspectj.weaver.patterns.PerFromSuper;
- import org.aspectj.weaver.patterns.PerObject;
- import org.aspectj.weaver.patterns.PerSingleton;
- import org.aspectj.weaver.patterns.PerTypeWithin;
- import org.aspectj.weaver.patterns.Pointcut;
- import org.aspectj.weaver.patterns.SimpleScope;
- import org.aspectj.weaver.patterns.TypePattern;
-
- /**
- * Annotation defined aspect reader.
- * <p/>
- * It reads the Java 5 annotations and turns them into AjAttributes
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- public class AtAjAttributes {
-
- private final static List EMPTY_LIST = new ArrayList();
- private final static String[] EMPTY_STRINGS = new String[0];
- private final static String VALUE = "value";
- private final static String POINTCUT = "pointcut";
- private final static String THROWING = "throwing";
- private final static String RETURNING = "returning";
- private final static String STRING_DESC = "Ljava/lang/String;";
-
- /**
- * A struct that allows to add extra arguments without always breaking the API
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- private static class AjAttributeStruct {
-
- /**
- * The list of AjAttribute.XXX that we are populating from the @AJ read
- */
- List ajAttributes = new ArrayList();
-
- /**
- * The resolved type (class) for which we are reading @AJ for (be it class, method, field annotations)
- */
- final ResolvedTypeX enclosingType;
-
- final ISourceContext context;
- final IMessageHandler handler;
-
- public AjAttributeStruct(ResolvedTypeX type, ISourceContext sourceContext, IMessageHandler messageHandler) {
- enclosingType = type;
- context = sourceContext;
- handler = messageHandler;
- }
- }
-
- /**
- * A struct when we read @AJ on method
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- private static class AjAttributeMethodStruct extends AjAttributeStruct {
-
- /**
- * Argument names as they appear in the SOURCE code, ordered, and lazyly populated
- * Used to do formal binding
- */
- private String[] m_argumentNamesLazy = null;
-
- final Method method;
- final BcelMethod bMethod;
-
- public AjAttributeMethodStruct(Method method, BcelMethod bMethod, ResolvedTypeX type, ISourceContext sourceContext, IMessageHandler messageHandler) {
- super(type, sourceContext, messageHandler);
- this.method = method;
- this.bMethod = bMethod;
- }
-
- public String[] getArgumentNames() {
- if (m_argumentNamesLazy == null) {
- m_argumentNamesLazy = getMethodArgumentNamesAsInSource(method);
- }
- return m_argumentNamesLazy;
- }
- }
-
- /**
- * A struct when we read @AJ on field
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- private static class AjAttributeFieldStruct extends AjAttributeStruct {
-
- final Field field;
-
- public AjAttributeFieldStruct(Field field, ResolvedTypeX type, ISourceContext sourceContext, IMessageHandler messageHandler) {
- super(type, sourceContext, messageHandler);
- this.field = field;
- }
- }
-
- /**
- * Annotations are RuntimeVisible only. This allow us to not visit RuntimeInvisible ones.
- *
- * @param attribute
- * @return
- */
- public static boolean acceptAttribute(Attribute attribute) {
- return (attribute instanceof RuntimeVisibleAnnotations);
- }
-
- /**
- * Extract class level annotations and turn them into AjAttributes.
- *
- * @param javaClass
- * @param type
- * @param context
- * @param msgHandler
- * @return list of AjAttributes
- */
- public static List readAj5ClassAttributes(JavaClass javaClass, ResolvedTypeX type, ISourceContext context, IMessageHandler msgHandler, boolean isCodeStyleAspect) {
- AjAttributeStruct struct = new AjAttributeStruct(type, context, msgHandler);
- Attribute[] attributes = javaClass.getAttributes();
- boolean hasAtAspectAnnotation = false;
- boolean hasAtPrecedenceAnnotation = false;
-
- for (int i = 0; i < attributes.length; i++) {
- Attribute attribute = attributes[i];
- if (acceptAttribute(attribute)) {
- RuntimeAnnotations rvs = (RuntimeAnnotations) attribute;
- // we don't need to look for several attribute occurence since it cannot happen as per JSR175
- if (!isCodeStyleAspect) {
- hasAtAspectAnnotation = handleAspectAnnotation(rvs, struct);
- //TODO AV - if put outside the if isCodeStyleAspect then we would enable mix style
- hasAtPrecedenceAnnotation = handlePrecedenceAnnotation(rvs, struct);
- }
- // there can only be one RuntimeVisible bytecode attribute
- break;
- }
- }
-
- // basic semantic check
- if (hasAtPrecedenceAnnotation && !hasAtAspectAnnotation) {
- msgHandler.handleMessage(
- new Message(
- "Found @DeclarePrecedence on a non @Aspect type '" + type.getName() + "'",
- IMessage.WARNING,
- null,
- type.getSourceLocation()
- )
- );
- // bypass what we have read
- return EMPTY_LIST;
- }
- //FIXME turn on when ajcMightHaveAspect
- // if (hasAtAspectAnnotation && type.isInterface()) {
- // msgHandler.handleMessage(
- // new Message(
- // "Found @Aspect on an interface type '" + type.getName() + "'",
- // IMessage.WARNING,
- // null,
- // type.getSourceLocation()
- // )
- // );
- // // bypass what we have read
- // return EMPTY_LIST;
- // }
-
- // the following block will not detect @Pointcut in non @Aspect types for optimization purpose
- if (!hasAtAspectAnnotation) {
- return EMPTY_LIST;
- }
-
- // code style pointcuts are class attributes
- // we need to gather the @AJ pointcut right now and not at method level annotation extraction time
- // in order to be able to resolve the pointcut references later on
- // we don't need to look in super class, the pointcut reference in the grammar will do it
- for (int i = 0; i < javaClass.getMethods().length; i++) {
- Method method = javaClass.getMethods()[i];
- if (method.getName().startsWith(NameMangler.PREFIX)) continue; // already dealt with by ajc...
- //FIXME alex optimize, this method struct will gets recreated for advice extraction
- AjAttributeMethodStruct mstruct = new AjAttributeMethodStruct(method, null, type, context, msgHandler);//FIXME AVASM
- Attribute[] mattributes = method.getAttributes();
-
- for (int j = 0; j < mattributes.length; j++) {
- Attribute mattribute = mattributes[j];
- if (acceptAttribute(mattribute)) {
- RuntimeAnnotations mrvs = (RuntimeAnnotations) mattribute;
- handlePointcutAnnotation(mrvs, mstruct);
- // there can only be one RuntimeVisible bytecode attribute
- break;
- }
- }
- struct.ajAttributes.addAll(mstruct.ajAttributes);
- }
-
-
- // code style declare error / warning are class attributes
- for (int i = 0; i < javaClass.getFields().length; i++) {
- Field field = javaClass.getFields()[i];
- if (field.getName().startsWith(NameMangler.PREFIX)) continue; // already dealt with by ajc...
- //FIXME alex optimize, this method struct will gets recreated for advice extraction
- AjAttributeFieldStruct fstruct = new AjAttributeFieldStruct(field, type, context, msgHandler);
- Attribute[] fattributes = field.getAttributes();
-
- for (int j = 0; j < fattributes.length; j++) {
- Attribute fattribute = fattributes[j];
- if (acceptAttribute(fattribute)) {
- RuntimeAnnotations frvs = (RuntimeAnnotations) fattribute;
- if (handleDeclareErrorOrWarningAnnotation(frvs, fstruct)) {
- // semantic check - must be in an @Aspect [remove if previous block bypassed in advance]
- if (!type.isAnnotationStyleAspect()) {
- msgHandler.handleMessage(
- new Message(
- "Found @AspectJ annotations in a non @Aspect type '" + type.getName() + "'",
- IMessage.WARNING,
- null,
- type.getSourceLocation()
- )
- );
- ;// go ahead
- }
- }
- // there can only be one RuntimeVisible bytecode attribute
- break;
- }
- }
- struct.ajAttributes.addAll(fstruct.ajAttributes);
- }
- return struct.ajAttributes;
- }
-
- /**
- * Extract method level annotations and turn them into AjAttributes.
- *
- * @param method
- * @param type
- * @param context
- * @param msgHandler
- * @return list of AjAttributes
- */
- public static List readAj5MethodAttributes(Method method, BcelMethod bMethod, ResolvedTypeX type, ResolvedPointcutDefinition preResolvedPointcut, ISourceContext context, IMessageHandler msgHandler) {
- if (method.getName().startsWith(NameMangler.PREFIX)) return Collections.EMPTY_LIST; // already dealt with by ajc...
-
- AjAttributeMethodStruct struct = new AjAttributeMethodStruct(method, bMethod, type, context, msgHandler);
- Attribute[] attributes = method.getAttributes();
-
- // we remember if we found one @AJ annotation for minimal semantic error reporting
- // the real reporting beeing done thru AJDT and the compiler mapping @AJ to AjAtttribute
- // or thru APT
- //
- // Note: we could actually skip the whole thing if type is not itself an @Aspect
- // but then we would not see any warning. We do bypass for pointcut but not for advice since it would
- // be too silent.
- boolean hasAtAspectJAnnotation = false;
- boolean hasAtAspectJAnnotationMustReturnVoid = false;
- for (int i = 0; i < attributes.length; i++) {
- Attribute attribute = attributes[i];
- if (acceptAttribute(attribute)) {
- RuntimeAnnotations rvs = (RuntimeAnnotations) attribute;
- hasAtAspectJAnnotationMustReturnVoid = hasAtAspectJAnnotationMustReturnVoid || handleBeforeAnnotation(
- rvs, struct, preResolvedPointcut
- );
- hasAtAspectJAnnotationMustReturnVoid = hasAtAspectJAnnotationMustReturnVoid || handleAfterAnnotation(
- rvs, struct, preResolvedPointcut
- );
- hasAtAspectJAnnotationMustReturnVoid = hasAtAspectJAnnotationMustReturnVoid || handleAfterReturningAnnotation(
- rvs, struct, preResolvedPointcut
- );
- hasAtAspectJAnnotationMustReturnVoid = hasAtAspectJAnnotationMustReturnVoid || handleAfterThrowingAnnotation(
- rvs, struct, preResolvedPointcut
- );
- hasAtAspectJAnnotation = hasAtAspectJAnnotation || handleAroundAnnotation(
- rvs, struct, preResolvedPointcut
- );
- // there can only be one RuntimeVisible bytecode attribute
- break;
- }
- }
- hasAtAspectJAnnotation = hasAtAspectJAnnotation || hasAtAspectJAnnotationMustReturnVoid;
-
- // semantic check - must be in an @Aspect [remove if previous block bypassed in advance]
- if (hasAtAspectJAnnotation && !type.isAnnotationStyleAspect()) {
- msgHandler.handleMessage(
- new Message(
- "Found @AspectJ annotations in a non @Aspect type '" + type.getName() + "'",
- IMessage.WARNING,
- null,
- type.getSourceLocation()
- )
- );
- ;// go ahead
- }
- // semantic check - advice must be public
- if (hasAtAspectJAnnotation && !struct.method.isPublic()) {
- msgHandler.handleMessage(
- new Message(
- "Found @AspectJ annotation on a non public advice '" + methodToString(struct.method) + "'",
- IMessage.ERROR,
- null,
- type.getSourceLocation()
- )
- );
- ;// go ahead
- }
- // semantic check for non around advice must return void
- if (hasAtAspectJAnnotationMustReturnVoid && !Type.VOID.equals(struct.method.getReturnType())) {
- msgHandler.handleMessage(
- new Message(
- "Found @AspectJ annotation on a non around advice not returning void '" + methodToString(
- struct.method
- ) + "'",
- IMessage.ERROR,
- null,
- type.getSourceLocation()
- )
- );
- ;// go ahead
- }
-
-
- return struct.ajAttributes;
- }
-
- /**
- * Extract field level annotations and turn them into AjAttributes.
- *
- * @param field
- * @param type
- * @param context
- * @param msgHandler
- * @return list of AjAttributes, always empty for now
- */
- public static List readAj5FieldAttributes(Field field, ResolvedTypeX type, ISourceContext context, IMessageHandler msgHandler) {
- return Collections.EMPTY_LIST;
- }
-
- /**
- * Read @Aspect
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleAspectAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeStruct struct) {
- Annotation aspect = getAnnotation(runtimeAnnotations, AjcMemberMaker.ASPECT_ANNOTATION);
- if (aspect != null) {
- // semantic check for inheritance (only one level up)
- boolean extendsAspect = false;
- if (!"java.lang.Object".equals(struct.enclosingType.getSuperclass().getName())) {
- if (!struct.enclosingType.getSuperclass().isAbstract() && struct.enclosingType.getSuperclass().isAspect()) {
- reportError("cannot extend a concrete aspect", struct);
- return false;
- }
- extendsAspect = struct.enclosingType.getSuperclass().isAspect();
- }
-
- ElementNameValuePair aspectPerClause = getAnnotationElement(aspect, VALUE);
- final PerClause perClause;
- if (aspectPerClause == null) {
- // empty value means singleton unless inherited
- if (!extendsAspect) {
- perClause = new PerSingleton();
- } else {
- perClause = new PerFromSuper(struct.enclosingType.getSuperclass().getPerClause().getKind());
- }
- } else {
- String perX = aspectPerClause.getValue().stringifyValue();
- if (perX == null || perX.length() <= 0) {
- perClause = new PerSingleton();
- } else {
- perClause = parsePerClausePointcut(perX, struct);
- }
- }
- if (perClause == null) {
- // could not parse it, ignore the aspect
- return false;
- } else {
- perClause.setLocation(struct.context, struct.context.getOffset(), struct.context.getOffset()+1);//FIXME AVASM
- struct.ajAttributes.add(new AjAttribute.Aspect(perClause));
- return true;
- }
- }
- return false;
- }
-
- /**
- * Read a perClause, returns null on failure and issue messages
- *
- * @param perClauseString like "pertarget(.....)"
- * @param struct for which we are parsing the per clause
- * @return a PerClause instance
- */
- private static PerClause parsePerClausePointcut(String perClauseString, AjAttributeStruct struct) {
- final String pointcutString;
- Pointcut poincut = null;
- TypePattern typePattern = null;
- final PerClause perClause;
- if (perClauseString.startsWith(PerClause.KindAnnotationPrefix.PERCFLOW.getName())) {
- pointcutString = PerClause.KindAnnotationPrefix.PERCFLOW.extractPointcut(perClauseString);
- poincut = parsePointcut(pointcutString, struct, false);
- perClause = new PerCflow(poincut, false);
- } else if (perClauseString.startsWith(PerClause.KindAnnotationPrefix.PERCFLOWBELOW.getName())) {
- pointcutString = PerClause.KindAnnotationPrefix.PERCFLOWBELOW.extractPointcut(perClauseString);
- poincut = parsePointcut(pointcutString, struct, false);
- perClause = new PerCflow(poincut, true);
- } else if (perClauseString.startsWith(PerClause.KindAnnotationPrefix.PERTARGET.getName())) {
- pointcutString = PerClause.KindAnnotationPrefix.PERTARGET.extractPointcut(perClauseString);
- poincut = parsePointcut(pointcutString, struct, false);
- perClause = new PerObject(poincut, false);
- } else if (perClauseString.startsWith(PerClause.KindAnnotationPrefix.PERTHIS.getName())) {
- pointcutString = PerClause.KindAnnotationPrefix.PERTHIS.extractPointcut(perClauseString);
- poincut = parsePointcut(pointcutString, struct, false);
- perClause = new PerObject(poincut, true);
- } else if (perClauseString.startsWith(PerClause.KindAnnotationPrefix.PERTYPEWITHIN.getName())) {
- pointcutString = PerClause.KindAnnotationPrefix.PERTYPEWITHIN.extractPointcut(perClauseString);
- typePattern = parseTypePattern(pointcutString, struct);
- perClause = new PerTypeWithin(typePattern);
- } else if (perClauseString.equalsIgnoreCase(PerClause.SINGLETON.getName() + "()")) {
- perClause = new PerSingleton();
- } else {
- // could not parse the @AJ perclause - fallback to singleton and issue an error
- reportError("@Aspect per clause cannot be read '" + perClauseString + "'", struct);
- return null;
- }
-
- if (!PerClause.SINGLETON.equals(perClause.getKind())
- && !PerClause.PERTYPEWITHIN.equals(perClause.getKind())
- && poincut == null) {
- // we could not parse the pointcut
- return null;
- }
- if (PerClause.PERTYPEWITHIN.equals(perClause.getKind()) && typePattern == null) {
- // we could not parse the type pattern
- return null;
- }
- return perClause;
- }
-
- /**
- * Read @DeclarePrecedence
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handlePrecedenceAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeStruct struct) {
- Annotation aspect = getAnnotation(runtimeAnnotations, AjcMemberMaker.DECLAREPRECEDENCE_ANNOTATION);
- if (aspect != null) {
- ElementNameValuePair precedence = getAnnotationElement(aspect, VALUE);
- if (precedence != null) {
- String precedencePattern = precedence.getValue().stringifyValue();
- PatternParser parser = new PatternParser(precedencePattern);
- DeclarePrecedence ajPrecedence = parser.parseDominates();
- struct.ajAttributes.add(new AjAttribute.DeclareAttribute(ajPrecedence));
- return true;
- }
- }
- return false;
- }
-
- /**
- * Read @Before
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleBeforeAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeMethodStruct struct, ResolvedPointcutDefinition preResolvedPointcut) {
- Annotation before = getAnnotation(runtimeAnnotations, AjcMemberMaker.BEFORE_ANNOTATION);
- if (before != null) {
- ElementNameValuePair beforeAdvice = getAnnotationElement(before, VALUE);
- if (beforeAdvice != null) {
- // this/target/args binding
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- try {
- bindings = extractBindings(struct);
- } catch (UnreadableDebugInfoException unreadableDebugInfoException) {
- return false;
- }
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
-
- // joinpoint, staticJoinpoint binding
- int extraArgument = extractExtraArgument(struct.method);
-
- Pointcut pc = null;
- if (preResolvedPointcut != null) {
- pc = preResolvedPointcut.getPointcut();
- //pc.resolve(binding);
- } else {
- pc = parsePointcut(beforeAdvice.getValue().stringifyValue(), struct, false);
- if (pc == null) return false;//parse error
- pc.resolve(binding);
- }
- setIgnoreUnboundBindingNames(pc, bindings);
-
- ISourceLocation sl = struct.context.makeSourceLocation(struct.bMethod.getDeclarationLineNumber());
- struct.ajAttributes.add(
- new AjAttribute.AdviceAttribute(
- AdviceKind.Before,
- pc,
- extraArgument,
- sl.getOffset(),
- sl.getOffset()+1,//FIXME AVASM
- struct.context
- )
- );
- return true;
- }
- }
- return false;
- }
-
- /**
- * Read @After
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleAfterAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeMethodStruct struct, ResolvedPointcutDefinition preResolvedPointcut) {
- Annotation after = getAnnotation(runtimeAnnotations, AjcMemberMaker.AFTER_ANNOTATION);
- if (after != null) {
- ElementNameValuePair afterAdvice = getAnnotationElement(after, VALUE);
- if (afterAdvice != null) {
- // this/target/args binding
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- try {
- bindings = extractBindings(struct);
- } catch (UnreadableDebugInfoException unreadableDebugInfoException) {
- return false;
- }
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
-
- // joinpoint, staticJoinpoint binding
- int extraArgument = extractExtraArgument(struct.method);
-
- Pointcut pc = null;
- if (preResolvedPointcut != null) {
- pc = preResolvedPointcut.getPointcut();
- } else {
- pc = parsePointcut(afterAdvice.getValue().stringifyValue(), struct, false);
- if (pc == null) return false;//parse error
- pc.resolve(binding);
- }
- setIgnoreUnboundBindingNames(pc, bindings);
-
- ISourceLocation sl = struct.context.makeSourceLocation(struct.bMethod.getDeclarationLineNumber());
- struct.ajAttributes.add(
- new AjAttribute.AdviceAttribute(
- AdviceKind.After,
- pc,
- extraArgument,
- sl.getOffset(),
- sl.getOffset()+1,//FIXME AVASM
- struct.context
- )
- );
- return true;
- }
- }
- return false;
- }
-
- /**
- * Read @AfterReturning
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleAfterReturningAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeMethodStruct struct, ResolvedPointcutDefinition preResolvedPointcut) {
- Annotation after = getAnnotation(runtimeAnnotations, AjcMemberMaker.AFTERRETURNING_ANNOTATION);
- if (after != null) {
- ElementNameValuePair annValue = getAnnotationElement(after, VALUE);
- ElementNameValuePair annPointcut = getAnnotationElement(after, POINTCUT);
- ElementNameValuePair annReturned = getAnnotationElement(after, RETURNING);
-
- // extract the pointcut and returned type/binding - do some checks
- String pointcut = null;
- String returned = null;
- if ((annValue != null && annPointcut != null) || (annValue == null && annPointcut == null)) {
- reportError("@AfterReturning: either 'value' or 'poincut' must be provided, not both", struct);
- return false;
- }
- if (annValue != null) {
- pointcut = annValue.getValue().stringifyValue();
- } else {
- pointcut = annPointcut.getValue().stringifyValue();
- }
- if (isNullOrEmpty(pointcut)) {
- reportError("@AfterReturning: either 'value' or 'poincut' must be provided, not both", struct);
- return false;
- }
- if (annReturned != null) {
- returned = annReturned.getValue().stringifyValue();
- if (isNullOrEmpty(returned))
- returned = null;
- }
-
- // this/target/args binding
- // exclude the return binding from the pointcut binding since it is an extraArg binding
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- try {
- bindings = (returned == null ? extractBindings(struct) : extractBindings(struct, returned));
- } catch (UnreadableDebugInfoException unreadableDebugInfoException) {
- return false;
- }
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
-
- // joinpoint, staticJoinpoint binding
- int extraArgument = extractExtraArgument(struct.method);
-
- // return binding
- if (returned != null) {
- extraArgument |= Advice.ExtraArgument;
- }
-
- Pointcut pc = null;
- if (preResolvedPointcut != null) {
- pc = preResolvedPointcut.getPointcut();
- } else {
- pc = parsePointcut(pointcut, struct, false);
- if (pc == null) return false;//parse error
- pc.resolve(binding);
- }
- setIgnoreUnboundBindingNames(pc, bindings);
-
- ISourceLocation sl = struct.context.makeSourceLocation(struct.bMethod.getDeclarationLineNumber());
- struct.ajAttributes.add(
- new AjAttribute.AdviceAttribute(
- AdviceKind.AfterReturning,
- pc,
- extraArgument,
- sl.getOffset(),
- sl.getOffset()+1,//FIXME AVASM
- struct.context
- )
- );
- return true;
- }
- return false;
- }
-
- /**
- * Read @AfterThrowing
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleAfterThrowingAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeMethodStruct struct, ResolvedPointcutDefinition preResolvedPointcut) {
- Annotation after = getAnnotation(runtimeAnnotations, AjcMemberMaker.AFTERTHROWING_ANNOTATION);
- if (after != null) {
- ElementNameValuePair annValue = getAnnotationElement(after, VALUE);
- ElementNameValuePair annPointcut = getAnnotationElement(after, POINTCUT);
- ElementNameValuePair annThrowned = getAnnotationElement(after, THROWING);
-
- // extract the pointcut and throwned type/binding - do some checks
- String pointcut = null;
- String throwned = null;
- if ((annValue != null && annPointcut != null) || (annValue == null && annPointcut == null)) {
- reportError("@AfterThrowing: either 'value' or 'poincut' must be provided, not both", struct);
- return false;
- }
- if (annValue != null) {
- pointcut = annValue.getValue().stringifyValue();
- } else {
- pointcut = annPointcut.getValue().stringifyValue();
- }
- if (isNullOrEmpty(pointcut)) {
- reportError("@AfterThrowing: either 'value' or 'poincut' must be provided, not both", struct);
- return false;
- }
- if (annThrowned != null) {
- throwned = annThrowned.getValue().stringifyValue();
- if (isNullOrEmpty(throwned))
- throwned = null;
- }
-
- // this/target/args binding
- // exclude the throwned binding from the pointcut binding since it is an extraArg binding
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- try {
- bindings = (throwned == null ? extractBindings(struct) : extractBindings(struct, throwned));
- } catch (UnreadableDebugInfoException unreadableDebugInfoException) {
- return false;
- }
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
-
- // joinpoint, staticJoinpoint binding
- int extraArgument = extractExtraArgument(struct.method);
-
- // return binding
- if (throwned != null) {
- extraArgument |= Advice.ExtraArgument;
- }
-
- Pointcut pc = null;
- if (preResolvedPointcut != null) {
- pc = preResolvedPointcut.getPointcut();
- } else {
- pc = parsePointcut(pointcut, struct, false);
- if (pc == null) return false;//parse error
- pc.resolve(binding);
- }
- setIgnoreUnboundBindingNames(pc, bindings);
-
- ISourceLocation sl = struct.context.makeSourceLocation(struct.bMethod.getDeclarationLineNumber());
- struct.ajAttributes.add(
- new AjAttribute.AdviceAttribute(
- AdviceKind.AfterThrowing,
- pc,
- extraArgument,
- sl.getOffset(),
- sl.getOffset()+1,//FIXME AVASM
- struct.context
- )
- );
- return true;
- }
- return false;
- }
-
- /**
- * Read @Around
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleAroundAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeMethodStruct struct, ResolvedPointcutDefinition preResolvedPointcut) {
- Annotation around = getAnnotation(runtimeAnnotations, AjcMemberMaker.AROUND_ANNOTATION);
- if (around != null) {
- ElementNameValuePair aroundAdvice = getAnnotationElement(around, VALUE);
- if (aroundAdvice != null) {
- // this/target/args binding
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- try {
- bindings = extractBindings(struct);
- } catch (UnreadableDebugInfoException unreadableDebugInfoException) {
- return false;
- }
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
-
- // joinpoint, staticJoinpoint binding
- int extraArgument = extractExtraArgument(struct.method);
-
- Pointcut pc = null;
- if (preResolvedPointcut != null) {
- pc = preResolvedPointcut.getPointcut();
- } else {
- pc = parsePointcut(aroundAdvice.getValue().stringifyValue(), struct, false);
- if (pc == null) return false;//parse error
- pc.resolve(binding);
- }
- setIgnoreUnboundBindingNames(pc, bindings);
-
- ISourceLocation sl = struct.context.makeSourceLocation(struct.bMethod.getDeclarationLineNumber());
- struct.ajAttributes.add(
- new AjAttribute.AdviceAttribute(
- AdviceKind.Around,
- pc,
- extraArgument,
- sl.getOffset(),
- sl.getOffset()+1,//FIXME AVASM
- struct.context
- )
- );
- return true;
- }
- }
- return false;
- }
-
- /**
- * Read @Pointcut and handle the resolving in a lazy way to deal with pointcut references
- *
- * @param runtimeAnnotations
- * @param struct
- */
- private static void handlePointcutAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeMethodStruct struct) {
- Annotation pointcut = getAnnotation(runtimeAnnotations, AjcMemberMaker.POINTCUT_ANNOTATION);
- if (pointcut != null) {
- ElementNameValuePair pointcutExpr = getAnnotationElement(pointcut, VALUE);
- if (pointcutExpr != null) {
-
- // semantic check: the method must return void, or be "public static boolean" for if() support
- if (!(Type.VOID.equals(struct.method.getReturnType())
- || (Type.BOOLEAN.equals(struct.method.getReturnType()) && struct.method.isStatic() && struct.method.isPublic()))) {
- reportWarning("Found @Pointcut on a method not returning 'void' or not 'public static boolean'", struct);
- ;//no need to stop
- }
- // semantic check: the method must not throw anything
- if (struct.method.getExceptionTable() != null) {
- reportWarning("Found @Pointcut on a method throwing exception", struct);
- ;// no need to stop
- }
-
- // this/target/args binding
- final IScope binding;
- try {
- binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- extractBindings(struct)
- );
- } catch (UnreadableDebugInfoException e) {
- return;
- }
-
- TypeX[] argumentTypes = new TypeX[struct.method.getArgumentTypes().length];
- for (int i = 0; i < argumentTypes.length; i++) {
- argumentTypes[i] = TypeX.forSignature(struct.method.getArgumentTypes()[i].getSignature());
- }
-
- // use a LazyResolvedPointcutDefinition so that the pointcut is resolved lazily
- // since for it to be resolved, we will need other pointcuts to be registered as well
- Pointcut pc = parsePointcut(pointcutExpr.getValue().stringifyValue(), struct, true);
- if (pc == null) return;//parse error
- // do not resolve binding now but lazily
- pc.setLocation(struct.context, -1, -1);//FIXME AVASM !! bMethod is null here..
- struct.ajAttributes.add(
- new AjAttribute.PointcutDeclarationAttribute(
- new LazyResolvedPointcutDefinition(
- struct.enclosingType,
- struct.method.getModifiers(),
- struct.method.getName(),
- argumentTypes,
- TypeX.forSignature(struct.method.getReturnType().getSignature()),
- pc,
- binding
- )
- )
- );
- }
- }
- }
-
- /**
- * Read @DeclareError, @DeclareWarning
- *
- * @param runtimeAnnotations
- * @param struct
- * @return true if found
- */
- private static boolean handleDeclareErrorOrWarningAnnotation(RuntimeAnnotations runtimeAnnotations, AjAttributeFieldStruct struct) {
- Annotation error = getAnnotation(runtimeAnnotations, AjcMemberMaker.DECLAREERROR_ANNOTATION);
- boolean hasError = false;
- if (error != null) {
- ElementNameValuePair declareError = getAnnotationElement(error, VALUE);
- if (declareError != null) {
- if (!STRING_DESC.equals(struct.field.getSignature()) || struct.field.getConstantValue() == null) {
- reportError("@DeclareError used on a non String constant field", struct);
- return false;
- }
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
- Pointcut pc = parsePointcut(declareError.getValue().stringifyValue(), struct, false);
- if (pc == null) {
- hasError = false;//cannot parse pointcut
- } else {
- pc .resolve(binding);
- DeclareErrorOrWarning deow = new DeclareErrorOrWarning(true, pc, struct.field.getConstantValue().toString());
- deow.setLocation(struct.context, -1, -1);
- struct.ajAttributes.add(new AjAttribute.DeclareAttribute(deow));
- hasError = true;
- }
- }
- }
- Annotation warning = getAnnotation(runtimeAnnotations, AjcMemberMaker.DECLAREWARNING_ANNOTATION);
- boolean hasWarning = false;
- if (warning != null) {
- ElementNameValuePair declareWarning = getAnnotationElement(warning, VALUE);
- if (declareWarning != null) {
- if (!STRING_DESC.equals(struct.field.getSignature()) || struct.field.getConstantValue() == null) {
- reportError("@DeclareWarning used on a non String constant field", struct);
- return false;
- }
- FormalBinding[] bindings = new org.aspectj.weaver.patterns.FormalBinding[0];
- IScope binding = new BindingScope(
- struct.enclosingType,
- struct.context,
- bindings
- );
- Pointcut pc = parsePointcut(declareWarning.getValue().stringifyValue(), struct, false);
- if (pc == null) {
- hasWarning = false;//cannot parse pointcut
- } else {
- pc.resolve(binding);
- DeclareErrorOrWarning deow = new DeclareErrorOrWarning(false, pc, struct.field.getConstantValue().toString());
- deow.setLocation(struct.context, -1, -1);
- struct.ajAttributes.add(new AjAttribute.DeclareAttribute(deow));
- return hasWarning = true;
- }
- }
- }
- return hasError || hasWarning;
- }
-
- /**
- * Returns a readable representation of a method.
- * Method.toString() is not suitable.
- *
- * @param method
- * @return
- */
- private static String methodToString(Method method) {
- StringBuffer sb = new StringBuffer();
- sb.append(method.getName());
- sb.append(method.getSignature());
- return sb.toString();
- }
-
- /**
- * Returns a readable representation of a field.
- * Field.toString() is not suitable.
- *
- * @param field
- * @return
- */
- private static String fieldToString(Field field) {
- StringBuffer sb = new StringBuffer();
- sb.append(field.getName()).append(' ');
- sb.append(field.getSignature());
- return sb.toString();
- }
-
- /**
- * Build the bindings for a given method (pointcut / advice)
- *
- * @param struct
- * @return null if no debug info is available
- */
- private static FormalBinding[] extractBindings(AjAttributeMethodStruct struct)
- throws UnreadableDebugInfoException {
- Method method = struct.method;
- String[] argumentNames = struct.getArgumentNames();
-
- // assert debug info was here
- if (argumentNames.length != method.getArgumentTypes().length) {
- reportError("Cannot read debug info for @Aspect to handle formal binding in pointcuts (please compile with 'javac -g' or '<javac debug='true'.../>' in Ant)", struct);
- throw new UnreadableDebugInfoException();
- }
-
- List bindings = new ArrayList();
- for (int i = 0; i < argumentNames.length; i++) {
- String argumentName = argumentNames[i];
- TypeX argumentType = TypeX.forSignature(method.getArgumentTypes()[i].getSignature());
-
- // do not bind JoinPoint / StaticJoinPoint / EnclosingStaticJoinPoint
- // TODO solve me : this means that the JP/SJP/ESJP cannot appear as binding
- // f.e. when applying advice on advice etc
- if ((AjcMemberMaker.TYPEX_JOINPOINT.equals(argumentType)
- || AjcMemberMaker.TYPEX_PROCEEDINGJOINPOINT.equals(argumentType)
- || AjcMemberMaker.TYPEX_STATICJOINPOINT.equals(argumentType)
- || AjcMemberMaker.TYPEX_ENCLOSINGSTATICJOINPOINT.equals(argumentType)
- || AjcMemberMaker.AROUND_CLOSURE_TYPE.equals(argumentType))) {
- //continue;// skip
- bindings.add(new FormalBinding.ImplicitFormalBinding(argumentType, argumentName, i));
- } else {
- bindings.add(new FormalBinding(argumentType, argumentName, i));
- }
- }
-
- return (FormalBinding[]) bindings.toArray(new FormalBinding[]{});
- }
-
- //FIXME alex deal with exclude index
- private static FormalBinding[] extractBindings(AjAttributeMethodStruct struct, String excludeFormal)
- throws UnreadableDebugInfoException {
- FormalBinding[] bindings = extractBindings(struct);
- int excludeIndex = -1;
- for (int i = 0; i < bindings.length; i++) {
- FormalBinding binding = bindings[i];
- if (binding.getName().equals(excludeFormal)) {
- excludeIndex = i;
- bindings[i] = new FormalBinding.ImplicitFormalBinding(
- binding.getType(), binding.getName(), binding.getIndex()
- );
- break;
- }
- }
- return bindings;
- //
- // if (excludeIndex >= 0) {
- // FormalBinding[] bindingsFiltered = new FormalBinding[bindings.length-1];
- // int k = 0;
- // for (int i = 0; i < bindings.length; i++) {
- // if (i == excludeIndex) {
- // ;
- // } else {
- // bindingsFiltered[k] = new FormalBinding(bindings[i].getType(), bindings[i].getName(), k);
- // k++;
- // }
- // }
- // return bindingsFiltered;
- // } else {
- // return bindings;
- // }
- }
-
- /**
- * Compute the flag for the xxxJoinPoint extra argument
- *
- * @param method
- * @return
- */
- private static int extractExtraArgument(Method method) {
- Type[] methodArgs = method.getArgumentTypes();
- String[] sigs = new String[methodArgs.length];
- for (int i = 0; i < methodArgs.length; i++) {
- sigs[i] = methodArgs[i].getSignature();
- }
- return extractExtraArgument(sigs);
- }
-
- /**
- * Compute the flag for the xxxJoinPoint extra argument
- *
- * @param argumentSignatures
- * @return
- */
- public static int extractExtraArgument(String[] argumentSignatures) {
- int extraArgument = 0;
- for (int i = 0; i < argumentSignatures.length; i++) {
- if (AjcMemberMaker.TYPEX_JOINPOINT.getSignature().equals(argumentSignatures[i])) {
- extraArgument |= Advice.ThisJoinPoint;
- } else if (AjcMemberMaker.TYPEX_PROCEEDINGJOINPOINT.getSignature().equals(argumentSignatures[i])) {
- extraArgument |= Advice.ThisJoinPoint;
- } else if (AjcMemberMaker.TYPEX_STATICJOINPOINT.getSignature().equals(argumentSignatures[i])) {
- extraArgument |= Advice.ThisJoinPointStaticPart;
- } else if (AjcMemberMaker.TYPEX_ENCLOSINGSTATICJOINPOINT.getSignature().equals(argumentSignatures[i])) {
- extraArgument |= Advice.ThisEnclosingJoinPointStaticPart;
- }
- }
- return extraArgument;
- }
-
- /**
- * Returns the runtime (RV/RIV) annotation of type annotationType or null if no such annotation
- *
- * @param rvs
- * @param annotationType
- * @return
- */
- private static Annotation getAnnotation(RuntimeAnnotations rvs, TypeX annotationType) {
- final String annotationTypeName = annotationType.getName();
- for (Iterator iterator = rvs.getAnnotations().iterator(); iterator.hasNext();) {
- Annotation rv = (Annotation) iterator.next();
- if (annotationTypeName.equals(rv.getTypeName())) {
- return rv;
- }
- }
- return null;
- }
-
- /**
- * Returns the value of a given element of an annotation or null if not found
- * Caution: Does not handles default value.
- *
- * @param annotation
- * @param elementName
- * @return
- */
- private static ElementNameValuePair getAnnotationElement(Annotation annotation, String elementName) {
- for (Iterator iterator1 = annotation.getValues().iterator(); iterator1.hasNext();) {
- ElementNameValuePair element = (ElementNameValuePair) iterator1.next();
- if (elementName.equals(element.getNameString())) {
- return element;
- }
- }
- return null;
- }
-
- /**
- * Extract the method argument names as in source from debug info
- * returns an empty array upon inconsistency
- *
- * @param method
- * @return
- */
- private static String[] getMethodArgumentNamesAsInSource(Method method) {
- if (method.getArgumentTypes().length == 0) {
- return EMPTY_STRINGS;
- }
-
- final int startAtStackIndex = method.isStatic() ? 0 : 1;
- final List arguments = new ArrayList();
- LocalVariableTable lt = (LocalVariableTable) method.getLocalVariableTable();
- if (lt != null) {
- for (int j = 0; j < lt.getLocalVariableTable().length; j++) {
- LocalVariable localVariable = lt.getLocalVariableTable()[j];
- if (localVariable.getStartPC() == 0) {
- if (localVariable.getIndex() >= startAtStackIndex) {
- arguments.add(new MethodArgument(localVariable.getName(), localVariable.getIndex()));
- }
- }
- }
- }
-
- if (arguments.size() != method.getArgumentTypes().length) {
- return EMPTY_STRINGS;
- }
-
- // sort by index
- Collections.sort(
- arguments, new Comparator() {
- public int compare(Object o, Object o1) {
- MethodArgument mo = (MethodArgument) o;
- MethodArgument mo1 = (MethodArgument) o1;
- if (mo.indexOnStack == mo1.indexOnStack) {
- return 0;
- } else if (mo.indexOnStack > mo1.indexOnStack) {
- return 1;
- } else {
- return -1;
- }
- }
- }
- );
- String[] argumentNames = new String[arguments.size()];
- int i = 0;
- for (Iterator iterator = arguments.iterator(); iterator.hasNext(); i++) {
- MethodArgument methodArgument = (MethodArgument) iterator.next();
- argumentNames[i] = methodArgument.name;
- }
- return argumentNames;
- }
-
- /**
- * A method argument, used for sorting by indexOnStack (ie order in signature)
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- private static class MethodArgument {
- String name;
- int indexOnStack;
-
- public MethodArgument(String name, int indexOnStack) {
- this.name = name;
- this.indexOnStack = indexOnStack;
- }
- }
-
- /**
- * BindingScope that knows the enclosingType, which is needed for pointcut reference resolution
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- public static class BindingScope extends SimpleScope {
- private ResolvedTypeX m_enclosingType;
- private ISourceContext m_sourceContext;
-
- public BindingScope(ResolvedTypeX type, ISourceContext sourceContext, FormalBinding[] bindings) {
- super(type.getWorld(), bindings);
- m_enclosingType = type;
- m_sourceContext = sourceContext;
- }
-
- public ResolvedTypeX getEnclosingType() {
- return m_enclosingType;
- }
-
- public ISourceLocation makeSourceLocation(IHasPosition location) {
- return m_sourceContext.makeSourceLocation(location);
- }
- }
-
- /**
- * LazyResolvedPointcutDefinition lazyly resolve the pointcut so that we have time to register all
- * pointcut referenced before pointcut resolution happens
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- public static class LazyResolvedPointcutDefinition extends ResolvedPointcutDefinition {
- private Pointcut m_pointcutUnresolved;
- private IScope m_binding;
-
- private Pointcut m_lazyPointcut = null;
-
- public LazyResolvedPointcutDefinition(ResolvedTypeX declaringType, int modifiers, String name,
- TypeX[] parameterTypes, TypeX returnType,
- Pointcut pointcut, IScope binding) {
- super(declaringType, modifiers, name, parameterTypes, returnType, null);
- m_pointcutUnresolved = pointcut;
- m_binding = binding;
- }
-
- public Pointcut getPointcut() {
- if (m_lazyPointcut == null) {
- m_lazyPointcut = m_pointcutUnresolved.resolve(m_binding);
- m_lazyPointcut.copyLocationFrom(m_pointcutUnresolved);
- }
- return m_lazyPointcut;
- }
- }
-
- /**
- * Helper to test empty strings
- *
- * @param s
- * @return
- */
- private static boolean isNullOrEmpty(String s) {
- return (s == null || s.length() <= 0);
- }
-
- /**
- * Set the pointcut bindings for which to ignore unbound issues, so that we can implicitly bind
- * xxxJoinPoint for @AJ advices
- *
- * @param pointcut
- * @param bindings
- */
- private static void setIgnoreUnboundBindingNames(Pointcut pointcut, FormalBinding[] bindings) {
- // register ImplicitBindings as to be ignored since unbound
- // TODO is it likely to fail in a bad way if f.e. this(jp) etc ?
- List ignores = new ArrayList();
- for (int i = 0; i < bindings.length; i++) {
- FormalBinding formalBinding = bindings[i];
- if (formalBinding instanceof FormalBinding.ImplicitFormalBinding) {
- ignores.add(formalBinding.getName());
- }
- }
- pointcut.m_ignoreUnboundBindingForNames = (String[]) ignores.toArray(new String[ignores.size()]);
- }
-
- /**
- * A check exception when we cannot read debug info (needed for formal binding)
- */
- private static class UnreadableDebugInfoException extends Exception {
- }
-
- /**
- * Report an error
- *
- * @param message
- * @param location
- */
- private static void reportError(String message, AjAttributeStruct location) {
- if (!location.handler.isIgnoring(IMessage.ERROR)) {
- location.handler.handleMessage(
- new Message(
- message,
- location.enclosingType.getSourceLocation(),
- true
- )
- );
- }
- }
-
- /**
- * Report a warning
- *
- * @param message
- * @param location
- */
- private static void reportWarning(String message, AjAttributeStruct location) {
- if (!location.handler.isIgnoring(IMessage.WARNING)) {
- location.handler.handleMessage(
- new Message(
- message,
- location.enclosingType.getSourceLocation(),
- false
- )
- );
- }
- }
-
- /**
- * Parse the given pointcut, return null on failure and issue an error
- *
- * @param pointcutString
- * @param struct
- * @param allowIf
- * @return
- */
- private static Pointcut parsePointcut(String pointcutString, AjAttributeStruct struct, boolean allowIf) {
- try {
- Pointcut pointcut = new PatternParser(pointcutString, struct.context).parsePointcut();
- if (!allowIf && pointcutString.indexOf("if()") >= 0 && hasIf(pointcut)) {
- reportError("if() pointcut is not allowed at this pointcut location '" + pointcutString +"'", struct);
- return null;
- }
- pointcut.setLocation(struct.context, -1, -1);//FIXME -1,-1 is not good enough
- return pointcut;
- } catch (ParserException e) {
- reportError("Invalid pointcut '" + pointcutString + "': " + e.toString(), struct);
- return null;
- }
- }
-
- private static boolean hasIf(Pointcut pointcut) {
- IfFinder visitor = new IfFinder();
- pointcut.accept(visitor, null);
- return visitor.hasIf;
- }
-
- /**
- * Parse the given type pattern, return null on failure and issue an error
- *
- * @param patternString
- * @param location
- * @return
- */
- private static TypePattern parseTypePattern(String patternString, AjAttributeStruct location) {
- try {
- TypePattern typePattern = new PatternParser(patternString).parseTypePattern();
- typePattern.setLocation(location.context, -1, -1);//FIXME -1,-1 is not good enough
- return typePattern;
- } catch (ParserException e) {
- reportError("Invalid type pattern'" + patternString + "' : " + e.getLocation(), location);
- return null;
- }
- }
-
- /**
- * Look for an if() pointcut
- */
- private static class IfFinder extends IdentityPointcutVisitor {
- boolean hasIf = false;
- public Object visit(IfPointcut node, Object data) {
- if (node.alwaysFalse() || node.alwaysTrue()) {
- ;//IfFalse / IfTrue
- } else {
- hasIf = true;
- }
- return node;
- }
-
- public Object visit(AndPointcut node, Object data) {
- if (!hasIf) node.getLeft().accept(this, data);
- if (!hasIf) node.getLeft().accept(this, data);
- return node;
- }
-
- public Object visit(NotPointcut node, Object data) {
- if (!hasIf) node.getNegatedPointcut().accept(this, data);
- return node;
- }
-
- public Object visit(OrPointcut node, Object data) {
- if (!hasIf) node.getLeft().accept(this, data);
- if (!hasIf) node.getLeft().accept(this, data);
- return node;
- }
- }
- }
|