/** * Create a ParameterNameDiscoverer to be used for argument binding. * <p>The default implementation creates a {@link DefaultParameterNameDiscoverer} * and adds a specifically configured {@link AspectJAdviceParameterNameDiscoverer}. */ protected ParameterNameDiscoverer createParameterNameDiscoverer() { // We need to discover them, or if that fails, guess, // and if we can't guess with 100% accuracy, fail. DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); AspectJAdviceParameterNameDiscoverer adviceParameterNameDiscoverer = new AspectJAdviceParameterNameDiscoverer(this.pointcut.getExpression()); adviceParameterNameDiscoverer.setReturningName(this.returningName); adviceParameterNameDiscoverer.setThrowingName(this.throwingName); // Last in chain, so if we're called and we fail, that's bad... adviceParameterNameDiscoverer.setRaiseExceptions(true); discoverer.addDiscoverer(adviceParameterNameDiscoverer); return discoverer; }
tokens[i].equals("target") || tokens[i].startsWith("target(")) { PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; List<String> candidateVarNames = new ArrayList<>(); maybeExtractVariableNamesFromArgs(body.text, candidateVarNames); if (!alreadyBound(varName)) { varNames.add(varName); if (isUnbound(j)) { bindParameterName(j, varNames.get(0)); break;
PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; maybeExtractVariableNamesFromArgs(body.text, varNames); bindAnnotationsFromVarNames(varNames);
private void findAndBind(Class<?> argumentType, String varName) { for (int i = 0; i < this.argumentTypes.length; i++) { if (isUnbound(i) && isSubtypeOf(argumentType, i)) { bindParameterName(i, varName); return; } } throw new IllegalStateException("Expected to find an unbound argument of type '" + argumentType.getName() + "'"); }
int numUnboundPrimitives = countNumberOfUnboundPrimitiveArguments(); if (numUnboundPrimitives > 1) { throw new AmbiguousBindingException("Found '" + numUnboundPrimitives + for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("args") || tokens[i].startsWith("args(")) { PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; maybeExtractVariableNamesFromArgs(body.text, varNames); if (isUnbound(i) && this.argumentTypes[i].isPrimitive()) { bindParameterName(i, varNames.get(0)); break;
PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); if (isUnbound(j)) { bindParameterName(j, varNames.get(0)); break;
@Test public void testParameterNameDiscoverWithReferencePointcut() throws Exception { AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer("somepc(formal) && set(* *)"); discoverer.setRaiseExceptions(true); Method methodUsedForParameterTypeDiscovery = getClass().getMethod("methodWithOneParam", String.class); String[] pnames = discoverer.getParameterNames(methodUsedForParameterTypeDiscovery); assertEquals("one parameter name", 1, pnames.length); assertEquals("formal", pnames[0]); }
/** * Match the given list of extracted variable names to argument slots. */ private void bindAnnotationsFromVarNames(List<String> varNames) { if (!varNames.isEmpty()) { // we have work to do... int numAnnotationSlots = countNumberOfUnboundAnnotationArguments(); if (numAnnotationSlots > 1) { throw new AmbiguousBindingException("Found " + varNames.size() + " potential annotation variable(s), and " + numAnnotationSlots + " potential argument slots"); } else if (numAnnotationSlots == 1) { if (varNames.size() == 1) { // it's a match findAndBind(Annotation.class, varNames.get(0)); } else { // multiple candidate vars, but only one slot throw new IllegalArgumentException("Found " + varNames.size() + " candidate annotation binding variables" + " but only one potential argument binding slot"); } } else { // no slots so presume those candidate vars were actually type names } } }
private void maybeBindThisJoinPointStaticPart() { if (this.argumentTypes[0] == JoinPoint.StaticPart.class) { bindParameterName(0, THIS_JOIN_POINT_STATIC_PART); } }
int numUnboundPrimitives = countNumberOfUnboundPrimitiveArguments(); if (numUnboundPrimitives > 1) { throw new AmbiguousBindingException("Found '" + numUnboundPrimitives + for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("args") || tokens[i].startsWith("args(")) { PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; maybeExtractVariableNamesFromArgs(body.text, varNames); if (isUnbound(i) && this.argumentTypes[i].isPrimitive()) { bindParameterName(i, varNames.get(0)); break;
if (isUnbound(i) && isSubtypeOf(Throwable.class, i)) { if (throwableIndex == -1) { throwableIndex = i; bindParameterName(throwableIndex, this.throwingName);
PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); if (isUnbound(j)) { bindParameterName(j, varNames.get(0)); break;
/** * Match the given list of extracted variable names to argument slots. */ private void bindAnnotationsFromVarNames(List<String> varNames) { if (!varNames.isEmpty()) { // we have work to do... int numAnnotationSlots = countNumberOfUnboundAnnotationArguments(); if (numAnnotationSlots > 1) { throw new AmbiguousBindingException("Found " + varNames.size() + " potential annotation variable(s), and " + numAnnotationSlots + " potential argument slots"); } else if (numAnnotationSlots == 1) { if (varNames.size() == 1) { // it's a match findAndBind(Annotation.class, varNames.get(0)); } else { // multiple candidate vars, but only one slot throw new IllegalArgumentException("Found " + varNames.size() + " candidate annotation binding variables" + " but only one potential argument binding slot"); } } else { // no slots so presume those candidate vars were actually type names } } }
/** * If the first parameter is of type JoinPoint or ProceedingJoinPoint,bind "thisJoinPoint" as * parameter name and return true, else return false. */ private boolean maybeBindThisJoinPoint() { if ((this.argumentTypes[0] == JoinPoint.class) || (this.argumentTypes[0] == ProceedingJoinPoint.class)) { bindParameterName(0, THIS_JOIN_POINT); return true; } else { return false; } }
protected void assertException( Method method, String pointcut, String returning, String throwing, Class<?> exceptionType, String message) { AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer(pointcut); discoverer.setRaiseExceptions(true); discoverer.setReturningName(returning); discoverer.setThrowingName(throwing); try { discoverer.getParameterNames(method); fail("Expecting " + exceptionType.getName() + " with message '" + message + "'"); } catch (RuntimeException expected) { assertEquals("Expecting exception of type " + exceptionType.getName(), exceptionType, expected.getClass()); assertEquals("Exception message does not match expected", message, expected.getMessage()); } }
tokens[i].equals("target") || tokens[i].startsWith("target(")) { PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; List<String> candidateVarNames = new ArrayList<String>(); maybeExtractVariableNamesFromArgs(body.text, candidateVarNames); if (!alreadyBound(varName)) { varNames.add(varName); if (isUnbound(j)) { bindParameterName(j, varNames.get(0)); break;
int numUnboundPrimitives = countNumberOfUnboundPrimitiveArguments(); if (numUnboundPrimitives > 1) { throw new AmbiguousBindingException("Found '" + numUnboundPrimitives + for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("args") || tokens[i].startsWith("args(")) { PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; maybeExtractVariableNamesFromArgs(body.text, varNames); if (isUnbound(i) && this.argumentTypes[i].isPrimitive()) { bindParameterName(i, varNames.get(0)); break;
private void findAndBind(Class<?> argumentType, String varName) { for (int i = 0; i < this.argumentTypes.length; i++) { if (isUnbound(i) && isSubtypeOf(argumentType, i)) { bindParameterName(i, varName); return; } } throw new IllegalStateException("Expected to find an unbound argument of type '" + argumentType.getName() + "'"); }
PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; maybeExtractVariableNamesFromArgs(body.text, varNames); bindAnnotationsFromVarNames(varNames);
PointcutBody body = getPointcutBody(tokens, i); i += body.numTokensConsumed; String varName = maybeExtractVariableName(body.text); if (varName != null) { varNames.add(varName); if (isUnbound(j)) { bindParameterName(j, varNames.get(0)); break;