Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53f08a4be1272b525e6f204e20100c6fece47902 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2005, 2017 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.annotations.apitest;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

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.apt.Messager;
import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
import com.sun.mirror.declaration.AnnotationValue;
import com.sun.mirror.declaration.Declaration;
import com.sun.mirror.declaration.FieldDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.declaration.TypeParameterDeclaration;
import com.sun.mirror.type.DeclaredType;
import com.sun.mirror.type.TypeMirror;
import com.sun.mirror.util.Types;


public class APIAnnotationProcessorFactory extends BaseFactory {
	
	public APIAnnotationProcessorFactory(){
		super(Common.class.getName(), SubtypeOf.class.getName(), AssignableTo.class.getName());
	}
	
	public AnnotationProcessor getProcessorFor(
			Set<AnnotationTypeDeclaration> decls, 
			AnnotationProcessorEnvironment env) {
		return new APIAnnotationProcessor(env);
	}
	
	public static class APIAnnotationProcessor extends BaseProcessor{
		
		private Messager _msgr;
		private Types _types;
		
		public APIAnnotationProcessor(AnnotationProcessorEnvironment env){
			super(env);
		}
		
		public void process() {	
			_msgr = _env.getMessager();
			_types = _env.getTypeUtils();
			checkCommon();
			checkSubtypeOf();
			checkAssignableTo();
		}

		/**
		 * validate instances of the Common annotation
		 */
		private void checkCommon()
		{
			final AnnotationTypeDeclaration commonAnnoType = 
				(AnnotationTypeDeclaration)_env.getTypeDeclaration(Common.class.getName());
			final Collection<Declaration> decls = 
				_env.getDeclarationsAnnotatedWith(commonAnnoType);
			for( Declaration decl : decls ){
				if(decl instanceof FieldDeclaration ){
					final FieldDeclaration field = (FieldDeclaration)decl;
					final TypeMirror type = field.getType();
					if( type instanceof DeclaredType ){
						final TypeMirror collectionType =
							_env.getTypeUtils().getDeclaredType(_env.getTypeDeclaration(Collection.class.getName()));
						final Collection<TypeMirror> typeVars =
							((DeclaredType)type).getActualTypeArguments();
						if(typeVars.size() == 1 ){
							TypeMirror typeVar = typeVars.iterator().next(); 
							boolean assignable = _env.getTypeUtils().isAssignable(typeVar, collectionType);							
							if( assignable )
								_msgr.printError(typeVar + " is assignable to " + collectionType );
							else
								_msgr.printError(typeVar + " is not assignable to " + collectionType );
						}
					}
				}else if(decl instanceof TypeDeclaration){
					final TypeDeclaration typeDecl = (TypeDeclaration)decl;
					final Collection<TypeParameterDeclaration> typeParams =
						typeDecl.getFormalTypeParameters();					
					for(TypeParameterDeclaration typeParam : typeParams){
						Declaration owner = typeParam.getOwner();
						_msgr.printError("Type parameter '" + typeParam + "' belongs to " + owner.getClass().getName() + " " + owner.getSimpleName() );
					}
				}
				else if( decl instanceof MethodDeclaration ){
					final MethodDeclaration methodDecl = (MethodDeclaration)decl;
					final Collection<TypeParameterDeclaration> typeParams =
						methodDecl.getFormalTypeParameters();					
					for(TypeParameterDeclaration typeParam : typeParams){
						Declaration owner = typeParam.getOwner();
						_msgr.printError("Type parameter '" + typeParam + "' belongs to " + owner.getClass().getName() + " " + owner.getSimpleName() );
					}
				}
			}
		}
		
		/**
		 * Validate all the fields annotated with @SubtypeOf, in order to test
		 * the Types.subtypeOf() method.
		 * We ignore anything but fields, out of laziness.
		 */
		private void checkSubtypeOf() {
			final AnnotationTypeDeclaration annoType = 
				(AnnotationTypeDeclaration)_env.getTypeDeclaration(SubtypeOf.class.getName());
			final Collection<Declaration> decls = 
				_env.getDeclarationsAnnotatedWith(annoType);
			for( Declaration decl : decls ){
				if(decl instanceof FieldDeclaration ) {
					AnnotationMirror mirror = findMirror(decl, annoType);
					if (mirror == null) {
						return;
					}
					TypeMirror valueType = getTypeValue(mirror);
					final FieldDeclaration field = (FieldDeclaration)decl;
					final TypeMirror fieldType = field.getType();
					boolean isSubtype = _types.isSubtype(fieldType, valueType);
					if( isSubtype )
						_msgr.printError(fieldType + " is a subtype of " + valueType );
					else
						_msgr.printError(fieldType + " is not a subtype of " + valueType );
				}
			}
		}
		
		/**
		 * Validate all the fields annotated with @AssignableTo.
		 * We ignore anything but fields, out of laziness.
		 */
		private void checkAssignableTo() {
			final AnnotationTypeDeclaration annoType = 
				(AnnotationTypeDeclaration)_env.getTypeDeclaration(AssignableTo.class.getName());
			final Collection<Declaration> decls = 
				_env.getDeclarationsAnnotatedWith(annoType);
			for( Declaration decl : decls ){
				if(decl instanceof FieldDeclaration ) {
					AnnotationMirror mirror = findMirror(decl, annoType);
					if (mirror == null) {
						return;
					}
					TypeMirror valueType = getTypeValue(mirror);
					final FieldDeclaration field = (FieldDeclaration)decl;
					final TypeMirror fieldType = field.getType();
					boolean isAssignableTo = _types.isAssignable(fieldType, valueType);
					if( isAssignableTo )
						_msgr.printError(fieldType + " is assignable to " + valueType );
					else
						_msgr.printError(fieldType + " is not assignable to " + valueType );
				}
			}
		}
		
		/**
		 * @return a mirror for the instance of the specified annotation on the specified
		 * declaration, or null if one is not present.
		 */
		private AnnotationMirror findMirror(Declaration decl, AnnotationTypeDeclaration at) {
			for (AnnotationMirror mirror : decl.getAnnotationMirrors()) {
				if (mirror.getAnnotationType().getDeclaration().equals(at)) {
					return mirror;
				}
			}
			return null;
		}
		
		/**
		 * @return the value() of an annotation instance <code>mirror</code>, if it is a
		 * class value, or null if not.
		 */
		private TypeMirror getTypeValue(AnnotationMirror mirror) {
			Map<AnnotationTypeElementDeclaration, AnnotationValue> values = mirror.getElementValues();
			for (Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : values.entrySet()) {
				if ("value".equals(entry.getKey().getSimpleName())) {
					if (entry.getValue().getValue() instanceof TypeMirror)
						return (TypeMirror)entry.getValue().getValue();
					else
						return null;
				}
			}
			return null;
		}
	}
}

Back to the top