aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/AbstractAnnotationAJ.java
blob: e098251d17531c432d6f31e04823978a54119dc0 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* *******************************************************************
 * Copyright (c) 2008 Contributors
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * ******************************************************************/
package org.aspectj.weaver;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

public abstract class AbstractAnnotationAJ implements AnnotationAJ {

	protected final ResolvedType type;

	private Set<String> supportedTargets = null; // @target meta annotation

	public AbstractAnnotationAJ(ResolvedType type) {
		this.type = type;
	}

	/**
	 * {@inheritDoc}
	 */
	public final ResolvedType getType() {
		return type;
	}

	/**
	 * {@inheritDoc}
	 */
	public final String getTypeSignature() {
		return type.getSignature();
	}

	/**
	 * {@inheritDoc}
	 */
	public final String getTypeName() {
		return type.getName();
	}

	/**
	 * {@inheritDoc}
	 */
	public final boolean allowedOnAnnotationType() {
		ensureAtTargetInitialized();
		if (supportedTargets.isEmpty()) {
			return true;
		}
		return supportedTargets.contains("ANNOTATION_TYPE");
	}

	/**
	 * {@inheritDoc}
	 */
	public final boolean allowedOnField() {
		ensureAtTargetInitialized();
		if (supportedTargets.isEmpty()) {
			return true;
		}
		return supportedTargets.contains("FIELD");
	}

	/**
	 * {@inheritDoc}
	 */
	public final boolean allowedOnRegularType() {
		ensureAtTargetInitialized();
		if (supportedTargets.isEmpty()) {
			return true;
		}
		return supportedTargets.contains("TYPE");
	}

	/**
	 * {@inheritDoc}
	 */
	public final void ensureAtTargetInitialized() {
		if (supportedTargets == null) {
			AnnotationAJ atTargetAnnotation = retrieveAnnotationOnAnnotation(UnresolvedType.AT_TARGET);
			if (atTargetAnnotation == null) {
				supportedTargets = Collections.emptySet();
			} else {
				supportedTargets = atTargetAnnotation.getTargets();
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public final String getValidTargets() {
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		for (Iterator<String> iter = supportedTargets.iterator(); iter.hasNext();) {
			String evalue = iter.next();
			sb.append(evalue);
			if (iter.hasNext()) {
				sb.append(",");
			}
		}
		sb.append("}");
		return sb.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	public final boolean specifiesTarget() {
		ensureAtTargetInitialized();
		return !supportedTargets.isEmpty();
	}

	/**
	 * Helper method to retrieve an annotation on an annotation e.g. retrieveAnnotationOnAnnotation(UnresolvedType.AT_TARGET)
	 */
	private final AnnotationAJ retrieveAnnotationOnAnnotation(UnresolvedType requiredAnnotationSignature) {
		AnnotationAJ[] annos = type.getAnnotations();
		for (AnnotationAJ a : annos) {
			if (a.getTypeSignature().equals(requiredAnnotationSignature.getSignature())) {
				return a;
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public abstract boolean isRuntimeVisible();

	/**
	 * {@inheritDoc}
	 */
	public abstract Set<String> getTargets();

	/**
	 * {@inheritDoc}
	 */
	public abstract boolean hasNameValuePair(String name, String value);

	/**
	 * {@inheritDoc}
	 */
	public abstract boolean hasNamedValue(String name);

	/**
	 * {@inheritDoc}
	 */
	public abstract String stringify();

}