Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e625a672af20be4f8417e05432c89d6c6fd799dd (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 BEA Systems, Inc. 
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    mkaufman@bea.com - initial API and implementation
 *    
 *******************************************************************************/

package org.eclipse.jdt.apt.tests.annotations.helloworld;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import org.eclipse.jdt.apt.tests.annotations.BaseProcessor;
import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;

import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.Filer;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.Declaration;

/**
 * A processor that looks for HelloWorldAnnotation, and in response
 * generates a type named in the value of the annotation, containing code
 * specified in getCode().
 */
public class HelloWorldAnnotationProcessor extends BaseProcessor {

	private final static String PACKAGENAME = "generatedfilepackage"; //$NON-NLS-1$

	public HelloWorldAnnotationProcessor(AnnotationProcessorEnvironment env) {
		super(env);
	}

	public void process()
	{
		ProcessorTestStatus.setProcessorRan();
		Filer f = _env.getFiler();
		AnnotationTypeDeclaration annoDecl = (AnnotationTypeDeclaration) _env.getTypeDeclaration(HelloWorldAnnotation.class.getName());
		Collection<Declaration> annotatedDecls = _env.getDeclarationsAnnotatedWith(annoDecl);
		try {
			for (Declaration annotatedDecl : annotatedDecls) {
				String typeName = getTypeName(annotatedDecl);
				PrintWriter writer = f.createSourceFile(
						PACKAGENAME + "." + typeName);
				writer.print(getCode(typeName));
				writer.close();
			}
			reportSuccess(this.getClass());
		}
		catch (NullPointerException npe) {
			reportError(this.getClass(), "Could not read annotation in order to generate text file");
		}
		catch (IOException ioe) {
			reportError(this.getClass(), "Could not generate text file due to IOException");
		}
	}

	private String getTypeName(Declaration annotatedDecl) { 
		HelloWorldAnnotation tganno = annotatedDecl.getAnnotation(HelloWorldAnnotation.class);
		return tganno.value();
	}

	private String getCode(String typeName) { 
		return "package " + PACKAGENAME + ";" + "\n"
		+ "public class "+ typeName	+ "\n"
		+ "{" + "\n"
		+ "    public static void helloWorld()"	+ "\n"
		+ "    {" + "\n"
		+ "        System.out.println( \"Hello, world!  I am a generated file!\" ); " + "\n" 
		+ "    }" + "\n"
		+ "}"; 
	}
	
}

Back to the top