@Around("execution((@net.sf.oval.guard.Guarded *).new(..))") public Object allConstructors(final ProceedingJoinPoint thisJoinPoint) throws Throwable { final ConstructorSignature signature = (ConstructorSignature) thisJoinPoint.getSignature(); LOG.debug("aroundConstructor() {1}", signature); final Constructor<?> ctor = signature.getConstructor(); final Object[] args = thisJoinPoint.getArgs(); final Object target = thisJoinPoint.getTarget(); // pre conditions { guard.guardConstructorPre(target, ctor, args); } final Object result = thisJoinPoint.proceed(); // post conditions { guard.guardConstructorPost(target, ctor, args); } return result; }
private static Annotation getAnnotation(final Signature signature, final Class<? extends Annotation> annotationClass) { if (signature instanceof ConstructorSignature) { final Constructor<?> ctor = ConstructorSignature.class.cast(signature) .getConstructor(); return ctor.getAnnotation(annotationClass); } else if (signature instanceof MethodSignature) { return MethodSignature.class.cast(signature) .getMethod() .getAnnotation(annotationClass); } else if (signature instanceof FieldSignature) { return FieldSignature.class.cast(signature) .getField() .getAnnotation(annotationClass); } throw new RuntimeException( "Unsupported signature type " + signature.getClass().getName() ); }
@Test public void createByDefaultConstructor() throws Exception { // act / when final InitializationTestFixture testFixture = new InitializationTestFixture(); // assert / then assertJoinPoint(new AdditionalAssert() { @Override public void additionalAssert(final JoinPoint joinPoint, final String thisContext, final String targetContext) { assertThat("This?", joinPoint.getThis(), sameInstance((Object) testFixture)); assertThat("Target?", joinPoint.getTarget(), sameInstance((Object) testFixture)); assertThat("#Arguments?", joinPoint.getArgs().length, is(0)); assertThat("This context?", thisContext, is("InitializationTestFixture{anyValue='default'}")); assertThat("Target context?", targetContext, is("InitializationTestFixture{anyValue='default'}")); assertThat("Constructor?", ((ConstructorSignature)joinPoint.getSignature()).getConstructor().toString(), is("public eval.org.aspectj.lang.fixture.InitializationTestFixture()")); } }); }
@Test public void createByConstructorWithParameter() throws Exception { // act / when final InitializationTestFixture testFixture = new InitializationTestFixture("MY_VALUE"); // assert / then assertJoinPoint(new AdditionalAssert() { @Override public void additionalAssert(final JoinPoint joinPoint, final String thisContext, final String targetContext) { assertThat("This?", joinPoint.getThis(), sameInstance((Object) testFixture)); assertThat("Target?", joinPoint.getTarget(), sameInstance((Object) testFixture)); assertThat("#Arguments?", joinPoint.getArgs().length, is(1)); assertThat("Argument value?", joinPoint.getArgs()[0], is((Object)"MY_VALUE")); assertThat("This context?", thisContext, is("InitializationTestFixture{anyValue='MY_VALUE'}")); assertThat("Target context?", targetContext, is("InitializationTestFixture{anyValue='MY_VALUE'}")); assertThat("Constructor?", ((ConstructorSignature)joinPoint.getSignature()).getConstructor().toString(), is("public eval.org.aspectj.lang.fixture.InitializationTestFixture(java.lang.String)")); } }); }
@Test public void createByDefaultConstructor() throws Exception { // act / when final PreInitializationTestFixture testFixture=new PreInitializationTestFixture(); // assert / then assertJoinPoint(new AdditionalAssert() { @Override public void additionalAssert(final JoinPoint joinPoint, final String thisContext, final String targetContext) { assertThat("This?", joinPoint.getThis(), nullValue()); assertThat("Target?", joinPoint.getTarget(), nullValue()); assertThat("#Arguments?", joinPoint.getArgs().length, is(0)); assertThat("Constructor?", ((ConstructorSignature)joinPoint.getSignature()).getConstructor().toString(), is("public eval.org.aspectj.lang.fixture.PreInitializationTestFixture()")); } }); }
@Test public void createByConstructorWithParameter() throws Exception { // act / when final PreInitializationTestFixture testFixture=new PreInitializationTestFixture("MY_VALUE"); // assert / then assertJoinPoint(new AdditionalAssert() { @Override public void additionalAssert(final JoinPoint joinPoint, final String thisContext, final String targetContext) { assertThat("This?", joinPoint.getThis(), nullValue()); assertThat("Target?", joinPoint.getTarget(), nullValue()); assertThat("#Arguments?", joinPoint.getArgs().length, is(1)); assertThat("Argument value?", joinPoint.getArgs()[0], is((Object) "MY_VALUE")); assertThat("Constructor?", ((ConstructorSignature)joinPoint.getSignature()).getConstructor().toString(), is("public eval.org.aspectj.lang.fixture.PreInitializationTestFixture(java.lang.String)")); } }); }
/** * Generate traces and gather statistics on a method. * * @param point The proceeding join point. * @param gatherSizeLevel Logging level at which the returned value of the function should be measured. * @param secure Is the method secure ? If so, both the arguments and the return value will be masked. * @param trace Is trace enabled ? If it is, an exit trace will be printed if the logger bound to the class name is * enabled at the FINE level. An entry log will be generated if the logger is enabled at the FINER level. * @return The join point returned value. * @throws Throwable If the join point throws an exception. */ public static Object traceAndMeasureConstructorJoinPoint( ProceedingJoinPoint point, Level gatherSizeLevel, boolean secure, boolean trace ) throws Throwable { ConstructorSignature methodSignature = ConstructorSignature.class.cast( point.getSignature() ); Constructor<?> method = methodSignature.getConstructor(); String opName = method.toString(); Logger logger = null; if ( trace ) { logger = getLoggerByName( method.getDeclaringClass().getName() ); } OperationMetric metric = initializeConstructorMetricIfEnabled( point, secure, logger, method, opName ); return traceAndMeasureJoinPointWithLogger( point, gatherSizeLevel, secure, metric, opName, logger, true, method ); }
/** * Helper that returns all {@link java.lang.annotation.Annotation}s of the given call joint point needed for the * CostHat UseCase * * @return list of all {@link java.lang.annotation.Annotation} */ private List<java.lang.annotation.Annotation> getAnnotations() { Signature signature = getSignature(); if (signature instanceof MethodSignature) { return Arrays.asList(((MethodSignature) getSignature()).getMethod().getAnnotations()); } else if (signature instanceof ConstructorSignature) { return Arrays.asList(((ConstructorSignature) getSignature()).getConstructor().getAnnotations()); } return Lists.newArrayList(); }
private Signature createConstructorSignatureMock(Class<?>... parameterClasses) { final ConstructorSignature constructorSignature = mock(ConstructorSignature.class); when(constructorSignature.getConstructor()).thenReturn(resolveConstructor(parameterClasses)); LOGGER.debug("ConstructorSignature Mock - getConstructor() = {}", constructorSignature.getConstructor()); return constructorSignature; }
/** * Validate arguments of constructor. * * <p>Try NOT to change the signature of this method, in order to keep * it backward compatible. * * @param point Join point * @checkstyle LineLength (3 lines) */ @Before("preinitialization(*.new(.., @(javax.validation.* || javax.validation.constraints.*) (*), ..))") public void beforeCtor(final JoinPoint point) { if (this.validator != null) { @SuppressWarnings("unchecked") final Constructor<Object> constructor = (Constructor<Object>) ConstructorSignature.class.cast(point.getSignature()) .getConstructor(); this.validateConstructor( constructor, point.getArgs() ); } }
public InitializationTest() { super("eval.org.aspectj.lang.InitializationAspect", JoinPoint.INITIALIZATION, ConstructorSignature.class); }
public ConstructorExecutionTest() { super("eval.org.aspectj.lang.ConstructorExecutionAspect", JoinPoint.CONSTRUCTOR_EXECUTION, ConstructorSignature.class); }
public ConstructorCallTest() { super("eval.org.aspectj.lang.ConstructorCallAspect", JoinPoint.CONSTRUCTOR_CALL, ConstructorSignature.class); }
public PreInitializationTest() { super("eval.org.aspectj.lang.PreInitializationAspect", JoinPoint.PREINITIALIZATION, ConstructorSignature.class); }
ConstructorJoinPointMatcher(AjJoinPointType joinPointType) { super(joinPointType, ConstructorSignature.class); }
@Override protected boolean doMatchSignature(ConstructorSignature signature, AjJoinPoint ajUnitJoinPoint) { return signature.getConstructor().equals(ajUnitJoinPoint.getConstructor()); }
/** * Generate traces and gather statistics on a constructor. * * @param point The proceeding join point. * @param gatherSizeLevel Logging level at which the returned value of the function should be measured. * @param secure Is the method secure ? If so, both the arguments and the return value will be masked. * @param logger The logger to use. * @param operationNamePrefix the name of the layer being measured, appended as a prefix to the operation name. * @param printReturnValue option to print the returned value.f * @return The join point returned value. * @throws Throwable If the join point throws an exception. */ public static Object traceAndMeasureConstructorJoinPointWithLogger( ProceedingJoinPoint point, Level gatherSizeLevel, boolean secure, Logger logger, String operationNamePrefix, boolean printReturnValue ) throws Throwable { ConstructorSignature constructorSignature = ConstructorSignature.class.cast( point.getSignature() ); Constructor<?> constructor = constructorSignature.getConstructor(); String opName = operationNamePrefix + constructor.toString(); OperationMetric metric = initializeConstructorMetricIfEnabled( point, secure, logger, constructor, opName ); return traceAndMeasureJoinPointWithLogger( point, gatherSizeLevel, secure, metric, opName, logger, printReturnValue, constructor ); }