Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 78a01ac78ddcf497de22d6cfb8cf1d41be461218 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2005, 2008 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
 *******************************************************************************/
package org.eclipse.jdt.apt.tests.annotations.mirrortest;

import java.util.Collection;
import java.util.Set;

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

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.ConstructorDeclaration;
import com.sun.mirror.declaration.FieldDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;

public class ASTBasedMirrorDeclarationProcessorFactory
	extends BaseFactory
{
	public ASTBasedMirrorDeclarationProcessorFactory(){
		super("test.DeclarationAnno");
	}
	
	public AnnotationProcessor getProcessorFor(
			Set<AnnotationTypeDeclaration> arg0, 
			AnnotationProcessorEnvironment env) {
		return new ASTBasedMirrorDeclarationProcessor(env);
	}
	
	public static final class ASTBasedMirrorDeclarationProcessor
		extends BaseProcessor{
		
		public ASTBasedMirrorDeclarationProcessor(AnnotationProcessorEnvironment env){
			super(env);
		}
		public void process() {
			final Collection<TypeDeclaration> typeDecls = _env.getTypeDeclarations();
			boolean done = false;
			for( TypeDeclaration typeDecl : typeDecls ){
				if(typeDecl instanceof ClassDeclaration ){
					examineClass( (ClassDeclaration)typeDecl );
					if( typeDecl.getQualifiedName().equals("test.Foo") )
						done = true;
				}
			}
			// if the current file does not contain "test.Foo",
			// look for it by name and run the same tests.
			if( !done ){
				final ClassDeclaration fooDecl = (ClassDeclaration)_env.getTypeDeclaration("test.Foo");
				examineClass(fooDecl);
				final ClassDeclaration innerTypeDecl = (ClassDeclaration)_env.getTypeDeclaration("test.Foo$Inner");
				examineClass(innerTypeDecl);
			}
		}
	
		private void examineClass(ClassDeclaration typeDecl ){
			
			assertNonNull(typeDecl, "missing type declaration");
			if( typeDecl != null ){				
				final String typename = typeDecl.getSimpleName();
				if(typename.equals("Foo")){
					final Collection<? extends MethodDeclaration> methods = typeDecl.getMethods();
					assertEqual(2, methods.size(), "number of methods do not match ");
					for(MethodDeclaration method : methods ){
						final String name = method.getSimpleName();
						if( name.equals("getType") ){
							final String methodString = method.toString();
							final String expectedString = "UnknownType getType()";
							assertEqual(expectedString, methodString, "signature mismatch");
						}
						else if( name.equals("voidMethod")){
							final String methodString = method.toString();
							final String expectedString = "void voidMethod()";
							assertEqual(expectedString, methodString, "signature mismatch");
						}
						else{
							assertEqual(null, method.toString(), "unexpected method");
						}
					}
					
					final Collection<ConstructorDeclaration> constructors = 
						typeDecl.getConstructors();
					assertEqual(1, constructors.size(), "number of constructors do not match");
					for( ConstructorDeclaration constructor : constructors ){
						final String constructorString = constructor.toString();
						final String expectedString = "Foo(UnknownType type)";
						assertEqual(expectedString, constructorString, "signature mismatch");
					}
					final Collection<FieldDeclaration> fields = 
						typeDecl.getFields();
					assertEqual(2, fields.size(), "number of fields do not match");
					for( FieldDeclaration field : fields ){
						final String name = field.getSimpleName();
						if( "field0".equals(name) || "field1".equals(name) ){
							continue;
						}
						assertEqual(null, name, "unexpected field");
					}
				}
				else if(typename.equals("Inner")){
					final Collection<? extends MethodDeclaration> methods = 
						typeDecl.getMethods();
					assertEqual(0, methods.size(), "number of methods do not match ");
					
					final Collection<ConstructorDeclaration> constructors = 
						typeDecl.getConstructors();
					assertEqual(1, constructors.size(), "number of constructors do not match");
					for( ConstructorDeclaration constructor : constructors ){
						final String constructorString = constructor.toString();
						final String expectedString = "Inner()";
						assertEqual(expectedString, constructorString, "signature mismatch");
					}
					final Collection<FieldDeclaration> fields = 
						typeDecl.getFields();
					assertEqual(0, fields.size(), "number of fields do not match");
				}
			}
		}
	}
}

Back to the top