本文整理匯總了Java中org.eclipse.jdt.core.dom.ASTParser.setUnitName方法的典型用法代碼示例。如果您正苦於以下問題:Java ASTParser.setUnitName方法的具體用法?Java ASTParser.setUnitName怎麽用?Java ASTParser.setUnitName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jdt.core.dom.ASTParser
的用法示例。
在下文中一共展示了ASTParser.setUnitName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parse
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public void parse() throws ParseException {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
Map<String, String> options = JavaCore.getOptions();
options.put("org.eclipse.jdt.core.compiler.source", "1.8");
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("Program.java");
parser.setEnvironment(new String[] { classpath != null? classpath : "" },
new String[] { "" }, new String[] { "UTF-8" }, true);
parser.setResolveBindings(true);
cu = (CompilationUnit) parser.createAST(null);
List<IProblem> problems = Arrays.stream(cu.getProblems()).filter(p ->
p.isError() &&
p.getID() != IProblem.PublicClassMustMatchFileName && // we use "Program.java"
p.getID() != IProblem.ParameterMismatch // Evidence varargs
).collect(Collectors.toList());
if (problems.size() > 0)
throw new ParseException(problems);
}
示例2: getRecoveredAST
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) {
CompilationUnit ast = SharedASTProvider.getInstance().getAST(fCompilationUnit, null);
if (ast != null) {
recoveredDocument.set(document.get());
return ast;
}
char[] content= document.get().toCharArray();
// clear prefix to avoid compile errors
int index= offset - 1;
while (index >= 0 && Character.isJavaIdentifierPart(content[index])) {
content[index]= ' ';
index--;
}
recoveredDocument.set(new String(content));
final ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setSource(content);
parser.setUnitName(fCompilationUnit.getElementName());
parser.setProject(fCompilationUnit.getJavaProject());
return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}
示例3: Parse
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private CompilationUnit Parse(String contentFile, String fileName) throws Exception
{
File file = new File(fileName);
IFile[] files = WorkspaceRoot.findFilesForLocationURI(file.toURI(), IResource.FILE);
if (files.length > 1)
throw new Exception("Ambigous parse request for file: " + fileName);
else if (files.length == 0)
throw new Exception("File is not part of the enlistment: " + fileName);
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(contentFile.toCharArray());
parser.setUnitName(files[0].getName());
parser.setProject(JavaCore.create(files[0].getProject()));
parser.setResolveBindings(true);
CompilationUnit cu = (CompilationUnit)parser.createAST(null);
return cu;
}
示例4: prepareAST
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private CompilationUnit prepareAST(String javaFile) throws IOException {
File projectRoot = new File(VALIDATION_EXAMPLES_ROOT).getAbsoluteFile();
File sourceFile = new File(projectRoot.getCanonicalPath() + VALIDATION_EXAMPLES_PACKAGE + javaFile);
ASTParser parser = ASTParser.newParser(AST.JLS8);
char[] content = SharedUtils.getFileContents(sourceFile);
String[] classpath = {};
String[] sourcepath = { projectRoot.getCanonicalPath(), new File(API_SRC_LOC).getCanonicalPath() };
String[] encodings = { "UTF-8", "UTF-8" };
parser.setSource(content);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setUnitName(sourceFile.getName());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setEnvironment(classpath, sourcepath, encodings, true);
return (CompilationUnit) parser.createAST(null);
}
示例5: parseJavaSource
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* Parses the specified Java source file located in the given Java project.
*
* @param sourceFile
* The specified Java source file.
* @param project
* The given Java project.
* @return The parsed compilation unit.
* @throws IOException
* Thrown when I/O error occurs during reading the file.
* @throws JavaModelException
*/
public static CompilationUnit parseJavaSource(File sourceFile, IJavaProject project)
throws IOException, JavaModelException {
ASTParser parser = ASTParser.newParser(AST.JLS8);
char[] content = SharedUtils.getFileContents(sourceFile);
parser.setSource(content);
parser.setProject(project);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setUnitName(sourceFile.getName());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
return compilationUnit;
}
示例6: getRecoveredAST
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) {
CompilationUnit ast= SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
if (ast != null) {
recoveredDocument.set(document.get());
return ast;
}
char[] content= document.get().toCharArray();
// clear prefix to avoid compile errors
int index= offset - 1;
while (index >= 0 && Character.isJavaIdentifierPart(content[index])) {
content[index]= ' ';
index--;
}
recoveredDocument.set(new String(content));
final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setSource(content);
parser.setUnitName(fCompilationUnit.getElementName());
parser.setProject(fCompilationUnit.getJavaProject());
return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}
示例7: parseAndResolveSource
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* Parse a source file, attempting to resolve references in the AST. Valid Java files will usually
* have errors in the CompilationUnit returned by this method, because classes, imports and
* methods will be undefined (because we're only looking at a single file from a whole project).
* Consequently, do not assume the returned object's getProblems() is an empty list. On the other
* hand, {@link @parseSource} returns a CompilationUnit fit for syntax checking purposes.
*/
private static CompilationUnit parseAndResolveSource(String source) {
ASTParser parser = createCompilationUnitParser();
parser.setSource(source.toCharArray());
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setEnvironment(
EMPTY_STRING_ARRAY,
EMPTY_STRING_ARRAY,
EMPTY_STRING_ARRAY,
true /* includeRunningVMBootclasspath */);
parser.setUnitName("dontCare");
return (CompilationUnit) parser.createAST(null);
}
示例8: createCompilationUnit
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private CompilationUnit createCompilationUnit(String classpath) throws IOException {
ASTParser parser = ASTParser.newParser(AST.JLS8);
File input = new File(options.cmdLine.getOptionValue("input-file"));
parser.setSource(FileUtils.readFileToString(input, "utf-8").toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("Program.java");
parser.setEnvironment(new String[] { classpath != null? classpath : "" },
new String[] { "" }, new String[] { "UTF-8" }, true);
parser.setResolveBindings(true);
return (CompilationUnit) parser.createAST(null);
}
示例9: parse
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
parser.setProject(unit.getJavaProject());
parser.setUnitName(unit.getPath().toString());
return (CompilationUnit) parser.createAST(null);
}
示例10: call
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public FactBase call()
throws Exception
{
ASTParser parser = ASTParser.newParser(3);
parser.setResolveBindings(true);
parser.setSource(this.file_);
try
{
parser.setUnitName(this.file_.getUnderlyingResource().getProjectRelativePath().toOSString());
}
catch (JavaModelException localJavaModelException)
{
if (!$assertionsDisabled) {
throw new AssertionError();
}
}
try
{
ASTVisitorAtomicChange acvisitor = new ASTVisitorAtomicChange();
ASTNode ast = parser.createAST(new NullProgressMonitor());
ast.accept(acvisitor);
this.typeToFileMap_.putAll(acvisitor.getTypeToFileMap());
return acvisitor.facts;
}
catch (Exception e)
{
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Exception: " + e.getMessage());
}
return new FactBase();
}
示例11: parse
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public CompilationUnit parse(String unitName, String source) {
ASTParser parser = newASTParser();
parser.setUnitName(unitName);
parser.setSource(source.toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
checkCompilationErrors(unitName, unit);
return unit;
}
示例12: createASTParser
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* Creates a AST-parser
*
* @param cu
* @return the created AST-parser
*/
private static ASTParser createASTParser(ICompilationUnit cu) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setProject(cu.getJavaProject());
parser.setUnitName(cu.getElementName());
parser.setResolveBindings(true);
parser.setSource(cu);
return parser;
}
示例13: Main
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public Main(File javaFile, File infoFile) throws IOException {
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
parser.setCompilerOptions(options);
parser.setEnvironment(classpathEntries, srcpathEntries, null, true);
String canonicalPath = javaFile.getCanonicalPath();
System.out.println(canonicalPath);
parser.setUnitName(canonicalPath);
parser.setSource(toCharArray(canonicalPath));
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
infoFile.getParentFile().mkdirs();
/*
PetabloxAdapterNew visitor = new PetabloxAdapterNew(cu);
cu.accept(visitor);
visitor.writeXml(infoFile);
*/
}
示例14: generate
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
@Override
public AST generate(String filename, String source, String[] srcFolders) {
AST ast = new AST();
ast.setName(filename);
ast.setSource(source);
ASTParser parser = ASTParser.newParser(org.eclipse.jdt.core.dom.AST.JLS8);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setBindingsRecovery(true);
parser.setCompilerOptions(JavaCore.getOptions());
parser.setUnitName(filename);
parser.setSource(source.toCharArray());
parser.setEnvironment(null, srcFolders, null, true);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
FileVisitor visitor = new FileVisitor();
cu.accept(visitor);
ast.setImports(visitor.getImports());
ast.setPackageDeclaration(visitor.getPackageName());
ast.setTypes(visitor.getTypes());
return ast;
}
示例15: process
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public static void process(String srcRootPath, File javaFile) throws IOException {
String canonicalPath = javaFile.getCanonicalPath();
String relSrcFilePath = canonicalPath.substring(srcRootPath.length()+1);
File infoFile = new File(srcMapDir, relSrcFilePath.replace(".java", ".xml"));
if(javaFile.lastModified() < infoFile.lastModified())
return;
infoFile.getParentFile().mkdirs();
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
parser.setCompilerOptions(options);
parser.setEnvironment(classpathEntries, srcpathEntries, null, true);
parser.setUnitName(canonicalPath);
parser.setSource(toCharArray(canonicalPath));
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
System.out.println("generating srcmap file for "+canonicalPath);
PetabloxAdapterNew visitor = new PetabloxAdapterNew(cu);
try {
cu.accept(visitor);
visitor.writeXML(infoFile);
} catch(Exception e) {
System.out.println("Failed to generate srcmap file for "+relSrcFilePath);
e.printStackTrace();
infoFile.delete();
}
/*
try {
XMLContainerObject object = visitor.getXMLObject();
File objectFile = new File(srcMapDir, relSrcFilePath.replace(".java", ".obj"));
System.out.println("PRINTING XML OBJECT FOR " + javaFile.getName() + " TO " + objectFile.getCanonicalPath());
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(objectFile));
oos.writeObject(object);
oos.close();
} catch(IOException e) {
System.out.println("FAILED TO PRINT XML OBJECT FOR " + javaFile.getName() + "!");
e.printStackTrace();
}
*/
AnnotationReader annotReader = new AnnotationReader(canonicalPath);
cu.accept(annotReader);
}