Java Code Examples for org.aspectj.lang.annotation.Aspect

The following examples show how to use org.aspectj.lang.annotation.Aspect. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void validate(Class<?> aspectClass) throws AopConfigException {
	// If the parent has the annotation and isn't abstract it's an error
	if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null &&
			!Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) {
		throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" +
				aspectClass.getSuperclass().getName() + "]");
	}

	AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
	if (!ajType.isAspect()) {
		throw new NotAnAtAspectException(aspectClass);
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
}
 
Example 2
@Test
public void testCustomAnnotationExcludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(11, beanCount);
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
 
Example 3
@Test
public void testMultipleCustomExcludeFiltersAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class));
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(10, beanCount);
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example 4
@Override
public void validate(Class<?> aspectClass) throws AopConfigException {
	// If the parent has the annotation and isn't abstract it's an error
	if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null &&
			!Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) {
		throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" +
				aspectClass.getSuperclass().getName() + "]");
	}

	AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
	if (!ajType.isAspect()) {
		throw new NotAnAtAspectException(aspectClass);
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
}
 
Example 5
@Test
public void testCustomAnnotationExcludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(11, beanCount);
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
 
Example 6
@Test
public void testMultipleCustomExcludeFiltersAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class));
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(10, beanCount);
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example 7
@Override
public void validate(Class<?> aspectClass) throws AopConfigException {
	// If the parent has the annotation and isn't abstract it's an error
	if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null &&
			!Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) {
		throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" +
				aspectClass.getSuperclass().getName() + "]");
	}

	AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
	if (!ajType.isAspect()) {
		throw new NotAnAtAspectException(aspectClass);
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
}
 
Example 8
@Override
public void validate(Class<?> aspectClass) throws AopConfigException {
	// If the parent has the annotation and isn't abstract it's an error
	if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null &&
			!Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) {
		throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" +
				aspectClass.getSuperclass().getName() + "]");
	}

	AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
	if (!ajType.isAspect()) {
		throw new NotAnAtAspectException(aspectClass);
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
	if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) {
		throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " +
				"This is not supported in Spring AOP.");
	}
}
 
Example 9
@Test
public void testCustomAnnotationExcludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);
	assertEquals(11, beanCount);
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
 
Example 10
@Test
public void testMultipleCustomExcludeFiltersAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class));
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);
	assertEquals(10, beanCount);
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example 11
Source Project: spring-analysis-note   Source File: AspectMetadata.java    License: MIT License 5 votes vote down vote up
/**
 * Extract contents from String of form {@code pertarget(contents)}.
 */
private String findPerClause(Class<?> aspectClass) {
	String str = aspectClass.getAnnotation(Aspect.class).value();
	str = str.substring(str.indexOf('(') + 1);
	str = str.substring(0, str.length() - 1);
	return str;
}
 
