Example usage for org.aspectj.weaver.patterns NamePattern NamePattern

List of usage examples for org.aspectj.weaver.patterns NamePattern NamePattern

Introduction

In this page you can find the example usage for org.aspectj.weaver.patterns NamePattern NamePattern.

Prototype

public NamePattern(char[] pattern) 

Source Link

Usage

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

private CaesarPointcutWrapper parseKindedPointcut() {
    String kind = parseIdentifier();
    eat("(");//from  w  w w.ja  v a 2s  .  c o  m
    SignaturePattern sig;

    Shadow.Kind shadowKind = null;
    if (kind.equals("execution")) {
        sig = parseMethodOrConstructorSignaturePattern();
        if (sig.getKind() == Member.METHOD) {
            shadowKind = Shadow.MethodExecution;
        } else if (sig.getKind() == Member.CONSTRUCTOR) {
            shadowKind = Shadow.ConstructorExecution;
        }
    } else if (kind.equals("call")) {
        sig = parseMethodOrConstructorSignaturePattern();
        if (sig.getKind() == Member.METHOD) {
            shadowKind = Shadow.MethodCall;
        } else if (sig.getKind() == Member.CONSTRUCTOR) {
            shadowKind = Shadow.ConstructorCall;
        }
    } else if (kind.equals("get")) {
        sig = parseFieldSignaturePattern();
        shadowKind = Shadow.FieldGet;
    } else if (kind.equals("set")) {
        sig = parseFieldSignaturePattern();
        shadowKind = Shadow.FieldSet;
    } else {
        throw new ParserException("bad kind: " + kind, tokenSource.peek());
    }
    eat(")");

    // Creates the wrapper

    if (Shadow.MethodCall.equals(shadowKind) || Shadow.MethodExecution.equals(shadowKind)) {

        // Method call and execution are wrapped in an "and pointcut" to avoid getting constructors
        CaesarKindedPointcut p = new CaesarKindedPointcut(shadowKind, sig);
        registerPointcut(new CaesarPointcutWrapper(p));

        Pointcut andPointcut = new AndPointcut(p,
                new NotPointcut(new CaesarKindedPointcut(shadowKind, this.createConstructorSignature()),
                        tokenSource.peek().getStart()));

        return new CaesarPointcutWrapper(andPointcut);
    } else if (Shadow.ConstructorCall.equals(shadowKind) || Shadow.ConstructorExecution.equals(shadowKind)) {

        Shadow.Kind cclassShadowKind = null;

        // Transform the constructor call/execution to a method call/execution, 
        // using $constructor and the same parameters
        if (Shadow.ConstructorCall.equals(shadowKind)) {
            cclassShadowKind = Shadow.MethodCall;
        } else {
            cclassShadowKind = Shadow.MethodExecution;
        }

        CaesarCloner c = CaesarCloner.instance();

        SignaturePattern cclassSig = new SignaturePattern(Member.METHOD, sig.getModifiers(),
                createCaesarObjectPattern(), c.clone(sig.getDeclaringType()), new NamePattern("$constructor"),
                c.clone(sig.getParameterTypes()), c.clone(sig.getThrowsPattern()));

        CaesarKindedPointcut regular = new CaesarKindedPointcut(shadowKind, sig);

        CaesarKindedPointcut cclass = new CaesarKindedPointcut(cclassShadowKind, cclassSig);

        registerPointcut(new CaesarPointcutWrapper(regular));
        registerPointcut(new CaesarPointcutWrapper(cclass));

        // Creates an orPointcut for both the regular java and cclass constructors
        Pointcut orPointcut = new OrPointcut(regular, cclass);

        return new CaesarPointcutWrapper(orPointcut);
    }

    if (Shadow.FieldGet.equals(shadowKind) || Shadow.FieldSet.equals(shadowKind)) {

        // Creates the wrapper and register it
        CaesarKindedPointcut p = new CaesarKindedPointcut(shadowKind, sig);
        CaesarPointcutWrapper wrapper = new CaesarPointcutWrapper(p);
        registerPointcut(wrapper);

        // Append something like || get(public * Classname_Impl_Mixin_*.field)
        TypePattern mixinType = createMixinType(sig.getDeclaringType());
        SignaturePattern mixinSignature = createMixinSignature(sig, mixinType);

        CaesarKindedPointcut mixin = new CaesarKindedPointcut(shadowKind, mixinSignature);

        // Register the mixin
        wrapper = new CaesarPointcutWrapper(mixin, sig.getDeclaringType());
        wrapper.setDeclaringType(mixinType);
        registerPointcut(wrapper);

        // Creates an orPointcut for both the type and the mixin
        Pointcut orPointcut = new OrPointcut(p, mixin);

        return new CaesarPointcutWrapper(orPointcut);

    }
    CaesarKindedPointcut p = new CaesarKindedPointcut(shadowKind, sig);
    return new CaesarPointcutWrapper(p);
}

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

