Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62d79fa51df2834455eb26cec23abd6d52b9cd46 (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
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.apt.pluggable.tests.processors.buildertester;

import java.io.IOException;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
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.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

@SupportedAnnotationTypes("targets.bug387956.Generate")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Bug387956Processor extends AbstractProcessor {
	
	RoundEnvironment roundEnv = null;

	@Override
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

		for (TypeElement annotation : annotations) {
			Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
			for (Element annotatedElement : annotatedElements) {
				Filer filer = processingEnv.getFiler();
				String generatedClassSimpleName = "Generated" + annotatedElement.getSimpleName().toString();
				try {
					JavaFileObject file = filer.createSourceFile("generated/" + generatedClassSimpleName, annotatedElement);
					file.openWriter() //
							.append("package generated;\n" //
									+ "\n" //
									+ "public class " + generatedClassSimpleName + " {\n" //
									+ "\n" //
									+ "}\n") //
							.close();
				} catch (IOException e) {
					Messager messager = processingEnv.getMessager();
					messager.printMessage(Diagnostic.Kind.ERROR, "IOException: " + e);
				}
			}
		}
		return true;
	}
}

Back to the top