本文整理匯總了Java中org.eclipse.jdt.core.dom.ASTParser.setProject方法的典型用法代碼示例。如果您正苦於以下問題:Java ASTParser.setProject方法的具體用法?Java ASTParser.setProject怎麽用?Java ASTParser.setProject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jdt.core.dom.ASTParser
的用法示例。
在下文中一共展示了ASTParser.setProject方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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());
}
示例2: getParameterTypeNamesForSeeTag
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private static String[] getParameterTypeNamesForSeeTag(IMethod overridden) {
try {
ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setProject(overridden.getJavaProject());
IBinding[] bindings = parser.createBindings(new IJavaElement[] { overridden }, null);
if (bindings.length == 1 && bindings[0] instanceof IMethodBinding) {
return getParameterTypeNamesForSeeTag((IMethodBinding) bindings[0]);
}
} catch (IllegalStateException e) {
// method does not exist
}
// fall back code. Not good for generic methods!
String[] paramTypes = overridden.getParameterTypes();
String[] paramTypeNames = new String[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypeNames[i] = Signature.toString(Signature.getTypeErasure(paramTypes[i]));
}
return paramTypeNames;
}
示例3: parseCompilationUnits
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* Goes through a list of compilation units and parses them. The act of parsing creates the AST structures from the
* source code.
*
* @param compilationUnits the list of compilation units to parse
* @return the mapping from compilation unit to the AST roots of each
*/
public static Map<ICompilationUnit, ASTNode> parseCompilationUnits(List<ICompilationUnit> compilationUnits) {
if (compilationUnits == null) {
throw new CrystalRuntimeException("null list of compilation units");
}
ICompilationUnit[] compUnits = compilationUnits.toArray(new ICompilationUnit[0]);
final Map<ICompilationUnit, ASTNode> parsedCompilationUnits = new HashMap<ICompilationUnit, ASTNode>();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setProject(WorkspaceUtilities.javaProject);
parser.createASTs(compUnits, new String[0], new ASTRequestor() {
@Override
public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) {
parsedCompilationUnits.put(unit, node);
}
@Override
public final void acceptBinding(final String key, final IBinding binding) {
// Do nothing
}
}, null);
return parsedCompilationUnits;
}
示例4: 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;
}
示例5: resolveType
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public static ITypeBinding resolveType(IJavaProject javaProject,
String qualifiedTypeName) throws JavaModelException {
IType type = javaProject.findType(qualifiedTypeName);
if (type == null || !type.exists()) {
return null;
}
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setProject(javaProject);
IBinding[] bindings = parser.createBindings(new IJavaElement[] {type}, null);
if (bindings == null) {
return null;
}
return (ITypeBinding) bindings[0];
}
示例6: 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;
}
示例7: getJavadocNode
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private static Javadoc getJavadocNode(IMember member, String rawJavadoc) {
//FIXME: take from SharedASTProvider if available
//Caveat: Javadoc nodes are not available when Javadoc processing has been disabled!
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
IJavaProject javaProject= member.getJavaProject();
parser.setProject(javaProject);
Map<String, String> options= javaProject.getOptions(true);
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
parser.setCompilerOptions(options);
String source= rawJavadoc + "class C{}"; //$NON-NLS-1$
parser.setSource(source.toCharArray());
CompilationUnit root= (CompilationUnit) parser.createAST(null);
if (root == null)
return null;
List<AbstractTypeDeclaration> types= root.types();
if (types.size() != 1)
return null;
AbstractTypeDeclaration type= types.get(0);
return type.getJavadoc();
}
示例8: constructCUContent
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
/**
* Uses the New Java file template from the code template page to generate a
* compilation unit with the given type content.
*
* @param cu The new created compilation unit
* @param typeContent The content of the type, including signature and type
* body.
* @param lineDelimiter The line delimiter to be used.
* @return String Returns the result of evaluating the new file template
* with the given type content.
* @throws CoreException when fetching the file comment fails or fetching the content for the
* new compilation unit fails
* @since 2.1
*/
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
String fileComment= getFileComment(cu, lineDelimiter);
String typeComment= getTypeComment(cu, lineDelimiter);
IPackageFragment pack= (IPackageFragment) cu.getParent();
String content= CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
if (content != null) {
ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(cu.getJavaProject());
parser.setSource(content.toCharArray());
CompilationUnit unit= (CompilationUnit) parser.createAST(null);
if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
return content;
}
}
StringBuffer buf= new StringBuffer();
if (!pack.isDefaultPackage()) {
buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
}
buf.append(lineDelimiter).append(lineDelimiter);
if (typeComment != null) {
buf.append(typeComment).append(lineDelimiter);
}
buf.append(typeContent);
return buf.toString();
}
示例9: getParameterTypeNamesForSeeTag
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private static String[] getParameterTypeNamesForSeeTag(IMethod overridden) {
try {
ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(overridden.getJavaProject());
IBinding[] bindings= parser.createBindings(new IJavaElement[] { overridden }, null);
if (bindings.length == 1 && bindings[0] instanceof IMethodBinding) {
return getParameterTypeNamesForSeeTag((IMethodBinding)bindings[0]);
}
} catch (IllegalStateException e) {
// method does not exist
}
// fall back code. Not good for generic methods!
String[] paramTypes= overridden.getParameterTypes();
String[] paramTypeNames= new String[paramTypes.length];
for (int i= 0; i < paramTypes.length; i++) {
paramTypeNames[i]= Signature.toString(Signature.getTypeErasure(paramTypes[i]));
}
return paramTypeNames;
}
示例10: createBoxed
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
StandardType createBoxed(PrimitiveType type, IJavaProject focus) {
String fullyQualifiedName= BOXED_PRIMITIVE_NAMES[type.getId()];
try {
IType javaElementType= focus.findType(fullyQualifiedName);
StandardType result= fStandardTypes.get(javaElementType);
if (result != null)
return result;
ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(focus);
IBinding[] bindings= parser.createBindings(new IJavaElement[] {javaElementType} , null);
return createStandardType((ITypeBinding)bindings[0]);
} catch (JavaModelException e) {
// fall through
}
return null;
}
示例11: 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());
}
示例12: parse
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public void parse() {
if (this.member != null) {
if (this.member instanceof IType) {
this.elements.add(this.unit.findDeclaringNode(((IType)this.member).getKey()));
}
else if (this.member instanceof IMethod) {
final ASTParser parser = ASTParser.newParser(4);
parser.setProject(((IMethod)this.member).getJavaProject());
parser.setResolveBindings(true);
final IBinding binding = parser.createBindings(new IJavaElement[] { (IMethod)this.member }, (IProgressMonitor)null)[0];
if (binding instanceof IMethodBinding) {
final ASTNode method = this.unit.findDeclaringNode(((IMethodBinding)binding).getKey());
this.elements.add(method);
}
}
}
else {
for (final Object o : this.unit.types()) {
if (!(o instanceof TypeDeclaration)) {
continue;
}
this.elements.add((ASTNode)o);
}
}
}
示例13: 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);
}
示例14: getExpectedTypeForGenericParameters
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
private ITypeBinding getExpectedTypeForGenericParameters() {
char[][] chKeys= context.getExpectedTypesKeys();
if (chKeys == null || chKeys.length == 0) {
return null;
}
String[] keys= new String[chKeys.length];
for (int i= 0; i < keys.length; i++) {
keys[i]= String.valueOf(chKeys[0]);
}
final ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setProject(compilationUnit.getJavaProject());
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
final Map<String, IBinding> bindings= new HashMap<>();
ASTRequestor requestor= new ASTRequestor() {
@Override
public void acceptBinding(String bindingKey, IBinding binding) {
bindings.put(bindingKey, binding);
}
};
parser.createASTs(new ICompilationUnit[0], keys, requestor, null);
if (bindings.size() > 0) {
return (ITypeBinding) bindings.get(keys[0]);
}
return null;
}
示例15: getPackageName
import org.eclipse.jdt.core.dom.ASTParser; //導入方法依賴的package包/類
public static String getPackageName(IJavaProject javaProject, String fileContent) {
if (fileContent == null) {
return "";
}
//TODO probably not the most efficient way to get the package name as this reads the whole file;
char[] source = fileContent.toCharArray();
ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setProject(javaProject);
parser.setIgnoreMethodBodies(true);
parser.setSource(source);
CompilationUnit ast = (CompilationUnit) parser.createAST(null);
PackageDeclaration pkg = ast.getPackage();
return (pkg == null || pkg.getName() == null)?"":pkg.getName().getFullyQualifiedName();
}