Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 32a244127638075a5b2326ac2e4ea674b19fc6a7 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 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:
 *   wharley - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.apt.tests.external.annotations.loadertest;

import java.io.IOException;
import java.io.PrintWriter;

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.Filer;

/**
 * Used to test loading an annotation processor from a jar file.
 */
public class LoaderTestAnnotationProcessor implements AnnotationProcessor {
	
	private static String getClassName() {
		return getPackageName() + "." + getTypeName(); //$NON-NLS-1$
	}
	
	private final AnnotationProcessorEnvironment _env;

	/**
	 * Has an instance of this class been constructed in this VM?
	 * This is implemented in such a way that the answer does not
	 * depend on sharing a classloader.
	 */
	public static boolean isLoaded() {
		return "loaded".equals(System.getProperty(getClassName())); //$NON-NLS-1$
	}

	/**
	 * Clear the "isLoaded" setting.  After this, isLoaded() will
	 * return false until the next time an instance is constructed.
	 */
	public static void clearLoaded() {
		System.clearProperty(getClassName());
	}

	public LoaderTestAnnotationProcessor(AnnotationProcessorEnvironment env) {
		System.setProperty(getClassName(), "loaded"); //$NON-NLS-1$
		_env = env;
	}

	public void process() {
		try
		{
			Filer f = getEnvironment().getFiler();
			PrintWriter pw = f
				.createSourceFile( getClassName() );
			pw.print( getCode() );
			pw.close();
		}
		catch( IOException ioe )
		{
			ioe.printStackTrace();
		}
	}

	public AnnotationProcessorEnvironment getEnvironment()
	{
		return _env;
	}
	
	protected static String getCode() {
		return CODE; 
	}
	
	protected static String getPackageName() {
		return "generatedfilepackage";  //$NON-NLS-1$
	}
	
	protected static String getTypeName() { 
		return "LoadFactoryFromJarTest";  //$NON-NLS-1$
	}

	protected final static String CODE			= 
		"package " + getPackageName() + ";\n" +
		"public class "	+ getTypeName()	+ "\n"	+ 
		"{\n" +
		"    public static void helloWorld() {\n" +
		"        System.out.println( \"Hello, world!  I am a generated file!\" ); \n" +
		"    }\n" +
		"}\n";
}

Back to the top