List of usage examples for org.eclipse.jdt.core JavaCore getOptions
public static Hashtable<String, String> getOptions()
From source file:at.bestsolution.fxide.jdt.editor.JDTProposalComputer.java
License:Open Source License
private void restrictVisibility(boolean restrict) { Hashtable<String, String> options = JavaCore.getOptions(); Object value = options.get(VISIBILITY); if (value instanceof String) { String newValue = restrict ? ENABLED : DISABLED; if (!newValue.equals(value)) { options.put(VISIBILITY, newValue); JavaCore.setOptions(options); }// w ww .j a v a 2 s . c o m } }
From source file:boa.datagen.scm.AbstractCommit.java
License:Apache License
private boolean parseJavaScriptFile(final String path, final ChangedFile.Builder fb, final String content, final String compliance, final int astLevel, final boolean storeOnError, Writer astWriter, String key) { try {/*w w w . ja v a 2 s .co m*/ //System.out.println("parsing=" + (++count) + "\t" + path); final org.eclipse.wst.jsdt.core.dom.ASTParser parser = org.eclipse.wst.jsdt.core.dom.ASTParser .newParser(astLevel); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setSource(content.toCharArray()); final Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(compliance, options); parser.setCompilerOptions(options); JavaScriptUnit cu; try { cu = (JavaScriptUnit) parser.createAST(null); } catch (java.lang.IllegalArgumentException ex) { return false; } final JavaScriptErrorCheckVisitor errorCheck = new JavaScriptErrorCheckVisitor(); cu.accept(errorCheck); if (!errorCheck.hasError || storeOnError) { final ASTRoot.Builder ast = ASTRoot.newBuilder(); // final CommentsRoot.Builder comments = // CommentsRoot.newBuilder(); final JavaScriptVisitor visitor = new JavaScriptVisitor(content); try { ast.addNamespaces(visitor.getNamespaces(cu)); // for (final String s : visitor.getImports()) // ast.addImports(s); /* * for (final Comment c : visitor.getComments()) * comments.addComments(c); */ } catch (final UnsupportedOperationException e) { return false; } catch (final Exception e) { if (debug) System.err.println("Error visiting: " + path); //e.printStackTrace(); return false; } if (astWriter != null) { try { // System.out.println("writing=" + count + "\t" + path); astWriter.append(new Text(key), new BytesWritable(ast.build().toByteArray())); } catch (IOException e) { e.printStackTrace(); } } else fb.setAst(ast); // fb.setComments(comments); } return !errorCheck.hasError; } catch (final Exception e) { e.printStackTrace(); return false; } }
From source file:boa.datagen.scm.AbstractCommit.java
License:Apache License
private boolean parseJavaFile(final String path, final ChangedFile.Builder fb, final String content, final String compliance, final int astLevel, final boolean storeOnError, Writer astWriter, String key) { try {// w w w . j a va 2s.c o m final ASTParser parser = ASTParser.newParser(astLevel); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setSource(content.toCharArray()); final Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(compliance, options); parser.setCompilerOptions(options); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); final JavaErrorCheckVisitor errorCheck = new JavaErrorCheckVisitor(); cu.accept(errorCheck); if (!errorCheck.hasError || storeOnError) { final ASTRoot.Builder ast = ASTRoot.newBuilder(); //final CommentsRoot.Builder comments = CommentsRoot.newBuilder(); final JavaVisitor visitor = new JavaVisitor(content, connector.nameIndices); try { ast.addNamespaces(visitor.getNamespaces(cu)); for (final String s : visitor.getImports()) ast.addImports(s); /*for (final Comment c : visitor.getComments()) comments.addComments(c);*/ } catch (final UnsupportedOperationException e) { return false; } catch (final Exception e) { if (debug) System.err.println("Error visiting: " + path); e.printStackTrace(); return false; } if (astWriter != null) { try { astWriter.append(new Text(key), new BytesWritable(ast.build().toByteArray())); } catch (IOException e) { e.printStackTrace(); } } else fb.setAst(ast); //fb.setComments(comments); } return !errorCheck.hasError; } catch (final Exception e) { e.printStackTrace(); return false; } }
From source file:boa.test.datagen.Java7BaseTest.java
License:Apache License
protected static String parseJava(final String content) { final ASTParser parser = ASTParser.newParser(astLevel); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(content.toCharArray()); final Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(javaVersion, options); parser.setCompilerOptions(options);/*w w w . jav a 2 s . c o m*/ final CompilationUnit cu = (CompilationUnit) parser.createAST(null); final ASTRoot.Builder ast = ASTRoot.newBuilder(); try { ast.addNamespaces(visitor.getNamespaces(cu)); for (final String s : visitor.getImports()) ast.addImports(s); } catch (final Exception e) { System.err.println(e); e.printStackTrace(); return ""; } return JsonFormat.printToString(ast.build()); }
From source file:br.uff.ic.gems.resources.ast.ASTExtractor.java
public void parser() throws IOException { ASTParser parser = ASTParser.newParser(AST.JLS3); File file = new File(filePath); String stringFile = FileUtils.readFileToString(file); parser.setSource(stringFile.toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); //Setting options Map options;/*from w w w . j a v a 2 s . c om*/ options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7); parser.setCompilerOptions(options); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); Visitor visitor = new Visitor(cu); cu.accept(visitor); List<Comment> commentList = cu.getCommentList(); for (Comment comment : commentList) { comment.accept(visitor); } languageConstructs = visitor.getLanguageConstructs(); }
From source file:br.uff.ic.mergeguider.javaparser.JavaParser.java
private Storage generateASTs(String path) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true);//from w ww .ja v a 2 s .c om parser.setBindingsRecovery(true); Map options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); parser.setCompilerOptions(options); String[] srcDirs = FileUtils.getAllDirs(path); String[] javaFiles = FileUtils.getAllJavaFiles(path); parser.setEnvironment(null, srcDirs, null, true); Storage storage = new Storage(); parser.createASTs(javaFiles, null, new String[0], storage, null); return storage; }
From source file:chibi.gumtreediff.gen.jdt.AbstractJdtTreeGenerator.java
License:Open Source License
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public TreeContext generate(Reader r) throws IOException { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); Map pOptions = JavaCore.getOptions(); pOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); pOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); pOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); pOptions.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(pOptions); parser.setSource(readerToCharArray(r)); AbstractJdtVisitor v = createVisitor(); CompilationUnit c = (CompilationUnit) parser.createAST(null); v.setTreeRoot(c);// w w w.j a v a2s . c o m c.accept(v); return v.getTreeContext(); }
From source file:com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.java
License:Open Source License
private static String format(IProject project, String contents, IPath to) { String name = to.lastSegment(); if (name.endsWith(DOT_XML)) { XmlFormatStyle formatStyle = EclipseXmlPrettyPrinter.getForFile(to); EclipseXmlFormatPreferences prefs = EclipseXmlFormatPreferences.create(); return EclipseXmlPrettyPrinter.prettyPrint(contents, prefs, formatStyle, null); } else if (name.endsWith(DOT_JAVA)) { Map<?, ?> options = null; if (project != null && project.isAccessible()) { try { IJavaProject javaProject = BaseProjectHelper.getJavaProject(project); if (javaProject != null) { options = javaProject.getOptions(true); }// w w w .ja va 2s.co m } catch (CoreException e) { AdtPlugin.log(e, null); } } if (options == null) { options = JavaCore.getOptions(); } CodeFormatter formatter = ToolFactory.createCodeFormatter(options); try { IDocument doc = new org.eclipse.jface.text.Document(); // format the file (the meat and potatoes) doc.set(contents); TextEdit edit = formatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0, contents.length(), 0, null); if (edit != null) { edit.apply(doc); } return doc.get(); } catch (Exception e) { AdtPlugin.log(e, null); } } return contents; }
From source file:com.bsiag.eclipse.jdt.java.formatter.DefaultCodeFormatter.java
License:Open Source License
private ASTNode parseSourceCode(int kind) { ASTParser parser = ASTParser.newParser(AST.JLS8); Map<String, String> parserOptions = JavaCore.getOptions(); parserOptions.put(CompilerOptions.OPTION_Source, this.sourceLevel); parser.setCompilerOptions(parserOptions); switch (kind & K_MASK) { case K_COMPILATION_UNIT: return parseSourceCode(parser, ASTParser.K_COMPILATION_UNIT, true); case K_CLASS_BODY_DECLARATIONS: return parseSourceCode(parser, ASTParser.K_CLASS_BODY_DECLARATIONS, false); case K_STATEMENTS: return parseSourceCode(parser, ASTParser.K_STATEMENTS, false); case K_EXPRESSION: return parseSourceCode(parser, ASTParser.K_EXPRESSION, false); case K_UNKNOWN: int[] parserModes = { ASTParser.K_COMPILATION_UNIT, ASTParser.K_EXPRESSION, ASTParser.K_CLASS_BODY_DECLARATIONS, ASTParser.K_STATEMENTS }; for (int parserMode : parserModes) { ASTNode astNode = parseSourceCode(parser, parserMode, false); if (astNode != null) return astNode; parser.setCompilerOptions(parserOptions); // parser loses compiler options after every use }// w w w . j av a 2 s.c o m return null; default: throw new IllegalArgumentException(); } }
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 w w . ja va 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(); }