Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 Walter Harley and others |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* eclipse@cafewalter.com - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.jdt.apt.pluggable.tests.processors.buildertester; |
13 |
|
14 |
import java.io.IOException; |
15 |
import java.io.PrintWriter; |
16 |
import java.util.Set; |
17 |
|
18 |
import javax.annotation.processing.AbstractProcessor; |
19 |
import javax.annotation.processing.RoundEnvironment; |
20 |
import javax.annotation.processing.SupportedAnnotationTypes; |
21 |
import javax.annotation.processing.SupportedOptions; |
22 |
import javax.annotation.processing.SupportedSourceVersion; |
23 |
import javax.lang.model.SourceVersion; |
24 |
import javax.lang.model.element.TypeElement; |
25 |
import javax.tools.Diagnostic.Kind; |
26 |
import javax.tools.JavaFileObject; |
27 |
|
28 |
import org.eclipse.jdt.apt.pluggable.tests.annotations.FinalRoundTestTrigger; |
29 |
|
30 |
/** |
31 |
* Do nothing when first triggered; then, in the final round, generate a |
32 |
* new type that is annotated with {@link FinalRoundTestTrigger}. |
33 |
* Used to verify that a new type generated in the final round does not get |
34 |
* annotations processed, but does get compiled. The JSR269 spec is somewhat |
35 |
* vague about whether it should be possible to generate a new type during |
36 |
* the final round (since the final round does not happen until after a |
37 |
* round in which no new types are generated); but apparently javac behaves |
38 |
* this way. |
39 |
* <p> |
40 |
* See <a href="http://bugs.eclipse.org/329156">Bug 329156</a> and <a |
41 |
* href="http://bugs.sun.com/view_bug.do?bug_id=6634138">the corresponding |
42 |
* bug in javac</a>, which Sun fixed. |
43 |
*/ |
44 |
@SupportedAnnotationTypes( { "org.eclipse.jdt.apt.pluggable.tests.annotations.FinalRoundTestTrigger" }) |
45 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
46 |
@SupportedOptions( {}) |
47 |
public class TestFinalRoundProc extends AbstractProcessor { |
48 |
private static int _numRounds; |
49 |
|
50 |
public static int getNumRounds() { |
51 |
return _numRounds; |
52 |
} |
53 |
|
54 |
public static void resetNumRounds() { |
55 |
_numRounds = 0; |
56 |
} |
57 |
|
58 |
@Override |
59 |
public boolean process(Set<? extends TypeElement> annotations, |
60 |
RoundEnvironment roundEnv) { |
61 |
if (roundEnv.processingOver()) { |
62 |
createFile(); |
63 |
} |
64 |
_numRounds++; |
65 |
return true; |
66 |
} |
67 |
|
68 |
private void createFile() { |
69 |
PrintWriter pw = null; |
70 |
try { |
71 |
JavaFileObject jfo = processingEnv.getFiler().createSourceFile("g.FinalRoundGen"); |
72 |
pw = new PrintWriter(jfo.openWriter()); |
73 |
pw.println("package g;"); |
74 |
pw.println("import org.eclipse.jdt.apt.pluggable.tests.annotations.FinalRoundTestTrigger;"); |
75 |
pw.println("@FinalRoundTestTrigger"); |
76 |
pw.println("public class FinalRoundGen {}"); |
77 |
} catch (IOException e) { |
78 |
e.printStackTrace(); |
79 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Unable to create source file! Exception message was: " + e.getMessage()); |
80 |
} finally { |
81 |
if (pw != null) { |
82 |
pw.close(); |
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
} |