Lines 11-21
Link Here
|
11 |
package org.eclipse.jdt.core.tests.builder; |
11 |
package org.eclipse.jdt.core.tests.builder; |
12 |
|
12 |
|
13 |
import java.io.*; |
13 |
import java.io.*; |
|
|
14 |
import java.util.ArrayList; |
15 |
import java.util.List; |
16 |
|
14 |
import junit.framework.*; |
17 |
import junit.framework.*; |
15 |
|
18 |
|
16 |
import org.eclipse.core.runtime.*; |
19 |
import org.eclipse.core.runtime.*; |
17 |
import org.eclipse.core.resources.*; |
20 |
import org.eclipse.core.resources.*; |
18 |
import org.eclipse.jdt.core.compiler.*; |
21 |
import org.eclipse.jdt.core.compiler.*; |
|
|
22 |
import org.eclipse.jdt.core.dom.AST; |
23 |
import org.eclipse.jdt.core.dom.ASTParser; |
24 |
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; |
25 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
26 |
import org.eclipse.jdt.core.dom.IMethodBinding; |
27 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
28 |
import org.eclipse.jdt.core.dom.IVariableBinding; |
29 |
import org.eclipse.jdt.core.IJavaProject; |
30 |
import org.eclipse.jdt.core.JavaCore; |
19 |
import org.eclipse.jdt.core.JavaModelException; |
31 |
import org.eclipse.jdt.core.JavaModelException; |
20 |
import org.eclipse.jdt.core.tests.util.Util; |
32 |
import org.eclipse.jdt.core.tests.util.Util; |
21 |
|
33 |
|
Lines 75-80
Link Here
|
75 |
fullBuild(projectPath); |
87 |
fullBuild(projectPath); |
76 |
expectingNoProblems(); |
88 |
expectingNoProblems(); |
77 |
} |
89 |
} |
|
|
90 |
|
91 |
public void testDefaultValue() throws JavaModelException { |
92 |
IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$ //$NON-NLS-2$ |
93 |
env.addExternalJars(projectPath, Util.getJavaClassLibs()); |
94 |
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$ |
95 |
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$ |
96 |
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$ |
97 |
|
98 |
String codeEntryPoint = "package test;\n" + |
99 |
"public class EntryPoint {\n" + |
100 |
" ClassWithNestedAnnotation nestedAnno;\n}"; |
101 |
|
102 |
env.addClass(root, "test", "EntryPoint", codeEntryPoint); |
103 |
|
104 |
String codeClassWithNestedAnnotation = |
105 |
"package test; \n" + |
106 |
"public class ClassWithNestedAnnotation {\n" + |
107 |
" public final int FOUR = 4; \n " + |
108 |
" public @interface NestedAnnotation{\n" + |
109 |
" public enum Character{ \n" + |
110 |
" Winnie, Tiger, Piglet, Eore; \n" + |
111 |
" }\n"+ |
112 |
" Character value() default Character.Eore; \n" + |
113 |
" }\n" + |
114 |
"}"; |
115 |
|
116 |
env.addClass(root, "test", "ClassWithNestedAnnotation", codeClassWithNestedAnnotation); |
117 |
|
118 |
// install compilationParticipant |
119 |
new BuildTestParticipant() { |
120 |
public boolean isAnnotationProcessor() { |
121 |
return true; |
122 |
} |
123 |
public void processAnnotations(ICompilationParticipantResult[] files) { |
124 |
final class ParticipantProblem extends CategorizedProblem implements IProblem { |
125 |
|
126 |
int counter = 0; |
127 |
final String message; |
128 |
final int id; |
129 |
final char[] filename; |
130 |
ParticipantProblem(String message, String filename){ |
131 |
this.message = message; |
132 |
id = counter ++; |
133 |
this.filename = filename.toCharArray(); |
134 |
} |
135 |
public String[] getArguments() { return new String[0];} |
136 |
public int getID() {return id; } |
137 |
public String getMessage() { return message; } |
138 |
public char[] getOriginatingFileName() { return filename; } |
139 |
public int getSourceStart() { return 0; } |
140 |
public int getSourceEnd() { return 0; } |
141 |
public int getSourceLineNumber() { return 1; } |
142 |
public boolean isError() { return true; } |
143 |
public boolean isWarning() { return false; } |
144 |
public void setSourceEnd(int sourceEnd) {/* not needed */} |
145 |
public void setSourceLineNumber(int lineNumber) {/* not needed */} |
146 |
public void setSourceStart(int sourceStart) {/* not needed */} |
147 |
public int getCategoryID() { return 0; } |
148 |
public String getMarkerType() { return "Test_Marker"; } |
149 |
} |
150 |
|
151 |
final int total = files.length; |
152 |
for( int i=0; i<total; i++ ){ |
153 |
final IFile file = files[i].getFile(); |
154 |
final List problems = new ArrayList(); |
155 |
// Traversing the members of test.ClassWithNestedAnnotation through a reference |
156 |
// in EntryPoint.java |
157 |
|
158 |
if( "EntryPoint.java".equals(file.getName()) ){ //$NON-NLS-1$ |
159 |
final CompilationUnit unit = buildCompilationUnit(files[i]); |
160 |
final List types = unit.types(); |
161 |
for( int t=0, tLen=types.size(); t<tLen; t++ ){ |
162 |
final AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration)types.get(t); |
163 |
final ITypeBinding typeBinding = typeDecl.resolveBinding(); |
164 |
if(typeBinding == null ) continue; |
165 |
final IVariableBinding[] fieldBindings = typeBinding.getDeclaredFields(); |
166 |
|
167 |
for( int f=0, fLen=fieldBindings.length; f<fLen; f++ ){ |
168 |
final IVariableBinding field = fieldBindings[f]; |
169 |
final String name = field.getName(); |
170 |
if( "nestedAnno".equals(name)){ |
171 |
final ITypeBinding fieldType = field.getType(); |
172 |
final ITypeBinding[] declaredTypes = fieldType.getDeclaredTypes(); |
173 |
for(int d=0, dLen=declaredTypes.length; d<dLen; d++ ){ |
174 |
|
175 |
if( "NestedAnnotation".equals(declaredTypes[d].getName()) ){ |
176 |
final IMethodBinding[] annotationMethods = declaredTypes[d].getDeclaredMethods(); |
177 |
for( int m=0, mLen=annotationMethods.length; m<mLen; m++ ){ |
178 |
if( "value".equals(annotationMethods[m].getName()) ){ |
179 |
String defaultString = (String)annotationMethods[m].getDefaultValue(); |
180 |
final String expected = "Eore"; |
181 |
if(!expected.equals(defaultString) ){ |
182 |
final IProblem problem = new ParticipantProblem( |
183 |
"expecting default=" + expected + " but got " + defaultString, |
184 |
file.getName()); |
185 |
problems.add(problem); |
186 |
} |
187 |
} |
188 |
} |
189 |
break; |
190 |
} |
191 |
} |
192 |
|
193 |
final IVariableBinding[] nestedAnnoFields = fieldType.getDeclaredFields(); |
194 |
for( int nf=0, nfLen=nestedAnnoFields.length; nf<nfLen; nf ++ ){ |
195 |
if(nestedAnnoFields[nf].getName().equals("FOUR")){ |
196 |
final Object constant = nestedAnnoFields[nf].getConstantValue(); |
197 |
final String expected = "4"; |
198 |
final String constantStr = constant == null ? "" : constant.toString(); |
199 |
if(!constantStr.equals(expected) ){ |
200 |
final IProblem problem = new ParticipantProblem( |
201 |
"expecting constant=" + expected + " but got " + constantStr, |
202 |
file.getName()); |
203 |
problems.add(problem); |
204 |
} |
205 |
} |
206 |
break; |
207 |
} |
208 |
} |
209 |
else{ |
210 |
final IProblem problem = new ParticipantProblem( |
211 |
"found unexpected field " + field, |
212 |
file.getName()); |
213 |
problems.add(problem); |
214 |
} |
215 |
} |
216 |
} |
217 |
} |
218 |
// post problems, if any |
219 |
if(!problems.isEmpty() ){ |
220 |
IProblem[] problemArray = new IProblem[problems.size()]; |
221 |
problemArray = (IProblem[])problems.toArray(problemArray); |
222 |
files[i].recordNewProblems(problemArray); |
223 |
} |
224 |
} |
225 |
} |
226 |
}; |
227 |
|
228 |
fullBuild(projectPath); |
229 |
expectingNoProblems(); |
230 |
} |
231 |
|
232 |
CompilationUnit buildCompilationUnit(final ICompilationParticipantResult file){ |
233 |
final IJavaProject javaProject = JavaCore.create(file.getFile().getProject()); |
234 |
ASTParser p = ASTParser.newParser( AST.JLS3 ); |
235 |
p.setProject( javaProject ); |
236 |
p.setSource(file.getContents()); |
237 |
p.setResolveBindings( true ); |
238 |
p.setKind( ASTParser.K_COMPILATION_UNIT ); |
239 |
p.setUnitName(file.getFile().getName()); |
240 |
return (CompilationUnit) p.createAST(null); |
241 |
} |
78 |
|
242 |
|
79 |
public void testProcessAnnotationDeclarations() throws JavaModelException { |
243 |
public void testProcessAnnotationDeclarations() throws JavaModelException { |
80 |
IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$ //$NON-NLS-2$ |
244 |
IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$ //$NON-NLS-2$ |