Example 12
@Test
public void testWithAspectAnnotationOnly() throws Exception {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.addIncludeFilter(new AnnotationTypeFilter(Aspect.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertEquals(1, candidates.size());
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
}
 
Example 13
Source Project: java-technology-stack   Source File: AspectMetadata.java    License: MIT License 5 votes vote down vote up
/**
 * Extract contents from String of form {@code pertarget(contents)}.
 */
private String findPerClause(Class<?> aspectClass) {
	String str = aspectClass.getAnnotation(Aspect.class).value();
	str = str.substring(str.indexOf('(') + 1);
	str = str.substring(0, str.length() - 1);
	return str;
}
 
Example 14
@Test
public void testWithAspectAnnotationOnly() throws Exception {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.addIncludeFilter(new AnnotationTypeFilter(Aspect.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertEquals(1, candidates.size());
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
}
 
Example 15
Source Project: lams   Source File: AspectMetadata.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Extract contents from String of form {@code pertarget(contents)}.
 */
private String findPerClause(Class<?> aspectClass) {
	String str = aspectClass.getAnnotation(Aspect.class).value();
	str = str.substring(str.indexOf("(") + 1);
	str = str.substring(0, str.length() - 1);
	return str;
}
 
Example 16
Source Project: OkDeepLink   Source File: DeepLinkServiceProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private void generateDeepLinkService(TypeElement serviceElements,
                                     List<AddressElement> deepLinkMatchElements)
        throws IOException {

    ClassName providerClassName = getServiceProviderClassName(serviceElements);

    MethodSpec initMethod = generateInitMethod(deepLinkMatchElements);


    FieldSpec activity = FieldSpec
            .builder(DEEP_LINK_CLIENT, "deepLinkClient",
                    Modifier.PUBLIC)
            .build();


    MethodSpec activityConstructor = MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addParameter(DEEP_LINK_CLIENT, "deepLinkClient")
            .addCode("this.deepLinkClient= deepLinkClient;\n")
            .build();
    TypeSpec.Builder serviceProviderBuilder = TypeSpec.classBuilder(providerClassName)
            .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
            .addAnnotation(AnnotationSpec.builder(Aspect.class).build())
            .addSuperinterface(ClassName.get(serviceElements))
            .addField(activity)
            .addMethod(activityConstructor)
            .addMethod(initMethod);


    for (AddressElement matchElement : deepLinkMatchElements) {
        ExecutableElement element = (ExecutableElement) matchElement.getElement();
        serviceProviderBuilder.addMethod(geneOverServiceMethod(element));
    }

    MethodSpec buildMethodSpec = generateBuildMethod(serviceElements);
    serviceProviderBuilder.addMethod(buildMethodSpec);
    JavaFile.builder(ClassName.get(serviceElements).packageName(), serviceProviderBuilder.build())
            .build()
            .writeTo(filer);
}
 
Example 17
Source Project: OkDeepLink   Source File: DeepLinkInterceptorProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

    List<InterceptorElement> interceptorElements = generateInterceptorElements(roundEnv);
    if (ElementUtils.isEmpty(interceptorElements)) {
        return false;
    }

    for (InterceptorElement interceptorElement : interceptorElements) {
        String path = interceptorElement.getPath();
        MethodSpec.Builder methodBuilder;
        if (path == null || path.length() == 0) {
            methodBuilder = geneGlobalInterceptorsBuilder(interceptorElement);
        }else {
            methodBuilder = genePathInterceptorsBuilder(interceptorElement);
        }

        try {
            TypeSpec.Builder interceptorInitBuilder = TypeSpec.classBuilder(interceptorElement.getInitClassName())
                    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                    .addAnnotation(AnnotationSpec
                            .builder(Aspect.class)
                            .build())
                    .addMethod(methodBuilder.build());
            JavaFile.builder(interceptorElement.getPackageName(), interceptorInitBuilder.build())
                    .build()
                    .writeTo(filer);
        } catch (IOException e) {
            logger.error("Error creating matcher file", interceptorElement.getElement());
        }
    }


    return true;
}
 
Example 18
Source Project: spring4-understanding   Source File: AspectMetadata.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Extract contents from String of form {@code pertarget(contents)}.
 */
private String findPerClause(Class<?> aspectClass) {
	// TODO when AspectJ provides this, we can remove this hack. Hence we don't
	// bother to make it elegant. Or efficient. Or robust :-)
	String str = aspectClass.getAnnotation(Aspect.class).value();
	str = str.substring(str.indexOf("(") + 1);
	str = str.substring(0, str.length() - 1);
	return str;
}
 
Example 19
@Test
public void testWithAspectAnnotationOnly() throws Exception {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.addIncludeFilter(new AnnotationTypeFilter(Aspect.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertEquals(1, candidates.size());
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
}
 
Example 20
Source Project: festival   Source File: AopProxyProcessor.java    License: Apache License 2.0 4 votes vote down vote up
private boolean checkIfNeedRegister(ConfigurableBeanFactory beanFactory, BeanDefinition beanDefinition) {
    return beanDefinition.getBeanClass().isAnnotationPresent(Aspect.class) && !beanFactory.containSingleton(beanDefinition.getBeanName());
}
 
Example 21
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
 
Example 22
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
 
Example 23
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
 
Example 24
Source Project: OkDeepLink   Source File: DeepLinkInjectProcessor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

    List<Element> injectElements = new ArrayList<>();
    List<Element> queryElements = generateQueryElements(roundEnv);
    List<Element> serviceElements = generateServiceElements(roundEnv);
    injectElements.addAll(queryElements);
    injectElements.addAll(serviceElements);
    if (ElementUtils.isEmpty(injectElements)) {
        return false;
    }
    targetInjectElements = findInjectElements(injectElements);
    if (ElementUtils.isEmpty(targetInjectElements)) {
        return false;
    }
    for (Map.Entry<TypeElement, List<Element>> injectElementEntrySet : targetInjectElements.entrySet()) {
        TypeElement targetElement = injectElementEntrySet.getKey();
        List<Element> fieldElements = injectElementEntrySet.getValue();

        MethodSpec injectQueryMethod = geneOnCreateQueryMethod(targetElement, fieldElements);

        MethodSpec injectServiceMethod = geneInjectServiceMethod(targetElement, fieldElements);

        MethodSpec saveInstanceMethod = geneSaveInstanceMethod(targetElement, fieldElements);


        MethodSpec newIntentMethod = geneOnNewIntentQueryMethod(targetElement, fieldElements);

        String fileName = targetElement.getSimpleName() + INJECTOR_SUFFIX;
        TypeSpec.Builder helper = TypeSpec.classBuilder(fileName)
                .addModifiers(PUBLIC)
                .addAnnotation(AnnotationSpec.builder(Aspect.class).build())
                .addMethod(injectQueryMethod)
                .addMethod(injectServiceMethod)
                .addMethod(saveInstanceMethod);

        TypeMirror typeMirror = elements.getTypeElement(ACTIVITY).asType();
        if (types.isSubtype(targetElement.asType(), typeMirror)) {
            helper.addMethod(newIntentMethod);
        }

        try {
            PackageElement packageElement = (PackageElement) targetElement.getEnclosingElement();
            JavaFile.builder(packageElement.getQualifiedName().toString(), helper.build()).build().writeTo(filer);
        } catch (IOException e) {
            logger.error("Error creating inject file", targetElement);
        }

    }
    return true;
}
 
Example 25
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}