My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @(#)AtCompositeDemo.java 28 May 2009
*/
package com.qrmedia.pattern.compositeannotation.example;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qrmedia.pattern.compositeannotation.annotation.CompositeAnnotation;
import com.qrmedia.pattern.compositeannotation.annotation.LeafAnnotation;
import com.qrmedia.pattern.compositeannotation.api.AnnotatedElements;
import com.qrmedia.pattern.compositeannotation.example.annotation.TargetRetentionLeafCompositeAnnotation;
import com.qrmedia.pattern.compositeannotation.validation.CompositeAnnotationValidationProcessor;

/**
* A demonstration of integrating <strong>@Composite</strong> with Spring.
* <p>
* You can test the {@linkplain CompositeAnnotationValidationProcessor validating processor}
* by passing a {@code -processor com.qrmedia.pattern.compositeannotation.validation.CompositeAnnotationValidationProcessor}
* argument to <strong>javac</strong>.
*
* @author aphillips
* @since 28 May 2009
*
*/
public class AtCompositeDemo {
private static final String[] CONTEXT_FILENAMES =
new String[] { "example-applicationContext.xml" };

@Retention(RetentionPolicy.RUNTIME)
@CompositeAnnotation
private static @interface InvalidLeafStubCompositeAnnotation {
// invalid, since the annotated attribute does not return an annotation
@LeafAnnotation
int nonAnnotationAttribute() default 0;
}

@Resource
@TargetRetentionLeafCompositeAnnotation(runtimeRetention = true)
private static @interface AnnotatedAnnotation {}

@Resource
@TargetRetentionLeafCompositeAnnotation(runtimeRetention = false)
private static @interface OtherAnnotatedAnnotation {}

@InvalidLeafStubCompositeAnnotation
public static void main(String[] args) {
Log log = LogFactory.getLog(AtCompositeDemo.class);

AnnotatedElements annotatedElements =
(AnnotatedElements) new ClassPathXmlApplicationContext(CONTEXT_FILENAMES)
.getBean("com.qrmedia.pattern.compositeannotation.atCompositeAnnotatedElements");

log.warn("IMPORTANT: for demonstration purposes only! Composite annotations cannot be used to \"macro\" annotations such as @Retention or @Target that need to be visible to the compiler and/or standard annotation reflection methods! See the Javadoc for @CompositeAnnotation.");

log.info("Retrieving annotations from AnnotatedAnnotation.class");
log.info(Arrays.toString(annotatedElements.getAnnotations(AnnotatedAnnotation.class)));

log.info("Retrieving annotations from OtherAnnotatedAnnotation.class");
log.info(Arrays.toString(annotatedElements.getAnnotations(OtherAnnotatedAnnotation.class)));
}

}

Change log

r67 by sharedocs1 on Jun 22, 2009   Diff
Used the interface in the demo, rather
than the implementation class.
Go to: 
Project members, sign in to write a code review

Older revisions

r11 by sharedocs1 on Jun 14, 2009   Diff
Initial commit.
All revisions of this file

File info

Size: 3096 bytes, 70 lines
Powered by Google Project Hosting