use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class TypeContextChecker method parseType.
private static Type parseType(String typeString, IJavaProject javaProject, List<String> problemsCollector) {
if (//speed up for a common case //$NON-NLS-1$
"".equals(typeString.trim()))
return null;
if (!typeString.trim().equals(typeString))
return null;
StringBuffer cuBuff = new StringBuffer();
//$NON-NLS-1$
cuBuff.append("interface A{");
int offset = cuBuff.length();
//$NON-NLS-1$
cuBuff.append(typeString).append(" m();}");
ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
p.setProject(javaProject);
CompilationUnit cu = (CompilationUnit) p.createAST(null);
Selection selection = Selection.createFromStartLength(offset, typeString.length());
SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
cu.accept(analyzer);
ASTNode selected = analyzer.getFirstSelectedNode();
if (!(selected instanceof Type))
return null;
Type type = (Type) selected;
if (MethodTypesSyntaxChecker.isVoidArrayType(type))
return null;
IProblem[] problems = ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS);
if (problems.length > 0) {
for (int i = 0; i < problems.length; i++) problemsCollector.add(problems[i].getMessage());
}
String typeNodeRange = cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
if (typeString.equals(typeNodeRange))
return type;
else
return null;
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class InlineConstantRefactoring method initialize.
private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
if (selection != null) {
int offset = -1;
int length = -1;
final StringTokenizer tokenizer = new StringTokenizer(selection);
if (tokenizer.hasMoreTokens())
offset = Integer.valueOf(tokenizer.nextToken()).intValue();
if (tokenizer.hasMoreTokens())
length = Integer.valueOf(tokenizer.nextToken()).intValue();
if (offset >= 0 && length >= 0) {
fSelectionStart = offset;
fSelectionLength = length;
} else
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
}
final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
if (handle != null) {
final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
if (element == null || !element.exists())
return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
else {
if (element instanceof ICompilationUnit) {
fSelectionCu = (ICompilationUnit) element;
if (selection == null)
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
} else if (element instanceof IField) {
final IField field = (IField) element;
try {
final ISourceRange range = field.getNameRange();
if (range != null) {
fSelectionStart = range.getOffset();
fSelectionLength = range.getLength();
} else
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, IJavaRefactorings.INLINE_CONSTANT));
} catch (JavaModelException exception) {
return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
}
fSelectionCu = field.getCompilationUnit();
} else
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT }));
final ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setSource(fSelectionCu);
final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
initialize(fSelectionCu, unit);
if (checkStaticFinalConstantNameSelected().hasFatalError())
return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
}
} else
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
if (replace != null) {
fReplaceAllReferences = Boolean.valueOf(replace).booleanValue();
} else
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
final String remove = arguments.getAttribute(ATTRIBUTE_REMOVE);
if (remove != null)
fRemoveDeclaration = Boolean.valueOf(remove).booleanValue();
else
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REMOVE));
return new RefactoringStatus();
}
use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.
the class CompletionProposalReplacementProvider method getExpectedTypeForGenericParameters.
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(AST.JLS8);
parser.setProject(compilationUnit.getJavaProject());
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
final Map<String, IBinding> bindings = new HashMap<String, IBinding>();
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;
}
use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.
the class ASTResolving method createQuickFixAST.
public static CompilationUnit createQuickFixAST(ICompilationUnit compilationUnit, IProgressMonitor monitor) {
ASTParser astParser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
astParser.setSource(compilationUnit);
astParser.setResolveBindings(true);
astParser.setStatementsRecovery(ASTProvider.SHARED_AST_STATEMENT_RECOVERY);
astParser.setBindingsRecovery(ASTProvider.SHARED_BINDING_RECOVERY);
return (CompilationUnit) astParser.createAST(monitor);
}
use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.
the class ASTNodeFactory method newType.
public static Type newType(AST ast, String content) {
StringBuffer buffer = new StringBuffer(TYPE_HEADER);
buffer.append(content);
buffer.append(TYPE_FOOTER);
ASTParser p = ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root = (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list = root.types();
TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl = typeDecl.getMethods()[0];
ASTNode type = methodDecl.getReturnType2();
ASTNode result = ASTNode.copySubtree(ast, type);
result.accept(new PositionClearer());
return (Type) result;
}
Aggregations