Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93a680078a7bd795780594ab8156662251aed1b9 (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
/*******************************************************************************
 * Copyright (c) 2005 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:
 *    tyeung@bea.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.apt.tests.annotations.mirrortest;

import java.util.Collection;

import org.eclipse.jdt.apt.tests.annotations.BaseProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.Messager;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
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.type.DeclaredType;

public class DefaultConstantProcessor extends BaseProcessor {
	public DefaultConstantProcessor(AnnotationProcessorEnvironment env){
		super(env);
	}
	public void process() {
		final AnnotationTypeDeclaration trigger = (AnnotationTypeDeclaration)_env.getTypeDeclaration("test.Trigger");
		
		final Messager msger = _env.getMessager();
		if( trigger == null)
			msger.printError("cannot find test.Trigger");
		
		final Collection<Declaration> decls = _env.getDeclarationsAnnotatedWith(trigger);
		for(Declaration decl : decls ){
			if( decl instanceof TypeDeclaration ){
				final TypeDeclaration typeDecl = (TypeDeclaration)decl;
				if( "test.EntryPoint".equals(typeDecl.getQualifiedName()) ){
					final Collection<FieldDeclaration> fields = typeDecl.getFields();
					for( FieldDeclaration field : fields ){
						final String name = field.getSimpleName();
						if( "nestedAnno".equals(name)){
							final DeclaredType fieldType = (DeclaredType)field.getType();
							final Collection<TypeDeclaration> nestedTypes = fieldType.getDeclaration().getNestedTypes();
							for(TypeDeclaration nestedType : nestedTypes ){
								if( "NestedAnnotation".equals(nestedType.getSimpleName()) ){
									final Collection<? extends MethodDeclaration> annotationMethods = nestedType.getMethods();
									for( MethodDeclaration annotationMethod : annotationMethods ){
										if( "value".equals(annotationMethod.getSimpleName()) ){
											final AnnotationTypeElementDeclaration value = 
												(AnnotationTypeElementDeclaration)annotationMethod;
											final String defaultString = value.getDefaultValue() == null ? "" :
												value.getDefaultValue().toString();
											final String expected = "Eore";
											if(!defaultString.equals(expected) )
												msger.printError("expecting default=" + expected + " but got " +defaultString);
										}
									}
								}
							}
							
							final Collection<FieldDeclaration> nestedAnnoFields = fieldType.getDeclaration().getFields();
							for(FieldDeclaration nestedAnnoField : nestedAnnoFields ){
								if(nestedAnnoField.getSimpleName().equals("FOUR")){
									final Object constant = nestedAnnoField.getConstantValue();
									final String expected = "4";
									final String constantStr = constant == null ? "" : constant.toString();
									if(!constantStr.equals(expected) )
										msger.printError("expecting constant=" + expected + " but got " + constantStr);
								}
							}
							continue;
						}
						else{
							msger.printError("found unexpected field " + field );
						}
						
					}
					
					continue;
				}
			}
			msger.printError("found unexpected declaration " + decl );
		}
	}
}

Back to the top