本文整理匯總了Java中org.eclipse.jdt.core.dom.ASTParser.createAST方法的典型用法代碼示例。如果您正苦於以下問題:Java ASTParser.createAST方法的具體用法?Java ASTParser.createAST怎麽用?Java ASTParser.createAST使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jdt.core.dom.ASTParser
的用法示例。
在下文中一共展示了ASTParser.createAST方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getField
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* @param methodname
* @return
*/
public FieldDeclaration getField( ) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
String s = getStaticVariable ( );
if (s==null) return null;
parser.setSource(s.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(FieldDeclaration node) {
field = node;
return true;
}
});
return field;
}
示例2: getGraphWalkerClassAnnotation
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* @return
*/
public NormalAnnotation getGraphWalkerClassAnnotation() {
String source = getSource ();
if(source==null) return null;
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(NormalAnnotation node) {
annotation = node;
return true;
}
});
if (this.generateAnnotation) return annotation;
return null;
}
示例3: getGeneratedClassAnnotation
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public NormalAnnotation getGeneratedClassAnnotation() {
String source = getGeneratedAnnotationSource ();
if(source==null) return null;
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(NormalAnnotation node) {
annotation = node;
return true;
}
});
return annotation;
}
示例4: 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);
}
示例5: toGroovyTypeModel
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public TypeModel toGroovyTypeModel(String source) {
source = source.replaceAll("//(?i)\\s*" + quote("given") + SEPARATOR, "givenBlockStart();");
source = source.replaceAll("//(?i)\\s*" + quote("when") + SEPARATOR, "whenBlockStart();");
source = source.replaceAll("//(?i)\\s*" + quote("then") + SEPARATOR, "thenBlockStart();");
ASTParser parser = ASTParser.newParser(JLS8);
parser.setSource(source.toCharArray());
parser.setKind(K_COMPILATION_UNIT);
parser.setCompilerOptions(compilerOptions());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
astProxy.setTarget(cu.getAST());
TypeVisitor visitor = testClassVisitorSupplier.get();
cu.accept(visitor);
return visitor.typeModel();
}
示例6: 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());
}
示例7: parseJava
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
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);
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());
}
示例8: dumpJava
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
protected static void dumpJava(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);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
try {
final UglyMathCommentsExtractor cex = new UglyMathCommentsExtractor(cu, content);
final ASTDumper dumper = new ASTDumper(cex);
dumper.dump(cu);
cex.close();
} catch (final Exception e) {}
}
示例9: _processUnit
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private void _processUnit(ICompilationUnit cu)
throws JavaModelException, MalformedTreeException, BadLocationException {
// Parse the javacode to be able to modify it
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(cu);
// Create a copy of the CompilationUnit to work on
CompilationUnit copyOfUnit = (CompilationUnit)parser.createAST(null);
MemberComparator comparator = new MemberComparator();
// This helper method will sort our java code with the given comparator
TextEdit edits = CompilationUnitSorter.sort(copyOfUnit, comparator, 0, null, null);
// The sort method gives us null if there weren't any changes
if (edits != null) {
ICompilationUnit workingCopy = cu.getWorkingCopy(new WorkingCopyOwner() {}, null);
workingCopy.applyTextEdit(edits, null);
// Commit changes
workingCopy.commitWorkingCopy(true, null);
}
}
示例10: 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;
}
示例11: 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);
}
示例12: 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;
}
示例13: initDomAST
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private void initDomAST() {
if (isReadOnly())
return;
ASTParser parser= ASTParser.newParser(AST.JLS8);
parser.setSource(getCompilationUnit());
parser.setResolveBindings(true);
org.eclipse.jdt.core.dom.ASTNode domAst = parser.createAST(new NullProgressMonitor());
// org.eclipse.jdt.core.dom.AST ast = domAst.getAST();
NodeFinder nf = new NodeFinder(domAst, getCompletionOffset(), 1);
org.eclipse.jdt.core.dom.ASTNode cv = nf.getCoveringNode();
bodyDeclaration = ASTResolving.findParentBodyDeclaration(cv);
parentDeclaration = ASTResolving.findParentType(cv);
domInitialized = true;
}
示例14: initDomAST
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private void initDomAST() {
if (isReadOnly())
return;
ASTParser parser= ASTParser.newParser(AST.JLS4);
parser.setSource(getCompilationUnit());
parser.setResolveBindings(true);
org.eclipse.jdt.core.dom.ASTNode domAst = parser.createAST(new NullProgressMonitor());
// org.eclipse.jdt.core.dom.AST ast = domAst.getAST();
NodeFinder nf = new NodeFinder(domAst, getCompletionOffset(), 1);
org.eclipse.jdt.core.dom.ASTNode cv = nf.getCoveringNode();
bodyDeclaration = ASTResolving.findParentBodyDeclaration(cv);
parentDeclaration = ASTResolving.findParentType(cv);
domInitialized = true;
}
開發者ID:trylimits,項目名稱:Eclipse-Postfix-Code-Completion-Juno38,代碼行數:19,代碼來源:JavaStatementPostfixContext.java
示例15: isValidExpression
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public static boolean isValidExpression(String string){
String trimmed= string.trim();
if ("".equals(trimmed)) //speed up for a common case //$NON-NLS-1$
return false;
StringBuffer cuBuff= new StringBuffer();
cuBuff.append(CONST_CLASS_DECL)
.append("Object") //$NON-NLS-1$
.append(CONST_ASSIGN);
int offset= cuBuff.length();
cuBuff.append(trimmed)
.append(CONST_CLOSE);
ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
CompilationUnit cu= (CompilationUnit) p.createAST(null);
Selection selection= Selection.createFromStartLength(offset, trimmed.length());
SelectionAnalyzer analyzer= new SelectionAnalyzer(selection, false);
cu.accept(analyzer);
ASTNode selected= analyzer.getFirstSelectedNode();
return (selected instanceof Expression) &&
trimmed.equals(cuBuff.substring(cu.getExtendedStartPosition(selected), cu.getExtendedStartPosition(selected) + cu.getExtendedLength(selected)));
}
開發者ID:trylimits,項目名稱:Eclipse-Postfix-Code-Completion-Juno38,代碼行數:22,代碼來源:ChangeSignatureProcessor.java