KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > CodeSnippetParsingUtil


1 /*******************************************************************************
2  * Copyright (c) 2002, 2006 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.util;
12
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jdt.core.compiler.CategorizedProblem;
17 import org.eclipse.jdt.internal.compiler.CompilationResult;
18 import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
19 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
20 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
22 import org.eclipse.jdt.internal.compiler.ast.Expression;
23 import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
24 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
25 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
26 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
27 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
28
29 /**
30  * Utility class to parse different code snippets
31  */

32 public class CodeSnippetParsingUtil {
33
34     public RecordedParsingInformation recordedParsingInformation;
35
36     private RecordedParsingInformation getRecordedParsingInformation(CompilationResult compilationResult, CommentRecorderParser parser) {
37         int problemsCount = compilationResult.problemCount;
38         CategorizedProblem[] problems = null;
39         if (problemsCount != 0) {
40             final CategorizedProblem[] compilationResultProblems = compilationResult.problems;
41             if (compilationResultProblems.length == problemsCount) {
42                 problems = compilationResultProblems;
43             } else {
44                 System.arraycopy(compilationResultProblems, 0, (problems = new CategorizedProblem[problemsCount]), 0, problemsCount);
45             }
46         }
47         return new RecordedParsingInformation(problems, compilationResult.getLineSeparatorPositions(), parser.getCommentsPositions());
48     }
49
50     public ASTNode[] parseClassBodyDeclarations(char[] source, Map JavaDoc settings, boolean recordParsingInformation) {
51         return parseClassBodyDeclarations(source, 0, source.length, settings, recordParsingInformation);
52     }
53
54     public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length, Map JavaDoc settings, boolean recordParsingInformation) {
55         if (source == null) {
56             throw new IllegalArgumentException JavaDoc();
57         }
58         CompilerOptions compilerOptions = new CompilerOptions(settings);
59         final ProblemReporter problemReporter = new ProblemReporter(
60                     DefaultErrorHandlingPolicies.proceedWithAllProblems(),
61                     compilerOptions,
62                     new DefaultProblemFactory(Locale.getDefault()));
63                     
64         CommentRecorderParser parser = new CommentRecorderParser(problemReporter, false);
65         parser.setMethodsFullRecovery(false);
66         parser.setStatementsRecovery(false);
67         
68         ICompilationUnit sourceUnit =
69             new CompilationUnit(
70                 source,
71                 "", //$NON-NLS-1$
72
compilerOptions.defaultEncoding);
73
74         CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
75         final CompilationUnitDeclaration compilationUnitDeclaration = new CompilationUnitDeclaration(problemReporter, compilationResult, source.length);
76         ASTNode[] result = parser.parseClassBodyDeclarations(source, offset, length, compilationUnitDeclaration);
77         
78         if (recordParsingInformation) {
79             this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
80         }
81         return result;
82     }
83
84     public CompilationUnitDeclaration parseCompilationUnit(char[] source, Map JavaDoc settings, boolean recordParsingInformation) {
85         if (source == null) {
86             throw new IllegalArgumentException JavaDoc();
87         }
88         CompilerOptions compilerOptions = new CompilerOptions(settings);
89         CommentRecorderParser parser =
90             new CommentRecorderParser(
91                 new ProblemReporter(
92                     DefaultErrorHandlingPolicies.proceedWithAllProblems(),
93                     compilerOptions,
94                     new DefaultProblemFactory(Locale.getDefault())),
95             false);
96         
97         ICompilationUnit sourceUnit =
98             new CompilationUnit(
99                 source,
100                 "", //$NON-NLS-1$
101
compilerOptions.defaultEncoding);
102         final CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
103         CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult);
104
105         if (recordParsingInformation) {
106             this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
107         }
108         
109         if (compilationUnitDeclaration.ignoreMethodBodies) {
110             compilationUnitDeclaration.ignoreFurtherInvestigation = true;
111             // if initial diet parse did not work, no need to dig into method bodies.
112
return compilationUnitDeclaration;
113         }
114         
115         //fill the methods bodies in order for the code to be generated
116
//real parse of the method....
117
parser.scanner.setSource(compilationResult);
118         org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = compilationUnitDeclaration.types;
119         if (types != null) {
120             for (int i = types.length; --i >= 0;) {
121                 types[i].parseMethod(parser, compilationUnitDeclaration);
122             }
123         }
124         
125         if (recordParsingInformation) {
126             this.recordedParsingInformation.updateRecordedParsingInformation(compilationResult);
127         }
128         return compilationUnitDeclaration;
129     }
130
131     public Expression parseExpression(char[] source, Map JavaDoc settings, boolean recordParsingInformation) {
132         return parseExpression(source, 0, source.length, settings, recordParsingInformation);
133     }
134     
135     public Expression parseExpression(char[] source, int offset, int length, Map JavaDoc settings, boolean recordParsingInformation) {
136         
137         if (source == null) {
138             throw new IllegalArgumentException JavaDoc();
139         }
140         CompilerOptions compilerOptions = new CompilerOptions(settings);
141         final ProblemReporter problemReporter = new ProblemReporter(
142                     DefaultErrorHandlingPolicies.proceedWithAllProblems(),
143                     compilerOptions,
144                     new DefaultProblemFactory(Locale.getDefault()));
145                     
146         CommentRecorderParser parser = new CommentRecorderParser(problemReporter, false);
147
148         ICompilationUnit sourceUnit =
149             new CompilationUnit(
150                 source,
151                 "", //$NON-NLS-1$
152
compilerOptions.defaultEncoding);
153
154         CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
155         Expression result = parser.parseExpression(source, offset, length, new CompilationUnitDeclaration(problemReporter, compilationResult, source.length));
156         
157         if (recordParsingInformation) {
158             this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
159         }
160         return result;
161     }
162
163     public ConstructorDeclaration parseStatements(char[] source, Map JavaDoc settings, boolean recordParsingInformation, boolean enabledStatementRecovery) {
164         return parseStatements(source, 0, source.length, settings, recordParsingInformation, enabledStatementRecovery);
165     }
166     
167     public ConstructorDeclaration parseStatements(char[] source, int offset, int length, Map JavaDoc settings, boolean recordParsingInformation, boolean enabledStatementRecovery) {
168         if (source == null) {
169             throw new IllegalArgumentException JavaDoc();
170         }
171         CompilerOptions compilerOptions = new CompilerOptions(settings);
172         final ProblemReporter problemReporter = new ProblemReporter(
173                     DefaultErrorHandlingPolicies.proceedWithAllProblems(),
174                     compilerOptions,
175                     new DefaultProblemFactory(Locale.getDefault()));
176         CommentRecorderParser parser = new CommentRecorderParser(problemReporter, false);
177         parser.setMethodsFullRecovery(false);
178         parser.setStatementsRecovery(enabledStatementRecovery);
179         
180         ICompilationUnit sourceUnit =
181             new CompilationUnit(
182                 source,
183                 "", //$NON-NLS-1$
184
compilerOptions.defaultEncoding);
185
186         final CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
187         CompilationUnitDeclaration compilationUnitDeclaration = new CompilationUnitDeclaration(problemReporter, compilationResult, length);
188
189         ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(compilationResult);
190         constructorDeclaration.sourceEnd = -1;
191         constructorDeclaration.declarationSourceEnd = offset + length - 1;
192         constructorDeclaration.bodyStart = offset;
193         constructorDeclaration.bodyEnd = offset + length - 1;
194         
195         parser.scanner.setSource(compilationResult);
196         parser.scanner.resetTo(offset, offset + length);
197         parser.parse(constructorDeclaration, compilationUnitDeclaration, true);
198         
199         if (recordParsingInformation) {
200             this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
201         }
202         return constructorDeclaration;
203     }
204 }
205
Popular Tags