List of usage examples for org.eclipse.jdt.core.dom ASTParser setUnitName
public void setUnitName(String unitName)
From source file:br.com.objectos.way.core.code.jdt.AstReaderResource.java
License:Apache License
@Override void setSource(ASTParser parser) throws Exception { String name = resourceName.substring(resourceName.lastIndexOf('/')); parser.setUnitName(name); URL url = Resources.getResource(getClass(), resourceName); Reader reader = Resources.asCharSource(url, Charsets.UTF_8).openStream(); String source = CharStreams.toString(reader); char[] chars = source.toCharArray(); parser.setSource(chars);// w w w . ja v a 2 s .c o m }
From source file:ch.acanda.eclipse.pmd.java.resolution.ASTQuickFixTestCase.java
License:Open Source License
private CompilationUnit createAST(final org.eclipse.jface.text.Document document, final ASTQuickFix<ASTNode> quickFix) { final ASTParser astParser = ASTParser.newParser(AST.JLS4); astParser.setSource(document.get().toCharArray()); astParser.setKind(ASTParser.K_COMPILATION_UNIT); astParser.setResolveBindings(quickFix.needsTypeResolution()); astParser.setEnvironment(new String[0], new String[0], new String[0], true); final String name = last(params.pmdReferenceId.or("QuickFixTest").split("/")); astParser.setUnitName(format("{0}.java", name)); final String version = last(params.language.or("java 1.7").split("\\s+")); astParser.setCompilerOptions(ImmutableMap.<String, String>builder().put(JavaCore.COMPILER_SOURCE, version) .put(JavaCore.COMPILER_COMPLIANCE, version).put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version) .build());//w w w. j a v a 2 s .c om final CompilationUnit ast = (CompilationUnit) astParser.createAST(null); ast.recordModifications(); return ast; }
From source file:com.facebook.nuclide.debugger.EvaluationManager.java
License:Open Source License
private CompilationUnit compileSource(String source, String sourceFilePath, String unitName, StringBuilder errors) throws Exception { if (!unitName.endsWith(".java")) { // The AST compiler is surprisingly insistent about this. unitName += ".java"; }//from w ww . j a v a2 s .c o m final File sourceFile = new File(sourceFilePath); final String directoryPath = sourceFile.getAbsoluteFile().getParent(); Utils.logVerbose("compiling source for: " + directoryPath); final String[] sources = _contextManager.getSourceLocator().getSourceSearchPaths(); final String[] classpath = _contextManager.getSourceLocator().getBinaryJarPaths(); final ASTParser parser = ASTParser.newParser(AST.JLS8); final Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setUnitName(unitName); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setEnvironment(classpath, sources, null, true); parser.setSource(source.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); String errorMsg = checkUnitForProblems(unit); if (errorMsg != null) { errors.append(errorMsg); } return unit; }
From source file:com.google.devtools.j2cpp.GenerationTest.java
License:Open Source License
/** * Compiles Java source, as contained in a source file. * * @param name the name of the public type being declared * @param source the source code//from w ww . java2 s .c om * @param assertErrors assert that no compilation errors were reported * @return the parsed compilation unit */ protected CompilationUnit compileType(String name, String source, boolean assertErrors) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setCompilerOptions(Options.getCompilerOptions()); parser.setSource(source.toCharArray()); parser.setResolveBindings(true); parser.setUnitName(name + ".java"); parser.setEnvironment(new String[] { getComGoogleDevtoolsJ2objcPath() }, new String[] { tempDir.getAbsolutePath() }, null, true); CompilationUnit unit = (CompilationUnit) parser.createAST(null); assertNoCompilationErrors(unit); return unit; }
From source file:com.google.devtools.j2cpp.J2ObjC.java
License:Open Source License
private static CompilationUnit parse(String filename, String source) { logger.finest("parsing " + filename); ASTParser parser = ASTParser.newParser(AST.JLS3); Map<String, String> compilerOptions = Options.getCompilerOptions(); parser.setCompilerOptions(compilerOptions); parser.setSource(source.toCharArray()); parser.setResolveBindings(true);/*from w w w. j a v a 2s. c om*/ setPaths(parser); parser.setUnitName(filename); CompilationUnit unit = (CompilationUnit) parser.createAST(null); for (IProblem problem : getCompilationErrors(unit)) { if (problem.isError()) { error(String.format("%s:%s: %s", filename, problem.getSourceLineNumber(), problem.getMessage())); } } return unit; }
From source file:com.google.devtools.j2objc.J2ObjC.java
License:Open Source License
private static CompilationUnit parse(String filename, String source) { logger.finest("parsing " + filename); ASTParser parser = ASTParser.newParser(AST.JLS4); Map<String, String> compilerOptions = Options.getCompilerOptions(); parser.setCompilerOptions(compilerOptions); parser.setSource(source.toCharArray()); parser.setResolveBindings(true);/*from www . ja va2 s . c om*/ setPaths(parser); parser.setUnitName(filename); CompilationUnit unit = (CompilationUnit) parser.createAST(null); for (IProblem problem : getCompilationErrors(unit)) { if (problem.isError()) { error(String.format("%s:%s: %s", filename, problem.getSourceLineNumber(), problem.getMessage())); } } return unit; }
From source file:com.google.devtools.j2objc.jdt.JdtParser.java
License:Apache License
private CompilationUnit parse(String unitName, String source, boolean resolveBindings) { ASTParser parser = newASTParser(resolveBindings, options.getSourceVersion()); parser.setUnitName(unitName); parser.setSource(source.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); if (checkCompilationErrors(unitName, unit)) { return unit; } else {// www . j a v a2 s. co m return null; } }
From source file:com.google.devtools.j2objc.util.JdtParser.java
License:Apache License
private CompilationUnit parse(String unitName, String source, boolean resolveBindings) { ASTParser parser = newASTParser(resolveBindings); parser.setUnitName(unitName); parser.setSource(source.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); if (checkCompilationErrors(unitName, unit)) { return unit; } else {/*from ww w. j a va 2 s . com*/ return null; } }
From source file:com.google.gwt.eclipse.core.markers.quickfixes.CreateAsyncInterfaceProposal.java
License:Open Source License
@Override public String getAdditionalProposalInfo(IProgressMonitor monitor) { StringBuilder buf = new StringBuilder(); buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinterface_info); buf.append("<br>"); //$NON-NLS-1$ buf.append("<br>"); //$NON-NLS-1$ if (typeContainer instanceof IType) { buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_enclosingtype); } else {/*from w ww .ja va 2s. com*/ buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_package); } buf.append(" <b>"); //$NON-NLS-1$ buf.append(JavaElementLabels.getElementLabel(typeContainer, JavaElementLabels.T_FULLY_QUALIFIED)); buf.append("</b><br>"); //$NON-NLS-1$ buf.append("public "); //$NON-NLS-1$ buf.append("interface <b>"); //$NON-NLS-1$ nameToHTML(typeNameWithParameters, buf); ITypeBinding superclass = getPossibleSuperTypeBinding(node); if (superclass != null) { buf.append("</b> extends <b>"); //$NON-NLS-1$ } buf.append("</b> {"); //$NON-NLS-1$ ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setProject(compilationUnit.getJavaProject()); parser.setResolveBindings(true); StringBuilder sb = new StringBuilder(); IPackageFragment packageFragment = (IPackageFragment) compilationUnit .getAncestor(IJavaElement.PACKAGE_FRAGMENT); if (packageFragment != null) { sb.append("package "); sb.append(packageFragment.getElementName()); sb.append(";\n"); } sb.append("public interface "); sb.append(typeNameWithParameters); sb.append("{}\n"); IPath fullPath = compilationUnit.getResource().getFullPath(); String extension = fullPath.getFileExtension(); fullPath = fullPath.removeFileExtension(); parser.setUnitName(fullPath.toString() + "Async." + extension); parser.setSource(sb.toString().toCharArray()); CompilationUnit astNode = (CompilationUnit) parser.createAST(null); TypeDeclaration asyncTypeDecl = (TypeDeclaration) astNode.types().get(0); List<IMethodBinding> methodsToConvert = NewAsyncRemoteServiceInterfaceCreationWizardPage .computeSyncMethodsThatNeedAsyncVersions(syncTypeBinding, asyncTypeDecl.resolveBinding()); // Cheat, its just a preview anyway NewAsyncRemoteServiceInterfaceCreationWizardPage.ImportManagerAdapter importAdapter = new NewAsyncRemoteServiceInterfaceCreationWizardPage.ImportManagerAdapter() { public String addImport(ITypeBinding typeBinding) { return typeBinding.getName(); } public String addImport(String qualifiedTypeName) { return Signature.getSimpleName(qualifiedTypeName); } }; for (IMethodBinding methodToConvert : methodsToConvert) { try { buf.append("<br><pre> </pre>"); String methodContents = NewAsyncRemoteServiceInterfaceCreationWizardPage.createMethodContents(null, importAdapter, methodToConvert, false); nameToHTML(methodContents, buf); } catch (JavaModelException e) { GWTPluginLog.logError(e); } catch (CoreException e) { GWTPluginLog.logError(e); } } buf.append("<br>}<br>"); //$NON-NLS-1$ return buf.toString(); }
From source file:com.liferay.blade.eclipse.provider.CUCache.java
License:Open Source License
@SuppressWarnings("unchecked") private static CompilationUnit createCompilationUnit(String unitName, char[] javaSource) { ASTParser parser = ASTParser.newParser(AST.JLS8); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); parser.setCompilerOptions(options);//from w w w . j a v a 2s . co m //setUnitName for resolve bindings parser.setUnitName(unitName); String[] sources = { "" }; String[] classpath = { "" }; //setEnvironment for resolve bindings even if the args is empty parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setSource(javaSource); parser.setIgnoreMethodBodies(false); return (CompilationUnit) parser.createAST(null); }