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;
}
use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.
the class StubUtility method getParameterTypeNamesForSeeTag.
/*
* Returns the parameters type names used in see tags. Currently, these are always fully qualified.
*/
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;
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class ReplaceInvocationsRefactoring method resolveSourceProvider.
private SourceProvider resolveSourceProvider(IMethodBinding methodBinding, RefactoringStatus status) throws JavaModelException {
final IMethod method = (IMethod) methodBinding.getJavaElement();
ITypeRoot typeRoot;
IDocument source;
CompilationUnit methodDeclarationAstRoot;
ICompilationUnit methodCu = (method).getCompilationUnit();
if (methodCu != null) {
typeRoot = methodCu;
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(methodCu);
parser.setFocalPosition(method.getNameRange().getOffset());
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
MethodDeclaration methodDecl = (MethodDeclaration) NodeFinder.perform(compilationUnit, method.getNameRange()).getParent();
AST ast = compilationUnit.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
Block newBody = ast.newBlock();
newBody.statements().add(rewrite.createStringPlaceholder(fBody, ASTNode.EMPTY_STATEMENT));
rewrite.replace(methodDecl.getBody(), newBody, null);
List<SingleVariableDeclaration> parameters = methodDecl.parameters();
for (int i = 0; i < parameters.size(); i++) {
SingleVariableDeclaration parameter = parameters.get(i);
rewrite.set(parameter.getName(), SimpleName.IDENTIFIER_PROPERTY, fParameterNames[i], null);
}
TextEdit textEdit = rewrite.rewriteAST();
Document document = new Document(methodCu.getBuffer().getContents());
try {
textEdit.apply(document);
} catch (MalformedTreeException e) {
JavaPlugin.log(e);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
source = document;
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(source.get(), methodCu, true, true, null);
} else {
IClassFile classFile = method.getClassFile();
//TODO: use source if available?
StubCreator stubCreator = new StubCreator(true) {
@Override
protected void appendMethodBody(IMethod currentMethod) throws JavaModelException {
if (currentMethod.equals(method)) {
fBuffer.append(fBody);
} else {
super.appendMethodBody(currentMethod);
}
}
/*
* @see org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator#appendMethodParameterName(org.eclipse.jdt.core.IMethod, int)
*/
@Override
protected void appendMethodParameterName(IMethod currentMethod, int index) {
if (currentMethod.equals(method)) {
fBuffer.append(fParameterNames[index]);
} else {
super.appendMethodParameterName(currentMethod, index);
}
}
};
String stub = stubCreator.createStub(classFile.getType(), null);
source = new Document(stub);
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(stub, classFile, true, true, null);
typeRoot = classFile;
}
ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getKey());
if (node instanceof MethodDeclaration) {
return new SourceProvider(typeRoot, source, (MethodDeclaration) node);
} else {
status.addFatalError(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_find_method_declaration);
return null;
}
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class IntroduceIndirectionRefactoring method checkInitialConditions.
// ********** CONDITION CHECKING **********
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
try {
pm.beginTask(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_activation, 1);
fRewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
if (fTargetMethod == null) {
if (fSelectionStart == 0)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
// if a text selection exists, source is available.
CompilationUnit selectionCURoot;
ASTNode selectionNode;
if (fSelectionCompilationUnit != null) {
// compilation unit - could use CuRewrite later on
selectionCURoot = getCachedCURewrite(fSelectionCompilationUnit).getRoot();
selectionNode = getSelectedNode(fSelectionCompilationUnit, selectionCURoot, fSelectionStart, fSelectionLength);
} else {
// binary class file - no cu rewrite
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setSource(fSelectionClassFile);
selectionCURoot = (CompilationUnit) parser.createAST(null);
selectionNode = getSelectedNode(null, selectionCURoot, fSelectionStart, fSelectionLength);
}
if (selectionNode == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
IMethodBinding targetMethodBinding = null;
if (selectionNode.getNodeType() == ASTNode.METHOD_INVOCATION) {
targetMethodBinding = ((MethodInvocation) selectionNode).resolveMethodBinding();
} else if (selectionNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
targetMethodBinding = ((MethodDeclaration) selectionNode).resolveBinding();
} else if (selectionNode.getNodeType() == ASTNode.SUPER_METHOD_INVOCATION) {
// Allow invocation on super methods calls. makes sense as other
// calls or even only the declaration can be updated.
targetMethodBinding = ((SuperMethodInvocation) selectionNode).resolveMethodBinding();
} else {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
}
// resolve generics
fTargetMethodBinding = targetMethodBinding.getMethodDeclaration();
fTargetMethod = (IMethod) fTargetMethodBinding.getJavaElement();
//allow single updating mode if an invocation was selected and the invocation can be updated
if (selectionNode instanceof MethodInvocation && fSelectionCompilationUnit != null)
fSelectionMethodInvocation = (MethodInvocation) selectionNode;
} else {
if (fTargetMethod.getDeclaringType().isAnnotation())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_annotation);
if (fTargetMethod.getCompilationUnit() != null) {
// source method
CompilationUnit selectionCURoot = getCachedCURewrite(fTargetMethod.getCompilationUnit()).getRoot();
MethodDeclaration declaration = ASTNodeSearchUtil.getMethodDeclarationNode(fTargetMethod, selectionCURoot);
fTargetMethodBinding = declaration.resolveBinding().getMethodDeclaration();
} else {
// binary method - no CURewrite available (and none needed as we cannot update the method anyway)
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(fTargetMethod.getJavaProject());
IBinding[] bindings = parser.createBindings(new IJavaElement[] { fTargetMethod }, null);
fTargetMethodBinding = ((IMethodBinding) bindings[0]).getMethodDeclaration();
}
}
if (fTargetMethod == null || fTargetMethodBinding == null || (!RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(fTargetMethod)))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
if (fTargetMethod.getDeclaringType().isLocal() || fTargetMethod.getDeclaringType().isAnonymous())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_for_local_or_anonymous_types);
if (fTargetMethod.isConstructor())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_for_constructors);
if (fIntermediaryMethodName == null)
fIntermediaryMethodName = fTargetMethod.getElementName();
if (fIntermediaryType == null) {
if (fSelectionCompilationUnit != null && !fSelectionCompilationUnit.isReadOnly())
fIntermediaryType = getEnclosingInitialSelectionMember().getDeclaringType();
else if (!fTargetMethod.isBinary() && !fTargetMethod.isReadOnly())
fIntermediaryType = fTargetMethod.getDeclaringType();
}
return new RefactoringStatus();
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.dom.ASTParser in project buck by facebook.
the class JavaFileParser method makeCompilationUnitFromSource.
private CompilationUnit makeCompilationUnitFromSource(String code) {
ASTParser parser = ASTParser.newParser(jlsLevel);
parser.setSource(code.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(javaVersion, options);
parser.setCompilerOptions(options);
return (CompilationUnit) parser.createAST(/* monitor */
null);
}
Aggregations