use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class ChangeSignatureProcessor method isValidVarargsExpression.
public static boolean isValidVarargsExpression(String string) {
String trimmed = string.trim();
if (//speed up for a common case //$NON-NLS-1$
"".equals(trimmed))
return true;
StringBuffer cuBuff = new StringBuffer();
//$NON-NLS-1$
cuBuff.append("class A{ {m(");
int offset = cuBuff.length();
cuBuff.append(trimmed).append(//$NON-NLS-1$
");}}");
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[] selectedNodes = analyzer.getSelectedNodes();
if (selectedNodes.length == 0)
return false;
for (int i = 0; i < selectedNodes.length; i++) {
if (!(selectedNodes[i] instanceof Expression))
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class ASTCreator method getCuNode.
private static CompilationUnit getCuNode(WorkingCopyOwner workingCopyOwner, ICompilationUnit cu) {
ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cu);
p.setResolveBindings(true);
p.setWorkingCopyOwner(workingCopyOwner);
p.setCompilerOptions(RefactoringASTParser.getCompilerOptions(cu));
return (CompilationUnit) p.createAST(null);
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class TypeEnvironment method createStandardType.
private StandardType createStandardType(String fullyQualifiedName, IJavaProject focus) {
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;
}
use of org.eclipse.jdt.core.dom.ASTParser in project generator by mybatis.
the class EclipseDomUtils method getCompilationUnitFromSource.
public static CompilationUnit getCompilationUnitFromSource(String javaSource) {
ASTParser astParser = ASTParser.newParser(AST.JLS8);
Map<?, ?> options = JavaCore.getDefaultOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
astParser.setCompilerOptions(options);
astParser.setSource(javaSource.toCharArray());
CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
return cu;
}
use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.
the class AbstractBuildErrorDetailsHandler method createAST.
/**
* Obtain an AST for a source file in a project
*
* @param javaProject
* @param className
* @return An AST, or null if no source file exists for that class
* @throws JavaModelException
*/
public static final CompilationUnit createAST(IJavaProject javaProject, String className) throws JavaModelException {
IType type = javaProject.findType(className);
if (type == null)
return null;
final ICompilationUnit cunit = type.getCompilationUnit();
if (cunit == null)
// not a source type
return null;
@SuppressWarnings("deprecation") ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(cunit);
parser.setResolveBindings(true);
return (CompilationUnit) parser.createAST(null);
}
Aggregations