List of usage examples for org.eclipse.jdt.internal.compiler SourceElementParser SourceElementParser
public SourceElementParser(final ISourceElementRequestor requestor, IProblemFactory problemFactory, CompilerOptions options, boolean reportLocalDeclarations, boolean optimizeStringLiterals)
From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java
License:Open Source License
protected boolean buildStructure(OpenableElementInfo info, final IProgressMonitor pm, Map newElements, File underlyingResource) throws JavaModelException { CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info; // ensure buffer is opened IBuffer buffer = getBufferManager().getBuffer(CompilationUnit.this); if (buffer == null) { openBuffer(pm, unitInfo); // open buffer independently from the info, since we are building the info }/* w ww . j a v a 2 s . c om*/ // generate structure and compute syntax problems if needed CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo, newElements, manager); JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo(); IJavaProject project = getJavaProject(); boolean createAST; boolean resolveBindings; int reconcileFlags; HashMap problems; if (info instanceof ASTHolderCUInfo) { ASTHolderCUInfo astHolder = (ASTHolderCUInfo) info; createAST = astHolder.astLevel != NO_AST; resolveBindings = astHolder.resolveBindings; reconcileFlags = astHolder.reconcileFlags; problems = astHolder.problems; } else { createAST = false; resolveBindings = true; reconcileFlags = 0; problems = null; } boolean computeProblems = false; // boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo.isActive() && project != null && JavaProject // .hasJavaNature(project.getProject()); IProblemFactory problemFactory = new DefaultProblemFactory(); Map options = project == null ? JavaCore.getOptions() : project.getOptions(true); if (!computeProblems) { // disable task tags checking to speed up parsing options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$ } CompilerOptions compilerOptions = new CompilerOptions(options); compilerOptions.ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0; SourceElementParser parser = new SourceElementParser(requestor, problemFactory, compilerOptions, true/*report local declarations*/, !createAST /*optimize string literals only if not creating a DOM AST*/); parser.reportOnlyOneSyntaxError = !computeProblems; parser.setMethodsFullRecovery(true); parser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0); if (!computeProblems && !resolveBindings && !createAST) // disable javadoc parsing if not computing problems, not resolving and not creating ast parser.javadocParser.checkDocComment = false; requestor.parser = parser; // update timestamp (might be IResource.NULL_STAMP if original does not exist) if (underlyingResource == null) { underlyingResource = resource(); } // underlying resource is null in the case of a working copy on a class file in a jar if (underlyingResource != null) unitInfo.timestamp = (underlyingResource).lastModified(); // compute other problems if needed CompilationUnitDeclaration compilationUnitDeclaration = null; CompilationUnit source = cloneCachingContents(); try { if (computeProblems) { // if (problems == null) { // // report problems to the problem requestor // problems = new HashMap(); // compilationUnitDeclaration = CompilationUnitProblemFinder // .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm); // try { // perWorkingCopyInfo.beginReporting(); // for (Iterator iteraror = problems.values().iterator(); iteraror.hasNext();) { // CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror.next(); // if (categorizedProblems == null) continue; // for (int i = 0, length = categorizedProblems.length; i < length; i++) { // perWorkingCopyInfo.acceptProblem(categorizedProblems[i]); // } // } // } finally { // perWorkingCopyInfo.endReporting(); // } // } else { // // collect problems // compilationUnitDeclaration = CompilationUnitProblemFinder // .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm); // } } else { compilationUnitDeclaration = parser.parseCompilationUnit(source, true /*full parse to find local elements*/, pm); } if (createAST) { // int astLevel = ((ASTHolderCUInfo) info).astLevel; // org.eclipse.jdt.core.dom.CompilationUnit cu = AST // .convertCompilationUnit(astLevel, compilationUnitDeclaration, options, computeProblems, source, reconcileFlags, pm); // ((ASTHolderCUInfo) info).ast = cu; } } finally { if (compilationUnitDeclaration != null) { unitInfo.hasFunctionalTypes = compilationUnitDeclaration.hasFunctionalTypes(); compilationUnitDeclaration.cleanUp(); } } return unitInfo.isStructureKnown(); }
From source file:com.codenvy.ide.ext.java.server.internal.core.SourceMapper.java
License:Open Source License
/** * Maps the given source code to the given binary type and its children. * If a non-null java element is passed, finds the name range for the * given java element without storing it. *//*from w w w .j a v a2 s . c o m*/ public synchronized ISourceRange mapSource(IType type, char[] contents, IBinaryType info, IJavaElement elementToFind) { this.binaryType = (BinaryType) type; // check whether it is already mapped if (this.sourceRanges.get(type) != null) return (elementToFind != null) ? getNameRange(elementToFind) : null; this.importsTable.remove(this.binaryType); this.importsCounterTable.remove(this.binaryType); this.searchedElement = elementToFind; this.types = new IType[1]; this.typeDeclarationStarts = new int[1]; this.typeNameRanges = new SourceRange[1]; this.typeModifiers = new int[1]; this.typeDepth = -1; this.memberDeclarationStart = new int[1]; this.memberName = new String[1]; this.memberNameRange = new SourceRange[1]; this.methodParameterTypes = new char[1][][]; this.methodParameterNames = new char[1][][]; this.anonymousCounter = 0; HashMap oldSourceRanges = null; if (elementToFind != null) { oldSourceRanges = (HashMap) this.sourceRanges.clone(); } try { IProblemFactory factory = new DefaultProblemFactory(); SourceElementParser parser = null; this.anonymousClassName = 0; if (info == null) { try { info = (IBinaryType) this.binaryType.getElementInfo(); } catch (JavaModelException e) { return null; } } boolean isAnonymousClass = info.isAnonymous(); char[] fullName = info.getName(); if (isAnonymousClass) { String eltName = this.binaryType.getParent().getElementName(); eltName = eltName.substring(eltName.lastIndexOf('$') + 1, eltName.length()); try { this.anonymousClassName = Integer.parseInt(eltName); } catch (NumberFormatException e) { // ignore } } boolean doFullParse = hasToRetrieveSourceRangesForLocalClass(fullName); parser = new SourceElementParser(this, factory, new CompilerOptions(this.options), doFullParse, true/*optimize string literals*/); parser.javadocParser.checkDocComment = false; // disable javadoc parsing IJavaElement javaElement = this.binaryType.getCompilationUnit(); if (javaElement == null) javaElement = this.binaryType.getParent(); parser.parseCompilationUnit( new BasicCompilationUnit(contents, null, this.binaryType.sourceFileName(info), javaElement), doFullParse, null/*no progress*/); if (elementToFind != null) { ISourceRange range = getNameRange(elementToFind); return range; } else { return null; } } finally { if (elementToFind != null) { this.sourceRanges = oldSourceRanges; } this.binaryType = null; this.searchedElement = null; this.types = null; this.typeDeclarationStarts = null; this.typeNameRanges = null; this.typeDepth = -1; } }
From source file:lombok.RunTestsViaEclipse.java
License:Open Source License
@Override public void transformCode(final StringBuilder messages, StringWriter result, File file) throws Throwable { // setup parser and compiler final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems(); final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH); final INameEnvironment nameEnvironment = new LombokTestNameEnvironment(); final ICompilerRequestor compilerRequestor = new LombokTestCompilerRequestor(); final ISourceElementRequestor sourceElementRequestor = new LombokTestSourceElementRequestor(); final CompilerOptions options = ecjCompilerOptions(); final LombokTestCompiler jdtCompiler = new LombokTestCompiler(nameEnvironment, policy, options, compilerRequestor, problemFactory); final SourceElementParser parser = new SourceElementParser(sourceElementRequestor, problemFactory, options, true, true);/* ww w.j a v a 2 s.co m*/ // read the file final String source = readFile(file); final CompilationUnit sourceUnit = new CompilationUnit(source.toCharArray(), file.getName(), "UTF-8"); // parse final CompilationUnitDeclaration cud = parser.parseCompilationUnit(sourceUnit, true, null); // build jdtCompiler.lookupEnvironment.buildTypeBindings(cud, null); jdtCompiler.lookupEnvironment.completeTypeBindings(cud, true); // process if (cud.scope != null) cud.scope.verifyMethods(jdtCompiler.lookupEnvironment.methodVerifier()); // handle problems final CategorizedProblem[] problems = cud.compilationResult.getAllProblems(); if (problems != null) for (CategorizedProblem p : problems) { messages.append(String.format("%d %s %s\n", p.getSourceLineNumber(), p.isError() ? "error" : p.isWarning() ? "warning" : "unknown", p.getMessage())); } // set transformed code result.append(cud.toString()); }
From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java
License:Open Source License
@Override public void initializeParser() { // AspectJ Change Begin if (cu != null) { // wait until object is initialized to initialize parser try {/*from ww w . ja v a 2 s. com*/ Object elementInfo = ((JavaElement) cu).getElementInfo(); if (elementInfo instanceof AJCompilationUnitInfo) { AJCompilationUnit ajcu = (AJCompilationUnit) cu; ajcu.discardOriginalContentMode(); this.parser = new AJSourceElementParser2(new NullRequestor(), new DefaultProblemFactory(), getCompilerOptions(cu), true /* parse local declarations */, false); ajcu.requestOriginalContentMode(); } else { // use a SourceElementParser to ensure that local declarations are parsed even when diet this.parser = new SourceElementParser(new NullRequestor(), new DefaultProblemFactory(), getCompilerOptions(cu), true, false); } } catch (JavaModelException e) { } } // AspectJ Change End }
From source file:org.eclipse.jdt.internal.core.CompilationUnit.java
License:Open Source License
protected boolean buildStructure(OpenableElementInfo info, final IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException { CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info; // ensure buffer is opened IBuffer buffer = getBufferManager().getBuffer(CompilationUnit.this); if (buffer == null) { openBuffer(pm, unitInfo); // open buffer independently from the info, since we are building the info }//from w w w . j a v a2 s .c om // generate structure and compute syntax problems if needed CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo, newElements); JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo(); IJavaProject project = getJavaProject(); boolean createAST; boolean resolveBindings; int reconcileFlags; HashMap problems; if (info instanceof ASTHolderCUInfo) { ASTHolderCUInfo astHolder = (ASTHolderCUInfo) info; createAST = astHolder.astLevel != NO_AST; resolveBindings = astHolder.resolveBindings; reconcileFlags = astHolder.reconcileFlags; problems = astHolder.problems; } else { createAST = false; resolveBindings = false; reconcileFlags = 0; problems = null; } boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo.isActive() && project != null && JavaProject.hasJavaNature(project.getProject()); IProblemFactory problemFactory = new DefaultProblemFactory(); Map options = project == null ? JavaCore.getOptions() : project.getOptions(true); if (!computeProblems) { // disable task tags checking to speed up parsing options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$ } CompilerOptions compilerOptions = new CompilerOptions(options); compilerOptions.ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0; SourceElementParser parser = new SourceElementParser(requestor, problemFactory, compilerOptions, true/*report local declarations*/, !createAST /*optimize string literals only if not creating a DOM AST*/); parser.reportOnlyOneSyntaxError = !computeProblems; parser.setMethodsFullRecovery(true); parser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0); if (!computeProblems && !resolveBindings && !createAST) // disable javadoc parsing if not computing problems, not resolving and not creating ast parser.javadocParser.checkDocComment = false; requestor.parser = parser; // update timestamp (might be IResource.NULL_STAMP if original does not exist) if (underlyingResource == null) { underlyingResource = getResource(); } // underlying resource is null in the case of a working copy on a class file in a jar if (underlyingResource != null) unitInfo.timestamp = ((IFile) underlyingResource).getModificationStamp(); // compute other problems if needed CompilationUnitDeclaration compilationUnitDeclaration = null; CompilationUnit source = cloneCachingContents(); try { if (computeProblems) { if (problems == null) { // report problems to the problem requestor problems = new HashMap(); compilationUnitDeclaration = CompilationUnitProblemFinder.process(source, parser, this.owner, problems, createAST, reconcileFlags, pm); try { perWorkingCopyInfo.beginReporting(); for (Iterator iteraror = problems.values().iterator(); iteraror.hasNext();) { CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror.next(); if (categorizedProblems == null) continue; for (int i = 0, length = categorizedProblems.length; i < length; i++) { perWorkingCopyInfo.acceptProblem(categorizedProblems[i]); } } } finally { perWorkingCopyInfo.endReporting(); } } else { // collect problems compilationUnitDeclaration = CompilationUnitProblemFinder.process(source, parser, this.owner, problems, createAST, reconcileFlags, pm); } } else { compilationUnitDeclaration = parser.parseCompilationUnit(source, true /*full parse to find local elements*/, pm); } if (createAST) { int astLevel = ((ASTHolderCUInfo) info).astLevel; org.eclipse.jdt.core.dom.CompilationUnit cu = AST.convertCompilationUnit(astLevel, compilationUnitDeclaration, options, computeProblems, source, reconcileFlags, pm); ((ASTHolderCUInfo) info).ast = cu; } } finally { if (compilationUnitDeclaration != null) { compilationUnitDeclaration.cleanUp(); } } return unitInfo.isStructureKnown(); }
From source file:org.eclipse.objectteams.otdt.tests.compiler.smap.AbstractSourceMapGeneratorTest.java
License:Open Source License
public boolean parseAndCompile(org.eclipse.jdt.core.ICompilationUnit[] units, HashMap<String, int[]> methodLineNumbers, String[] classPaths, String outputPath) throws JavaModelException { CompilerOptions options = getCompilerOptions(); SourceElementParser parser = new SourceElementParser(this, new DefaultProblemFactory(Locale.getDefault()), options, false, false);//from w w w . j a v a 2 s. com ICompilationUnit[] cUnits = new ICompilationUnit[units.length]; for (int idx = 0; idx < units.length; idx++) { org.eclipse.jdt.core.ICompilationUnit unit = units[idx]; String unit_src = unit.getSource(); IResource unit_res = unit.getCorrespondingResource(); String unit_fileName = unit_res.toString(); char[] unit_source = unit_src.toCharArray(); ICompilationUnit unit_sourceUnit = new CompilationUnit(unit_source, unit_fileName, null); CompilationUnitDeclaration cuDecl = parser.parseCompilationUnit(unit_sourceUnit, true, null); if (cuDecl.hasErrors()) { // approximated error reporting: String expectedErrorlog = "Filename : L/JSR-045/src/" + unit_fileName + "\n" + "COMPILED type(s) " + "to be filled in\n" + "No REUSED BINARY type\n" + "No PROBLEM"; String actualErrorlog = cuDecl.compilationResult().toString(); assertEquals("COMPILATION FAILED. Errorlog should be empty.", expectedErrorlog, actualErrorlog); return false; } cUnits[idx] = unit_sourceUnit; } Requestor requestor = new Requestor(false, null, /*no custom requestor */ false, /* show category */ false /* show warning token*/, methodLineNumbers); if (outputPath != null) { requestor.outputPath = outputPath; File outDir = new File(outputPath); if (!outDir.exists()) outDir.mkdir(); } CustomizedCompiler batchCompiler; try { batchCompiler = new CustomizedCompiler(getNameEnvironment(new String[] {}, classPaths), getErrorHandlingPolicy(), options, requestor, getProblemFactory()); } catch (IOException ioex) { throw new JavaModelException(ioex, IJavaModelStatusConstants.INVALID_CLASSPATH); } batchCompiler.addCallBack(this); batchCompiler.compile(cUnits); // compile all files together boolean hasErrors = requestor.hasErrors; //errorlog contains errore and warnings, skip warnings if (hasErrors) { String expectedErrorlog = ""; String actualErrorlog = requestor.problemLog; assertEquals("COMPILATION FAILED. Errorlog should be empty.", expectedErrorlog, actualErrorlog); } if (methodLineNumbers != null) requestor.checkAllLineNumbersSeen(); return !hasErrors; }
From source file:org.eclipse.objectteams.otdt.tests.compiler.SourceElementRequestorTest.java
License:Open Source License
public void fullParse(String src, String fileName) { this.source = src.toCharArray(); // km: ctor with new parameter "reportLocalDeclarations", "optimizeStringLiterals" assuming both to be true SourceElementParser parser = new SourceElementParser(this, new DefaultProblemFactory(Locale.getDefault()), new CompilerOptions(), true, true); ICompilationUnit sourceUnit = new CompilationUnit(source, fileName, null); parser.parseCompilationUnit(sourceUnit, true, null); }
From source file:org.eclipse.objectteams.otdt.tests.parser.SourceElementParserTest.java
License:Open Source License
public void fullParse(String src, String fileName) { this.source = src.toCharArray(); // km: two new parameter in 32m4: "reportLocalDeclarations","optimizeStringLiterals" // assuming both as true SourceElementParser parser = new SourceElementParser(this, new DefaultProblemFactory(Locale.getDefault()), new CompilerOptions(), true, true); ICompilationUnit sourceUnit = new CompilationUnit(source, fileName, null); parser.parseCompilationUnit(sourceUnit, true, null); }