List of usage examples for org.eclipse.jdt.core.dom CompilationUnit getMessages
public Message[] getMessages()
From source file:com.google.googlejavaformat.java.Formatter.java
License:Apache License
/** * Construct a {@code Formatter} given Java compilation unit. Parses the code; builds a * {@link JavaInput} and the corresponding {@link JavaOutput}. * @param javaInput the input, a Java compilation unit * @param javaOutput the {@link JavaOutput} * @param maxWidth the maximum formatted width * @param errors mutable list to receive errors * @param indentationMultiplier the multiplier for the unit of indent; the default is 1 *///from w w w.ja va 2 s. c om static void format(JavaInput javaInput, JavaOutput javaOutput, int maxWidth, List<FormatterDiagnostic> errors, int indentationMultiplier) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(javaInput.getText().toCharArray()); @SuppressWarnings("unchecked") // safe by specification Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); CompilationUnit unit = (CompilationUnit) parser.createAST(null); javaInput.setCompilationUnit(unit); if (unit.getMessages().length > 0) { for (Message message : unit.getMessages()) { errors.add(javaInput.createDiagnostic(message.getStartPosition(), message.getMessage())); } return; } OpsBuilder builder = new OpsBuilder(javaInput, javaOutput, errors); // Output compilation unit. new JavaInputAstVisitor(builder, indentationMultiplier).visit(unit); builder.sync(javaInput.getText().length()); builder.drain(); Doc doc = new DocBuilder().withOps(builder.build()).build(); doc.computeBreaks(javaOutput.getCommentsHelper(), maxWidth, new Doc.State(+0, 0, 0)); doc.write(javaOutput); javaOutput.flush(); }
From source file:com.google.googlejavaformat.java.RemoveUnusedImports.java
License:Open Source License
private static CompilationUnit parse(String source) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(source.toCharArray()); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options);// ww w. ja va 2s. c o m CompilationUnit unit = (CompilationUnit) parser.createAST(null); if (unit.getMessages().length > 0) { // error handling is done during formatting return null; } return unit; }
From source file:edu.cmu.cs.crystal.tac.eclipse.EclipseTACSimpleTestDriver.java
License:Open Source License
public static CompilationUnit parseCode(String qualifiedCompUnitName, String code) throws CoreException { ASTParser parser = ASTParser.newParser(AST.JLS3); CompilationUnit node; IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("CrystalTest"); project.open(null /* IProgressMonitor */); IJavaProject javaProject = JavaCore.create(project); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setProject(javaProject);//from w w w .ja va2 s . co m parser.setSource(code.toCharArray()); parser.setUnitName("/CrystalTest/" + qualifiedCompUnitName); parser.setResolveBindings(true); node = (CompilationUnit) parser.createAST(null); Message[] msgs = node.getMessages(); if (msgs.length > 0) { StringBuffer errs = new StringBuffer(); errs.append("Compiler problems for "); errs.append(qualifiedCompUnitName); for (Message m : msgs) { errs.append('\n'); errs.append(m.getMessage()); } throw new IllegalArgumentException(errs.toString()); } return node; }
From source file:edu.washington.cs.cupid.jdt.compiler.CompilerMessagesCapability.java
License:Open Source License
@Override public LinearJob<ICompilationUnit, List<Message>> getJob(final ICompilationUnit input) { return new LinearJob<ICompilationUnit, List<Message>>(this, input) { @Override/* www . jav a 2s . c o m*/ protected LinearStatus<List<Message>> run(final IProgressMonitor monitor) { try { monitor.beginTask(getName(), 100); CompilationUnit unit = ParseUtil.parse(input, new SubProgressMonitor(monitor, 100)); return LinearStatus.makeOk(getCapability(), Arrays.asList(unit.getMessages())); } catch (Exception ex) { return LinearStatus.<List<Message>>makeError(ex); } finally { monitor.done(); } } }; }
From source file:org.bundlemaker.core.jdt.internal.parser.JdtAstVisitor.java
License:Open Source License
@Override public boolean visit(CompilationUnit node) { // get messages/problems _messages = node.getMessages(); _problems = node.getProblems();//from w ww. ja v a 2s . c o m // visit the child nodes return !hasErrors(); }
From source file:org.bundlemaker.core.ui.editor.sourceviewer.referencedetail.JdtAstVisitor.java
License:Open Source License
@Override public boolean visit(CompilationUnit node) { // get messages/problems _messages = node.getMessages(); _problems = node.getProblems();/*from w w w.j a va 2s . c om*/ for (IProblem problem : node.getProblems()) { System.out.println(problem); } // visit the child nodes return true; }
From source file:org.modeshape.sequencer.javafile.JdtRecorder.java
License:Apache License
protected void recordCompilerMessages(final CompilationUnit unit, final Node parentNode) throws Exception { final Message[] messages = unit.getMessages(); if ((messages != null) && (messages.length != 0)) { final Node containerNode = parentNode.addNode(ClassFileSequencerLexicon.MESSAGES, ClassFileSequencerLexicon.MESSAGES); for (final Message message : messages) { final Node messageNode = containerNode.addNode(ClassFileSequencerLexicon.MESSAGE, ClassFileSequencerLexicon.MESSAGE); messageNode.setProperty(ClassFileSequencerLexicon.MESSAGE, message.getMessage()); messageNode.setProperty(ClassFileSequencerLexicon.START_POSITION, message.getStartPosition()); messageNode.setProperty(ClassFileSequencerLexicon.LENGTH, message.getLength()); }/*from ww w . jav a2s. c o m*/ } }
From source file:sharpen.ui.tests.BindingTestCase.java
License:Open Source License
private CompilationUnit createAST(String source) throws CoreException { ICompilationUnit cu = _project.createCompilationUnit("", "Bar.java", source); CompilationUnit ast = createAST(cu); assertEquals(toString(ast.getMessages()), 0, ast.getMessages().length); return ast;//from w ww.ja v a 2 s. c o m }