org.aspectj.lang.annotation.Aspect Java Examples

The following examples show how to use org.aspectj.lang.annotation.Aspect. 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
Source File: AbstractAspectJAdvisorFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: ClassPathBeanDefinitionScannerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #4
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #5
Source File: ClassPathBeanDefinitionScannerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: AbstractAspectJAdvisorFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #7
Source File: AbstractAspectJAdvisorFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
Source File: ClassPathBeanDefinitionScannerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #9
Source File: ClassPathBeanDefinitionScannerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #10
Source File: AbstractAspectJAdvisorFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 #11
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #12
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 File: AspectMetadata.java    From spring4-understanding with 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 #14
Source File: DeepLinkInterceptorProcessor.java    From OkDeepLink with 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 #15
Source File: DeepLinkServiceProcessor.java    From OkDeepLink with 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 #16
Source File: AspectMetadata.java    From lams with 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 #17
Source File: AspectMetadata.java    From java-technology-stack with 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 #18
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #19
Source File: AspectMetadata.java    From spring-analysis-note with 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 #20
Source File: AbstractAspectJAdvisorFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
 
Example #21
Source File: DeepLinkInjectProcessor.java    From OkDeepLink with 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 #22
Source File: AopProxyProcessor.java    From festival with 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 #23
Source File: AbstractAspectJAdvisorFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
 
Example #24
Source File: AbstractAspectJAdvisorFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
 
Example #25
Source File: AbstractAspectJAdvisorFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private boolean hasAspectAnnotation(Class<?> clazz) {
	return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}