/**
 * Parses a Withincode Pointcut/*from  w  w  w  . j  a  v  a  2  s.c  o  m*/
 * 
 * @return
 */
private CaesarPointcutWrapper parseWithinCodePointcut() {

    // Parses the signature pattern
    parseIdentifier();
    eat("(");
    SignaturePattern sig = parseMethodOrConstructorSignaturePattern();
    eat(")");

    // Gets the declaring type
    TypePattern type = sig.getDeclaringType();

    // Creates the wrapper and register it
    Pointcut p = new WithincodePointcut(sig);
    CaesarPointcutWrapper wrapper = new CaesarPointcutWrapper(p);
    wrapper.setDeclaringType(type);
    registerPointcut(wrapper);

    if (Member.CONSTRUCTOR.equals(sig.getKind())) {

        // Transform the constructor withincode to a method withincode, 
        // using $constructor and the same parameters (for cclasses)
        CaesarCloner c = CaesarCloner.instance();

        sig = new SignaturePattern(Member.METHOD, sig.getModifiers(), createCaesarObjectPattern(),
                c.clone(type), new NamePattern("$constructor"), c.clone(sig.getParameterTypes()),
                c.clone(sig.getThrowsPattern()));

        WithincodePointcut cclass = new WithincodePointcut(sig);
        wrapper = new CaesarPointcutWrapper(cclass);
        wrapper.setDeclaringType(sig.getDeclaringType());
        registerPointcut(wrapper);

        // Creates an orPointcut for both the regular java and cclass constructors
        p = new OrPointcut(p, cclass);
    }

    // Creates something like || withincode(* Classname_Impl_Mixin_*.m())
    TypePattern mixinType = createMixinType(type);
    SignaturePattern mixinSignature = createMixinSignature(sig, mixinType);

    WithincodePointcut mixin = new WithincodePointcut(mixinSignature);

    // Register the mixin
    wrapper = new CaesarPointcutWrapper(mixin, type);
    wrapper.setDeclaringType(mixinType);
    registerPointcut(wrapper);

    // Creates an orPointcut for both the type and the mixin
    Pointcut orPointcut = new OrPointcut(p, mixin);

    return new CaesarPointcutWrapper(orPointcut);
}

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

public List<NamePattern> parseDottedNamePattern() {
    List<NamePattern> names = new ArrayList<NamePattern>();
    StringBuffer buf = new StringBuffer();
    IToken previous = null;/*from  w  w w.j a  va 2  s  .c  o m*/
    boolean justProcessedEllipsis = false; // Remember if we just dealt with an ellipsis (PR61536)
    boolean justProcessedDot = false;
    boolean onADot = false;
    while (true) {
        IToken tok = null;
        int startPos = tokenSource.peek().getStart();
        String afterDot = null;
        while (true) {
            if (previous != null && previous.getString().equals("."))
                justProcessedDot = true;
            tok = tokenSource.peek();
            onADot = (tok.getString().equals("."));
            if (previous != null) {
                if (!isAdjacent(previous, tok))
                    break;
            }
            if (tok.getString() == "*" || tok.isIdentifier()) {
                buf.append(tok.getString());
            } else if (tok.getLiteralKind() != null) {
                //System.err.println("literal kind: " + tok.getString());
                String s = tok.getString();
                int dot = s.indexOf('.');
                if (dot != -1) {
                    buf.append(s.substring(0, dot));
                    afterDot = s.substring(dot + 1);
                    previous = tokenSource.next();
                    break;
                }
                buf.append(s); // ??? so-so
            } else {
                break;
            }
            previous = tokenSource.next();
            //XXX need to handle floats and other fun stuff
        }
        int endPos = tokenSource.peek(-1).getEnd();
        if (buf.length() == 0 && names.isEmpty()) {
            throw new ParserException("expected name pattern", tok);
        }

        if (buf.length() == 0 && justProcessedEllipsis) {
            throw new ParserException("name pattern cannot finish with ..", tok);
        }
        if (buf.length() == 0 && justProcessedDot && !onADot) {
            throw new ParserException("name pattern cannot finish with .", tok);
        }

        if (buf.length() == 0) {
            names.add(NamePattern.ELLIPSIS);
            justProcessedEllipsis = true;
        } else {
            checkLegalName(buf.toString(), previous);
            NamePattern ret = new NamePattern(buf.toString());
            ret.setLocation(sourceContext, startPos, endPos);
            names.add(ret);
            justProcessedEllipsis = false;
        }

        if (afterDot == null) {
            buf.setLength(0);
            if (!maybeEat("."))
                break;
            else
                previous = tokenSource.peek(-1);
        } else {
            buf.setLength(0);
            buf.append(afterDot);
            afterDot = null;
        }
    }
    //System.err.println("parsed: " + names);
    return names;
}

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

