use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.
the class CheCompilationUnitResolver method resolve.
public static CompilationUnitDeclaration resolve(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, IJavaProject javaProject, List classpaths, NodeSearcher nodeSearcher, Map options, WorkingCopyOwner owner, int flags, IProgressMonitor monitor) throws JavaModelException {
CompilationUnitDeclaration unit = null;
INameEnvironmentWithProgress environment = null;
CancelableProblemFactory problemFactory = null;
CheCompilationUnitResolver resolver = null;
try {
if (javaProject == null) {
FileSystem.Classpath[] allEntries = new FileSystem.Classpath[classpaths.size()];
classpaths.toArray(allEntries);
environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
} else {
environment = new CancelableNameEnvironment((JavaProject) javaProject, owner, monitor);
}
problemFactory = new CancelableProblemFactory(monitor);
CompilerOptions compilerOptions = CompilationUnitResolver.getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
boolean ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
resolver = new CheCompilationUnitResolver(environment, CompilationUnitResolver.getHandlingPolicy(), compilerOptions, CompilationUnitResolver.getRequestor(), problemFactory, monitor, javaProject != null);
boolean analyzeAndGenerateCode = !ignoreMethodBodies;
unit = resolver.resolve(// no existing compilation unit declaration
null, sourceUnit, nodeSearcher, // method verification
true, // analyze code
analyzeAndGenerateCode, // generate code
analyzeAndGenerateCode);
if (resolver.hasCompilationAborted) {
// the bindings could not be resolved due to missing types in name environment
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=86541
CompilationUnitDeclaration unitDeclaration = CompilationUnitResolver.parse(sourceUnit, nodeSearcher, options, flags);
if (unit != null) {
final int problemCount = unit.compilationResult.problemCount;
if (problemCount != 0) {
unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount];
System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems, 0, problemCount);
unitDeclaration.compilationResult.problemCount = problemCount;
}
} else if (resolver.abortProblem != null) {
unitDeclaration.compilationResult.problemCount = 1;
unitDeclaration.compilationResult.problems = new CategorizedProblem[] { resolver.abortProblem };
}
return unitDeclaration;
}
if (NameLookup.VERBOSE && environment instanceof CancelableNameEnvironment) {
CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms");
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms");
}
return unit;
} finally {
if (environment != null) {
// don't hold a reference to this external object
environment.setMonitor(null);
}
if (problemFactory != null) {
// don't hold a reference to this external object
problemFactory.monitor = null;
}
}
}
use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.
the class CheCompilationUnitResolver method resolve.
private CompilationUnitDeclaration resolve(CompilationUnitDeclaration unit, org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, NodeSearcher nodeSearcher, boolean verifyMethods, boolean analyzeCode, boolean generateCode) {
try {
if (unit == null) {
// build and record parsed units
// will request a full parse
this.parseThreshold = 0;
beginToCompile(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { sourceUnit });
// find the right unit from what was injected via accept(ICompilationUnit,..):
for (int i = 0, max = this.totalUnits; i < max; i++) {
CompilationUnitDeclaration currentCompilationUnitDeclaration = this.unitsToProcess[i];
if (currentCompilationUnitDeclaration != null && currentCompilationUnitDeclaration.compilationResult.compilationUnit == sourceUnit) {
unit = currentCompilationUnitDeclaration;
break;
}
}
if (unit == null) {
// fall back to old behavior
unit = this.unitsToProcess[0];
}
} else {
// initial type binding creation
this.lookupEnvironment.buildTypeBindings(unit, null);
// binding resolution
this.lookupEnvironment.completeTypeBindings();
}
if (nodeSearcher == null) {
// no-op if method bodies have already been parsed
this.parser.getMethodBodies(unit);
} else {
int searchPosition = nodeSearcher.position;
char[] source = sourceUnit.getContents();
int length = source.length;
if (searchPosition >= 0 && searchPosition <= length) {
unit.traverse(nodeSearcher, unit.scope);
org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodeSearcher.found;
if (node != null) {
// save existing values to restore them at the end of the parsing process
// see bug 47079 for more details
int[] oldLineEnds = this.parser.scanner.lineEnds;
int oldLinePtr = this.parser.scanner.linePtr;
this.parser.scanner.setSource(source, unit.compilationResult);
org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enclosingTypeDeclaration = nodeSearcher.enclosingType;
if (node instanceof AbstractMethodDeclaration) {
((AbstractMethodDeclaration) node).parseStatements(this.parser, unit);
} else if (enclosingTypeDeclaration != null) {
if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) {
((org.eclipse.jdt.internal.compiler.ast.Initializer) node).parseStatements(this.parser, enclosingTypeDeclaration, unit);
} else if (node instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) node).parseMethods(this.parser, unit);
}
}
// this is done to prevent any side effects on the compilation unit result
// line separator positions array.
this.parser.scanner.lineEnds = oldLineEnds;
this.parser.scanner.linePtr = oldLinePtr;
}
}
}
if (unit.scope != null) {
// fault in fields & methods
unit.scope.faultInTypes();
if (unit.scope != null && verifyMethods) {
// http://dev.eclipse.org/bugs/show_bug.cgi?id=23117
// verify inherited methods
unit.scope.verifyMethods(this.lookupEnvironment.methodVerifier());
}
// type checking
unit.resolve();
// flow analysis
if (analyzeCode)
unit.analyseCode();
// code generation
if (generateCode)
unit.generateCode();
// finalize problems (suppressWarnings)
unit.finalizeProblems();
}
// release reference to processed unit declaration
if (this.unitsToProcess != null)
this.unitsToProcess[0] = null;
this.requestor.acceptResult(unit.compilationResult.tagAsAccepted());
return unit;
} catch (org.eclipse.jdt.internal.compiler.problem.AbortCompilation e) {
this.handleInternalException(e, unit);
return unit == null ? this.unitsToProcess[0] : unit;
} catch (Error e) {
this.handleInternalException(e, unit, null);
// rethrow
throw e;
} catch (RuntimeException e) {
this.handleInternalException(e, unit, null);
// rethrow
throw e;
} finally {
// No reset is performed there anymore since,
// within the CodeAssist (or related tools),
// the compiler may be called *after* a call
// to this resolve(...) method. And such a call
// needs to have a compiler with a non-empty
// environment.
// this.reset();
}
}
use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.
the class CodenvyCompilationUnitResolver method resolve.
public static CompilationUnitDeclaration resolve(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, IJavaProject javaProject, INameEnvironment environment, Map options, int flags, IProgressMonitor monitor) throws JavaModelException {
CompilationUnitDeclaration unit = null;
// INameEnvironmentWithProgress environment = null;
CancelableProblemFactory problemFactory = null;
CodenvyCompilationUnitResolver resolver = null;
try {
// if (javaProject == null) {
// FileSystem.Classpath[] allEntries = new FileSystem.Classpath[classpaths.size()];
// classpaths.toArray(allEntries);
// environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
// } else {
// environment = new CancelableNameEnvironment((JavaProject) javaProject, owner, monitor);
// }
problemFactory = new CancelableProblemFactory(monitor);
CompilerOptions compilerOptions = CompilationUnitResolver.getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
boolean ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
resolver = new CodenvyCompilationUnitResolver(environment, CompilationUnitResolver.getHandlingPolicy(), compilerOptions, CompilationUnitResolver.getRequestor(), problemFactory, monitor, javaProject != null);
boolean analyzeAndGenerateCode = !ignoreMethodBodies;
unit = resolver.resolve(// no existing compilation unit declaration
null, sourceUnit, null, // method verification
true, // analyze code
analyzeAndGenerateCode, // generate code
analyzeAndGenerateCode);
if (resolver.hasCompilationAborted) {
// the bindings could not be resolved due to missing types in name environment
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=86541
CompilationUnitDeclaration unitDeclaration = CompilationUnitResolver.parse(sourceUnit, null, options, flags);
if (unit != null) {
final int problemCount = unit.compilationResult.problemCount;
if (problemCount != 0) {
unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount];
System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems, 0, problemCount);
unitDeclaration.compilationResult.problemCount = problemCount;
}
} else if (resolver.abortProblem != null) {
unitDeclaration.compilationResult.problemCount = 1;
unitDeclaration.compilationResult.problems = new CategorizedProblem[] { resolver.abortProblem };
}
return unitDeclaration;
}
if (NameLookup.VERBOSE && environment instanceof CancelableNameEnvironment) {
CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms");
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms");
}
return unit;
} finally {
if (environment != null) {
// don't hold a reference to this external object
// environment.setMonitor(null);
}
if (problemFactory != null) {
// don't hold a reference to this external object
problemFactory.monitor = null;
}
}
}
use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.
the class CodenvyCompilationUnitResolver method resolve.
private CompilationUnitDeclaration resolve(CompilationUnitDeclaration unit, org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, NodeSearcher nodeSearcher, boolean verifyMethods, boolean analyzeCode, boolean generateCode) {
try {
if (unit == null) {
// build and record parsed units
// will request a full parse
this.parseThreshold = 0;
beginToCompile(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { sourceUnit });
// find the right unit from what was injected via accept(ICompilationUnit,..):
for (int i = 0, max = this.totalUnits; i < max; i++) {
CompilationUnitDeclaration currentCompilationUnitDeclaration = this.unitsToProcess[i];
if (currentCompilationUnitDeclaration != null && currentCompilationUnitDeclaration.compilationResult.compilationUnit == sourceUnit) {
unit = currentCompilationUnitDeclaration;
break;
}
}
if (unit == null) {
// fall back to old behavior
unit = this.unitsToProcess[0];
}
} else {
// initial type binding creation
this.lookupEnvironment.buildTypeBindings(unit, null);
// binding resolution
this.lookupEnvironment.completeTypeBindings();
}
if (nodeSearcher == null) {
// no-op if method bodies have already been parsed
this.parser.getMethodBodies(unit);
} else {
int searchPosition = nodeSearcher.position;
char[] source = sourceUnit.getContents();
int length = source.length;
if (searchPosition >= 0 && searchPosition <= length) {
unit.traverse(nodeSearcher, unit.scope);
org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodeSearcher.found;
if (node != null) {
// save existing values to restore them at the end of the parsing process
// see bug 47079 for more details
int[] oldLineEnds = this.parser.scanner.lineEnds;
int oldLinePtr = this.parser.scanner.linePtr;
this.parser.scanner.setSource(source, unit.compilationResult);
org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enclosingTypeDeclaration = nodeSearcher.enclosingType;
if (node instanceof AbstractMethodDeclaration) {
((AbstractMethodDeclaration) node).parseStatements(this.parser, unit);
} else if (enclosingTypeDeclaration != null) {
if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) {
((org.eclipse.jdt.internal.compiler.ast.Initializer) node).parseStatements(this.parser, enclosingTypeDeclaration, unit);
} else if (node instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) node).parseMethods(this.parser, unit);
}
}
// this is done to prevent any side effects on the compilation unit result
// line separator positions array.
this.parser.scanner.lineEnds = oldLineEnds;
this.parser.scanner.linePtr = oldLinePtr;
}
}
}
if (unit.scope != null) {
// fault in fields & methods
unit.scope.faultInTypes();
if (unit.scope != null && verifyMethods) {
// http://dev.eclipse.org/bugs/show_bug.cgi?id=23117
// verify inherited methods
unit.scope.verifyMethods(this.lookupEnvironment.methodVerifier());
}
// type checking
unit.resolve();
// flow analysis
if (analyzeCode)
unit.analyseCode();
// code generation
if (generateCode)
unit.generateCode();
// finalize problems (suppressWarnings)
unit.finalizeProblems();
}
// release reference to processed unit declaration
if (this.unitsToProcess != null)
this.unitsToProcess[0] = null;
this.requestor.acceptResult(unit.compilationResult.tagAsAccepted());
return unit;
} catch (org.eclipse.jdt.internal.compiler.problem.AbortCompilation e) {
this.handleInternalException(e, unit);
return unit == null ? this.unitsToProcess[0] : unit;
} catch (Error e) {
this.handleInternalException(e, unit, null);
// rethrow
throw e;
} catch (RuntimeException e) {
this.handleInternalException(e, unit, null);
// rethrow
throw e;
} finally {
// No reset is performed there anymore since,
// within the CodeAssist (or related tools),
// the compiler may be called *after* a call
// to this resolve(...) method. And such a call
// needs to have a compiler with a non-empty
// environment.
// this.reset();
}
}
use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.
the class SourceTypeConverter method convert.
/*
* Convert a set of source element types into a parsed compilation unit declaration
* The argument types are then all grouped in the same unit. The argument types must
* at least contain one type.
*/
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
if (sourceTypes.length == 0)
return this.unit;
SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
this.cu = (ICompilationUnit) cuHandle;
final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
// Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
if ((this.flags & LOCAL_TYPE) == 0) {
return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
} else {
return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
}
}
/* only positions available */
int start = topLevelTypeInfo.getNameSourceStart();
int end = topLevelTypeInfo.getNameSourceEnd();
/* convert package and imports */
String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
if (packageName.length > 0)
// if its null then it is defined in the default package
this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
int importCount = importDeclarations.length;
this.unit.imports = new ImportReference[importCount];
for (int i = 0; i < importCount; i++) {
ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
String nameWithoutStar = importDeclaration.getNameWithoutStar();
this.unit.imports[i] = createImportReference(Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers());
}
/* convert type(s) */
try {
int typeCount = sourceTypes.length;
final TypeDeclaration[] types = new TypeDeclaration[typeCount];
/*
* We used a temporary types collection to prevent this.unit.types from being null during a call to
* convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
*/
for (int i = 0; i < typeCount; i++) {
SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
}
this.unit.types = types;
return this.unit;
} catch (AnonymousMemberFound e) {
return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
}
}
Aggregations