Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 70bc7355e6b45600b641498809e63d7e08194930 (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
/*******************************************************************************
 * Copyright (c) 2005 BEA Systems, Inc.
 * 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:
 *    tyeung@bea.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.apt.tests.external.annotations.batch;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.AnnotationProcessorFactory;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;

public class BatchGenAnnotationFactory implements AnnotationProcessorFactory {
	
	private static int ROUND = 0;
	private static final List<String> SUPPORTED_TYPES = 
		Collections.singletonList(BatchGen.class.getName());
	
	public AnnotationProcessor getProcessorFor(
			Set<AnnotationTypeDeclaration> decls, 
			AnnotationProcessorEnvironment env) {
		if( ROUND == 0 ){
			ROUND ++;
			return new BatchGen0AnnotationProcessor(env);
		}
		else if( ROUND == 1){
			ROUND ++;
			if( !decls.isEmpty() )
				env.getMessager().printError("Expecting empty set but got " + decls );
				
			return new BatchGen1AnnotationProcessor(env);
		}
		else if( ROUND == 2 ){ // NO-OP
			env.getMessager().printError("Called the third time.");
			return null; 
		}
		// This is to make sure we aren't bouncing the class loader without a full build.
		else
			env.getMessager().printError("Calling BatchGenAnnotionFactory too many times. Round=" + ROUND );
		return null;
	}
	
	public Collection<String> supportedAnnotationTypes() {
		return SUPPORTED_TYPES;
	}
	public Collection<String> supportedOptions() {
		return Collections.emptyList();
	}
	
	static class BatchGen0AnnotationProcessor implements AnnotationProcessor {
		
		final AnnotationProcessorEnvironment _env;
		BatchGen0AnnotationProcessor(AnnotationProcessorEnvironment env){
			_env = env;
		}
		public void process() {
			// a generated file will cause BatchGenAnnotationFactory to be 
			// called again.
			try{
				final PrintWriter writer = _env.getFiler().createSourceFile("gen.Class0");
				writer.print("package gen;\n");
				writer.print("public class Class0{}");
				writer.close();
			}
			catch(IOException e){
				_env.getMessager().printError(e.getMessage());
			}
		}
	}
	
	static class BatchGen1AnnotationProcessor implements AnnotationProcessor {
		final AnnotationProcessorEnvironment _env;
		BatchGen1AnnotationProcessor(AnnotationProcessorEnvironment env){
			_env = env;
		}
		public void process(){
			try{
				final PrintWriter writer = _env.getFiler().createSourceFile("gen.Class1");
				writer.print("package gen;\n");
				writer.print("public class Class1{}");
				writer.close();
			}
			catch(IOException e){
				_env.getMessager().printError(e.getMessage());
			}
		}
	}
}

Back to the top