KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CompilationUnitVisitor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.core.compiler.IProblem;
20 import org.eclipse.jdt.internal.compiler.CompilationResult;
21 import org.eclipse.jdt.internal.compiler.Compiler;
22 import org.eclipse.jdt.internal.compiler.ASTVisitor;
23 import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
24 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
25 import org.eclipse.jdt.internal.compiler.IProblemFactory;
26 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
27 import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
28 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
29 import org.eclipse.jdt.internal.compiler.env.ISourceType;
30 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
31 import org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter;
32 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
33 import org.eclipse.jdt.internal.core.util.Util;
34
35 public class CompilationUnitVisitor extends Compiler JavaDoc {
36     
37     /**
38      * Answer a new CompilationUnitVisitor using the given name environment and compiler options.
39      * The environment and options will be in effect for the lifetime of the compiler.
40      * When the compiler is run, compilation results are sent to the given requestor.
41      *
42      * @param environment org.eclipse.jdt.internal.compiler.api.env.INameEnvironment
43      * Environment used by the compiler in order to resolve type and package
44      * names. The name environment implements the actual connection of the compiler
45      * to the outside world (e.g. in batch mode the name environment is performing
46      * pure file accesses, reuse previous build state or connection to repositories).
47      * Note: the name environment is responsible for implementing the actual classpath
48      * rules.
49      *
50      * @param policy org.eclipse.jdt.internal.compiler.api.problem.IErrorHandlingPolicy
51      * Configurable part for problem handling, allowing the compiler client to
52      * specify the rules for handling problems (stop on first error or accumulate
53      * them all) and at the same time perform some actions such as opening a dialog
54      * in UI when compiling interactively.
55      * @see org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies
56      *
57      * @param requestor org.eclipse.jdt.internal.compiler.api.ICompilerRequestor
58      * Component which will receive and persist all compilation results and is intended
59      * to consume them as they are produced. Typically, in a batch compiler, it is
60      * responsible for writing out the actual .class files to the file system.
61      * @see org.eclipse.jdt.internal.compiler.CompilationResult
62      *
63      * @param problemFactory org.eclipse.jdt.internal.compiler.api.problem.IProblemFactory
64      * Factory used inside the compiler to create problem descriptors. It allows the
65      * compiler client to supply its own representation of compilation problems in
66      * order to avoid object conversions. Note that the factory is not supposed
67      * to accumulate the created problems, the compiler will gather them all and hand
68      * them back as part of the compilation unit result.
69      */

70     public CompilationUnitVisitor(
71         INameEnvironment environment,
72         IErrorHandlingPolicy policy,
73         Map JavaDoc settings,
74         ICompilerRequestor requestor,
75         IProblemFactory problemFactory) {
76
77         super(environment, policy, settings, requestor, problemFactory);
78     }
79
80     /**
81      * Add additional source types
82      */

83     public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding, AccessRestriction accessRestriction) {
84         CompilationResult result =
85             new CompilationResult(sourceTypes[0].getFileName(), 1, 1, this.options.maxProblemsPerUnit);
86         // need to hold onto this
87
CompilationUnitDeclaration unit =
88             SourceTypeConverter.buildCompilationUnit(
89                 sourceTypes,//sourceTypes[0] is always toplevel here
90
SourceTypeConverter.FIELD_AND_METHOD // need field and methods
91
| SourceTypeConverter.MEMBER_TYPE, // need member types
92
// no need for field initialization
93
this.lookupEnvironment.problemReporter,
94                 result);
95
96         if (unit != null) {
97             this.lookupEnvironment.buildTypeBindings(unit, accessRestriction);
98             this.lookupEnvironment.completeTypeBindings(unit, true);
99         }
100     }
101
102     /*
103      * Low-level API performing the actual compilation
104      */

105     protected static IErrorHandlingPolicy getHandlingPolicy() {
106
107         // passes the initial set of files to the batch oracle (to avoid finding more than once the same units when case insensitive match)
108
return new IErrorHandlingPolicy() {
109             public boolean stopOnFirstError() {
110                 return false;
111             }
112             public boolean proceedOnErrors() {
113                 return false; // stop if there are some errors
114
}
115         };
116     }
117
118     /*
119      * Answer the component to which will be handed back compilation results from the compiler
120      */

121     protected static ICompilerRequestor getRequestor() {
122         return new ICompilerRequestor() {
123             public void acceptResult(CompilationResult compilationResult) {
124                 // nothing to do
125
}
126         };
127     }
128
129     public static void visit(
130         ICompilationUnit unitElement,
131         ASTVisitor visitor)
132         throws JavaModelException {
133
134         JavaProject project = (JavaProject) unitElement.getJavaProject();
135         CompilationUnitVisitor compilationUnitVisitor =
136             new CompilationUnitVisitor(
137                 project.newSearchableNameEnvironment(unitElement.getOwner()),
138                 getHandlingPolicy(),
139                 project.getOptions(true),
140                 getRequestor(),
141                 getProblemFactory(visitor));
142
143         CompilationUnitDeclaration unit = null;
144         try {
145
146             PackageFragment packageFragment = (PackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
147             char[][] expectedPackageName = null;
148             if (packageFragment != null){
149                 expectedPackageName = Util.toCharArrays(packageFragment.names);
150             }
151             unit =
152                 compilationUnitVisitor.resolve(
153                     new BasicCompilationUnit(
154                         unitElement.getSource().toCharArray(),
155                         expectedPackageName,
156                         unitElement.getElementName(),
157                         unitElement),
158                     true, // method verification
159
false, // no flow analysis
160
false); // no code generation
161
if (unit != null) {
162                 unit.traverse(visitor, unit.scope);
163             }
164         } finally {
165             if (unit != null) {
166                 unit.cleanUp();
167             }
168         }
169     }
170
171     protected static IProblemFactory getProblemFactory(final ASTVisitor visitor) {
172
173         return new DefaultProblemFactory(Locale.getDefault()) {
174             public IProblem createProblem(
175                 char[] originatingFileName,
176                 int problemId,
177                 String JavaDoc[] problemArguments,
178                 String JavaDoc[] messageArguments,
179                 int severity,
180                 int startPosition,
181                 int endPosition,
182                 int lineNumber) {
183
184                 IProblem problem =
185                     super.createProblem(
186                         originatingFileName,
187                         problemId,
188                         problemArguments,
189                         messageArguments,
190                         severity,
191                         startPosition,
192                         endPosition,
193                         lineNumber);
194                 visitor.acceptProblem(problem);
195                 return problem;
196             }
197         };
198     }
199 }
200
Popular Tags