Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4cb82deffff28f205c83915eefe9e3791142a6d7 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation and others.
 * 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
 *
 * Contributors:
 *     het@google.com - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.compiler.apt.tests.processors.annotationmirror;

import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;

/**
 * A processor that tests that the methods for {@link AnnotationMirror} work as
 * expected.
 */
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class AnnotationMirrorProc extends BaseProcessor {
	@Override
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
		if (roundEnv.processingOver()) {
			// We're not interested in the postprocessing round.
			return false;
		}
		Map<String, String> options = processingEnv.getOptions();
		if (!options.containsKey(this.getClass().getName())) {
			// Disable this processor unless we are intentionally performing the test.
			return false;
		}

		if (!examineToString()) {
			return false;
		}

		if (!examineEscaping()) {
			return false;
		}

		reportSuccess();
		return false;
	}

	/**
	 * Tests that the {@link AnnotationValue#toString()} returns a
	 * {@link String} that is "in a form suitable for representing this value in
	 * the source code of an annotation."
	 * @return true if all tests pass
	 */
	private boolean examineToString() {
		TypeElement annotatedElement = _elementUtils.getTypeElement("targets.model.pc.AnnotatedWithManyTypes.Annotated");
		if (null == annotatedElement || annotatedElement.getKind() != ElementKind.CLASS) {
			reportError("examineToString: couldn't get AnnotatedWithManyTypes.Annotated element");
			return false;
		}
		final String badValue = "examineToString: unexpected value for ";
		List<? extends AnnotationMirror> annoMirrors = annotatedElement.getAnnotationMirrors();
		AnnotationMirror annoByte = annoMirrors.get(0);
		if (null == annoByte || !annoByte.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoByte(value = 3)")) {
			reportError(badValue + "AnnoByte");
			return false;
		}
		AnnotationMirror annoBoolean = annoMirrors.get(1);
		if (null == annoBoolean || !annoBoolean.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoBoolean(value = true)")) {
			reportError(badValue + "AnnoBoolean");
			return false;
		}
		AnnotationMirror annoChar = annoMirrors.get(2);
		if (null == annoChar || !annoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'c')")) {
			reportError(badValue + "AnnoChar");
			return false;
		}
		AnnotationMirror annoDouble = annoMirrors.get(3);
		if (null == annoDouble || !annoDouble.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoDouble(value = 6.3)")) {
			reportError(badValue + "AnnoDouble");
			return false;
		}
		AnnotationMirror annoFloat = annoMirrors.get(4);
		if (null == annoFloat || !annoFloat.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoFloat(value = 26.7)")) {
			reportError(badValue + "AnnoFloat");
			return false;
		}
		AnnotationMirror annoInt = annoMirrors.get(5);
		if (null == annoInt || !annoInt.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoInt(value = 19)")) {
			reportError(badValue + "AnnoInt");
			return false;
		}
		AnnotationMirror annoLong = annoMirrors.get(6);
		if (null == annoLong || !annoLong.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoLong(value = 300)")) {
			reportError(badValue + "AnnoLong");
			return false;
		}
		AnnotationMirror annoShort = annoMirrors.get(7);
		if (null == annoShort || !annoShort.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoShort(value = 289)")) {
			reportError(badValue + "AnnoShort");
			return false;
		}
		AnnotationMirror annoString = annoMirrors.get(8);
		if (null == annoString || !annoString.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoString(value = \"foo\")")) {
			reportError(badValue + "AnnoString");
			return false;
		}
		AnnotationMirror annoEnumConst = annoMirrors.get(9);
		if (null == annoEnumConst || !annoEnumConst.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoEnumConst(value = org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.Enum.A)")) {
			reportError(badValue + "AnnoEnumConst");
			return false;
		}
		AnnotationMirror annoType = annoMirrors.get(10);
		if (null == annoType || !annoType.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoType(value = java.lang.Exception.class)")) {
			reportError(badValue + "AnnoType");
			return false;
		}
		AnnotationMirror annoAnnoChar = annoMirrors.get(11);
		if (null == annoAnnoChar || !annoAnnoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoAnnoChar(value = @org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'x'))")) {
			reportError(badValue + "AnnoAnnoChar");
			return false;
		}
		AnnotationMirror annoArrayInt = annoMirrors.get(12);
		if (null == annoArrayInt || !annoArrayInt.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayInt(value = {5, 8, 11})")) {
			reportError(badValue + "AnnoArrayInt");
			return false;
		}
		AnnotationMirror annoArrayString = annoMirrors.get(13);
		if (null == annoArrayString || !annoArrayString.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayString(value = {\"bar\", \"quux\"})")) {
			reportError(badValue + "AnnoArrayString");
			return false;
		}
		AnnotationMirror annoArrayEnumConst = annoMirrors.get(14);
		if (null == annoArrayEnumConst || !annoArrayEnumConst.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayEnumConst(value = {org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.Enum.B, org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.Enum.C})")) {
			reportError(badValue + "AnnoArrayEnumConst");
			return false;
		}
		AnnotationMirror annoArrayType = annoMirrors.get(15);
		if (null == annoArrayType || !annoArrayType.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayType(value = {java.lang.String.class, targets.model.pc.AnnotatedWithManyTypes.Annotated.class})")) {
			reportError(badValue + "AnnoArrayType");
			return false;
		}
		AnnotationMirror annoArrayAnnoChar = annoMirrors.get(16);
		if (null == annoArrayAnnoChar || !annoArrayAnnoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayAnnoChar(value = {@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'y'), @org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'z')})")) {
			reportError(badValue + "AnnoArrayAnnoChar");
			return false;
		}
		return true;
	}

	private boolean examineEscaping() {
		TypeElement annotatedElement = _elementUtils.getTypeElement("targets.model.pc.K");
		if (null == annotatedElement || annotatedElement.getKind() != ElementKind.CLASS) {
			reportError("examineEscaping: couldn't get K element");
			return false;
		}
		final String badValue = "examineEscaping: unexpected value for ";
		List<? extends AnnotationMirror> annoMirrors = annotatedElement.getAnnotationMirrors();
		AnnotationMirror annoString = annoMirrors.get(0);
		if (null == annoString || !annoString.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoString(value = \"I'm \\\"special\\\": \\t\\\\\\n\")")) {
			reportError(badValue + "AnnoString");
			return false;
		}
		AnnotationMirror annoChar = annoMirrors.get(1);
		if (null == annoChar || !annoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = '\\'')")) {
			reportError(badValue + "AnnoChar");
			return false;
		}
		return true;
	}
}

Back to the top