/**
 * Creates a TypePatternList which contains only the CaesarWildTypePattern needed
 * to match the java.lang.Object name. This is used to create the parameters
 * list when matching the default contructor
 * //w ww.j a  va2 s . c  o m
 * @return a list with the pattern for Object
 */
protected TypePatternList createObjectTypeList() {

    ArrayList<NamePattern> names = new ArrayList<NamePattern>();
    names.add(new NamePattern("java.lang.Object"));

    return new TypePatternList(new TypePattern[] { new CaesarWildTypePattern(names, false, 0) });
}

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

/**
 * Creates a type pattern (a CaesarWildTypePattern) which represents the CaesarObject
 * class. /*from ww w .j  a v a2 s. com*/
 * 
 * @return a type pattern for the CaesarObject class
 */
protected TypePattern createCaesarObjectPattern() {

    ArrayList<NamePattern> names = new ArrayList<NamePattern>();
    names.add(new NamePattern("org.caesarj.runtime.CaesarObject"));

    return new CaesarWildTypePattern(names, false, 0);
}

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

/**
 * Creates a signator which select constructors:
 * /* w w w .  jav a2s. co m*/
 *   * $constructor(..)
 *   
 * @return
 */
protected SignaturePattern createConstructorSignature() {

    return new SignaturePattern(Member.METHOD, ModifiersPattern.ANY, TypePattern.ANY, TypePattern.ANY,
            new NamePattern("$constructor"), new TypePatternList(new TypePattern[] { TypePattern.ELLIPSIS }),
            ThrowsPattern.ANY);
}

From source file:org.caesarj.compiler.aspectj.CaesarWrapperPatternParser.java

License:Open Source License

/**
 * Returns a clone of the name patterns this type pattern has
 *  /*from   ww w.j  a  va  2s  .c om*/
 * @param pattern
 * @return a list of name patterns, which is a clone of this pattern
 */
protected List<NamePattern> createMixinNamePatterns(TypePattern pattern) {

    ArrayList<NamePattern> namePatterns = new ArrayList<NamePattern>();

    String[] names = pattern.toString().split("\\.");

    for (int i = 0; i < names.length; i++) {
        namePatterns.add(new NamePattern(names[i]));
    }

    return namePatterns;
}

From source file:org.caesarj.compiler.joinpoint.PerObjectDeploymentVisitor.java

License:Open Source License

public boolean visit(CjAdviceDeclaration adviceDec) {
    //include the old parameters
    List adviceParameters = new ArrayList();

    for (int i = 0; i < adviceDec.getParameters().length; i++) {
        adviceParameters.add(adviceDec.getParameters()[i]);
    }//  www  . jav  a  2s  .c om

    /* add additiona parameter JOINPOINT_THIS_PARAM to the advice */
    adviceDec.setExtraArgumentFlag(CaesarConstants.JoinPointThis);

    JFormalParameter extraParameter = new JFormalParameter(adviceDec.getTokenReference(),
            JFormalParameter.DES_PARAMETER, new CClassNameType(Constants.JAV_OBJECT),
            CaesarConstants.JOINPOINT_THIS_PARAM, false);
    adviceParameters.add(extraParameter);

    adviceDec.setParameters((JFormalParameter[]) adviceParameters.toArray(new JFormalParameter[0]));

    /* transform the advice poincut from a() to (a() && this(JOINPOINT_THIS_PARAM)) */
    ArrayList<NamePattern> names = new ArrayList<NamePattern>();
    names.add(new NamePattern(CaesarConstants.JOINPOINT_THIS_PARAM));

    CaesarWildTypePattern pattern = new CaesarWildTypePattern(names, false, 0);

    Pointcut origPointcut = adviceDec.getPointcut().wrappee();
    Pointcut newPointcut = new AndPointcut(origPointcut, new ThisOrTargetPointcut(true, pattern));
    adviceDec.getPointcut().replacePointcut(newPointcut);

    return false;
}