2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.j2ee.metadata.model.api.support.annotation;
44
import java.io.PrintStream;
45
import java.util.ArrayList;
46
import java.util.EnumSet;
47
import java.util.HashMap;
48
import java.util.HashSet;
49
import java.util.List;
52
import java.util.concurrent.Callable;
53
import java.util.logging.Level;
54
import javax.lang.model.element.AnnotationMirror;
55
import javax.lang.model.element.Element;
56
import javax.lang.model.element.ElementKind;
57
import javax.lang.model.element.TypeElement;
58
import org.netbeans.api.java.source.ClasspathInfo;
59
import org.netbeans.api.java.source.ElementHandle;
60
import org.netbeans.modules.j2ee.metadata.model.support.TestUtilities;
61
import org.netbeans.modules.j2ee.metadata.model.support.PersistenceTestCase;
62
import org.netbeans.modules.java.source.usages.RepositoryUpdater;
63
import org.openide.util.MapFormat;
67
* @author Andrei Badea
69
public class AnnotationScannerTest extends PersistenceTestCase {
71
public AnnotationScannerTest(String testName) {
75
protected Level logLevel() {
80
public PrintStream getLog() {
84
public void testScanTypes() throws Exception {
85
TestUtilities.copyStringToFileObject(srcFO, "Customer.java",
86
"@javax.persistence.Entity()" +
87
"public class Customer { }");
88
TestUtilities.copyStringToFileObject(srcFO, "Foo.java",
89
"public class Foo { }");
90
TestUtilities.copyStringToFileObject(srcFO, "Item.java",
91
"@javax.persistence.Entity()" +
92
"public class Item { }");
93
RepositoryUpdater.getDefault().scheduleCompilationAndWait(srcFO, srcFO).await();
94
ClasspathInfo cpi = ClasspathInfo.create(srcFO);
95
final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
96
final Set<String> types = new HashSet<String>();
97
helper.runJavaSourceTask(new Callable<Void>() {
98
public Void call() throws InterruptedException {
99
helper.getAnnotationScanner().findAnnotations("javax.persistence.Entity", AnnotationScanner.TYPE_KINDS, new AnnotationHandler() {
100
public void handleAnnotation(TypeElement type, Element element, AnnotationMirror annotation) {
101
types.add(type.getQualifiedName().toString());
107
assertEquals(2, types.size());
108
assertTrue(types.contains("Customer"));
109
assertTrue(types.contains("Item"));
112
public void testPerformance() throws Exception {
113
// Logger.getLogger(AnnotationScanner.class.getName()).setLevel(Level.FINEST);
114
final int ENTITY_COUNT = 500;
115
String template = TestUtilities.copyStreamToString(getClass().getResourceAsStream("Table.javax"));
116
Map<String, String> args = new HashMap<String, String>();
117
MapFormat format = new MapFormat(args);
118
format.setLeftBrace("__");
119
format.setRightBrace("__");
120
for (int i = 0; i < ENTITY_COUNT; i++) {
121
String name = "Table" + i;
122
args.put("NUM", Integer.toString(i));
123
args.put("NAME", name);
124
String contents = format.format(template);
125
TestUtilities.copyStringToFileObject(srcFO, name + ".java", contents);
127
long startTime = System.nanoTime();
128
RepositoryUpdater.getDefault().scheduleCompilationAndWait(srcFO, srcFO).await();
129
long compilationDoneTime = System.nanoTime();
130
ClasspathInfo cpi = ClasspathInfo.create(srcFO);
131
final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
132
final int[] entityCount = { 0 };
133
final long initialScanDoneTime[] = { 0L };
134
final List<ElementHandle<TypeElement>> typeHandles = new ArrayList<ElementHandle<TypeElement>>();
135
helper.runJavaSourceTask(new Callable<Void>() {
136
public Void call() throws InterruptedException {
137
helper.getAnnotationScanner().findAnnotations("javax.persistence.Entity", AnnotationScanner.TYPE_KINDS, new AnnotationHandler() {
138
public void handleAnnotation(TypeElement type, Element element, AnnotationMirror annotation) {
139
typeHandles.add(ElementHandle.create(type));
143
initialScanDoneTime[0] = System.nanoTime();
147
assertEquals(ENTITY_COUNT, entityCount[0]);
148
System.out.println("Compilation time (ms): " + (compilationDoneTime - startTime) / 1e6);
149
System.out.println("Initial annotation scan time (ms): " + (initialScanDoneTime[0] - compilationDoneTime) / 1e6);
152
private ClasspathInfo createClasspathInfoForScanningAnnotations() throws Exception {
153
TestUtilities.copyStringToFileObject(srcFO, "foo/MyClass.java",
156
"import javax.annotation.Resource;" +
157
"import javax.sql.DataSource;" +
159
"@Resource(name=\"myClass\")" +
160
"public class MyClass {" +
162
" private DataSource myDS;" +
164
TestUtilities.copyStringToFileObject(srcFO, "foo/YourClass.java",
167
"import javax.annotation.Resource;" +
168
"import javax.sql.DataSource;" +
170
"public class YourClass {" +
172
" private void setYourDataSource(DataSource ds) {" +
175
RepositoryUpdater.getDefault().scheduleCompilationAndWait(srcFO, srcFO).await();
176
return ClasspathInfo.create(srcFO);
179
public void testScanAnnotationsOnClassesMethodsFields() throws Exception {
180
final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
181
final Set<String> elements = new HashSet<String>();
183
helper.runJavaSourceTask(new Callable<Void>() {
184
public Void call() throws InterruptedException{
185
helper.getAnnotationScanner().findAnnotations(
186
"javax.annotation.Resource",
187
EnumSet.of(ElementKind.CLASS, ElementKind.METHOD, ElementKind.FIELD),
188
new AnnotationHandler() {
189
public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
190
elements.add(element.getSimpleName().toString());
196
assertEquals(3, elements.size());
197
assertTrue(elements.contains("MyClass"));
198
assertTrue(elements.contains("myDS"));
199
assertTrue(elements.contains("setYourDataSource"));
202
public void testScanAnnotationsOnMethodsFields() throws Exception {
203
final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
204
final Set<String> elements = new HashSet<String>();
206
helper.runJavaSourceTask(new Callable<Void>() {
207
public Void call() throws InterruptedException {
208
helper.getAnnotationScanner().findAnnotations(
209
"javax.annotation.Resource",
210
EnumSet.of(ElementKind.METHOD, ElementKind.FIELD),
211
new AnnotationHandler() {
212
public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
213
elements.add(element.getSimpleName().toString());
219
assertEquals(2, elements.size());
220
assertTrue(elements.contains("myDS"));
221
assertTrue(elements.contains("setYourDataSource"));
224
public void testScanAnnotationsOnClasses() throws Exception {
225
final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
226
final Set<String> elements = new HashSet<String>();
228
helper.runJavaSourceTask(new Callable<Void>() {
229
public Void call() throws InterruptedException {
230
helper.getAnnotationScanner().findAnnotations(
231
"javax.annotation.Resource",
232
EnumSet.of(ElementKind.CLASS),
233
new AnnotationHandler() {
234
public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
235
assertSame(typeElement, element);
236
elements.add(element.getSimpleName().toString());
242
assertEquals(1, elements.size());
243
assertTrue(elements.contains("MyClass"));
246
public void testScanAnnotationsOnMethods() throws Exception {
247
final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
248
final Set<String> elements = new HashSet<String>();
250
helper.runJavaSourceTask(new Callable<Void>() {
251
public Void call() throws InterruptedException {
252
helper.getAnnotationScanner().findAnnotations(
253
"javax.annotation.Resource",
254
EnumSet.of(ElementKind.METHOD),
255
new AnnotationHandler() {
256
public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
257
assertEquals("foo.YourClass", typeElement.getQualifiedName().toString());
258
elements.add(element.getSimpleName().toString());
264
assertEquals(1, elements.size());
265
assertTrue(elements.contains("setYourDataSource"));
268
public void testScanAnnotationsOnFields() throws Exception {
269
final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
270
final Set<String> elements = new HashSet<String>();
272
helper.runJavaSourceTask(new Callable<Void>() {
273
public Void call() throws InterruptedException {
274
helper.getAnnotationScanner().findAnnotations(
275
"javax.annotation.Resource",
276
EnumSet.of(ElementKind.FIELD),
277
new AnnotationHandler() {
278
public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
279
assertEquals("foo.MyClass", typeElement.getQualifiedName().toString());
280
elements.add(element.getSimpleName().toString());
286
assertEquals(1, elements.size());
287
assertTrue(elements.contains("myDS"));