List of usage examples for org.eclipse.jdt.core.dom NodeFinder perform
public static ASTNode perform(ASTNode root, ISourceRange range)
From source file:com.codenvy.ide.ext.java.server.javadoc.JavadocContentAccess2.java
License:Open Source License
private boolean handleConstantValue(IField field, boolean link) throws JavaModelException { String text = null;// ww w. ja v a2 s. c o m ISourceRange nameRange = field.getNameRange(); if (SourceRange.isAvailable(nameRange)) { CompilationUnit cuNode = ASTProvider.createAST(field.getTypeRoot(), null); if (cuNode != null) { ASTNode nameNode = NodeFinder.perform(cuNode, nameRange); if (nameNode instanceof SimpleName) { IBinding binding = ((SimpleName) nameNode).resolveBinding(); if (binding instanceof IVariableBinding) { IVariableBinding variableBinding = (IVariableBinding) binding; Object constantValue = variableBinding.getConstantValue(); if (constantValue != null) { if (constantValue instanceof String) { text = ASTNodes.getEscapedStringLiteral((String) constantValue); } else { text = constantValue.toString(); // Javadoc tool is even worse for chars... } } } } } } if (text == null) { Object constant = field.getConstant(); if (constant != null) { text = constant.toString(); } } if (text != null) { text = HTMLPrinter.convertToHTMLContentWithWhitespace(text); if (link) { String uri; try { uri = JavaElementLinks.createURI(urlPrefix, field); fBuf.append(JavaElementLinks.createLink(uri, text)); } catch (URISyntaxException e) { LOG.error(e.getMessage(), e); return false; } } else { handleText(text); } return true; } return false; }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * Determines what kind of AST node was selected, and returns an error status * if the kind of node is inappropriate for this refactoring. * @param pm/*from w ww. j a va 2s. c om*/ * @return a RefactoringStatus indicating whether the selection is valid * @throws JavaModelException */ private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException { try { pm.beginTask(RefactoringCoreMessages.IntroduceFactory_examiningSelection, 2); fSelectedNode = getTargetNode(fCUHandle, fSelectionStart, fSelectionLength); if (fSelectedNode == null) { System.out.println(fCUHandle.getSource()); return RefactoringStatus .createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_notAConstructorInvocation); } // getTargetNode() must return either a ClassInstanceCreation or a // constructor MethodDeclaration; nothing else. if (fSelectedNode instanceof ClassInstanceCreation) { ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fSelectedNode; fCtorBinding = classInstanceCreation.resolveConstructorBinding(); } else if (fSelectedNode instanceof MethodDeclaration) { MethodDeclaration methodDeclaration = (MethodDeclaration) fSelectedNode; fCtorBinding = methodDeclaration.resolveBinding(); } if (fCtorBinding == null) return RefactoringStatus.createFatalErrorStatus( RefactoringCoreMessages.IntroduceFactory_unableToResolveConstructorBinding); // If this constructor is of a generic type, get the generic version, // not some instantiation thereof. fCtorBinding = fCtorBinding.getMethodDeclaration(); pm.worked(1); // We don't handle constructors of nested types at the moment if (fCtorBinding.getDeclaringClass().isNested()) return RefactoringStatus .createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unsupportedNestedTypes); ITypeBinding ctorType = fCtorBinding.getDeclaringClass(); IType ctorOwningType = (IType) ctorType.getJavaElement(); if (ctorOwningType.isBinary()) // Can't modify binary CU; don't know what CU to put factory method return RefactoringStatus .createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInBinaryClass); if (ctorOwningType.isEnum()) // Doesn't make sense to encapsulate enum constructors return RefactoringStatus .createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInEnum); // Put the generated factory method inside the type that owns the constructor fFactoryUnitHandle = ctorOwningType.getCompilationUnit(); fFactoryCU = getASTFor(fFactoryUnitHandle); Name ctorOwnerName = (Name) NodeFinder.perform(fFactoryCU, ctorOwningType.getNameRange()); fCtorOwningClass = (AbstractTypeDeclaration) ASTNodes.getParent(ctorOwnerName, AbstractTypeDeclaration.class); fFactoryOwningClass = fCtorOwningClass; pm.worked(1); if (fNewMethodName == null) return setNewMethodName("create" + fCtorBinding.getName());//$NON-NLS-1$ else return new RefactoringStatus(); } finally { pm.done(); } }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * Sets the class on which the generated factory method is to be placed. * @param fullyQualifiedTypeName an <code>IType</code> referring to an existing class * @return return the resulting status/*from w w w. j a v a 2s . c om*/ */ public RefactoringStatus setFactoryClass(String fullyQualifiedTypeName) { IType factoryType; try { factoryType = findFactoryClass(fullyQualifiedTypeName); if (factoryType == null) return RefactoringStatus .createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceFactory_noSuchClass, BasicElementLabels.getJavaElementName(fullyQualifiedTypeName))); if (factoryType.isAnnotation()) return RefactoringStatus.createErrorStatus( RefactoringCoreMessages.IntroduceFactory_cantPutFactoryMethodOnAnnotation); if (factoryType.isInterface()) return RefactoringStatus.createErrorStatus( RefactoringCoreMessages.IntroduceFactory_cantPutFactoryMethodOnInterface); } catch (JavaModelException e) { return RefactoringStatus .createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_cantCheckForInterface); } ICompilationUnit factoryUnitHandle = factoryType.getCompilationUnit(); if (factoryType.isBinary()) return RefactoringStatus .createErrorStatus(RefactoringCoreMessages.IntroduceFactory_cantPutFactoryInBinaryClass); else { try { if (!fFactoryUnitHandle.equals(factoryUnitHandle)) { fFactoryCU = getASTFor(factoryUnitHandle); fFactoryUnitHandle = factoryUnitHandle; } fFactoryOwningClass = (AbstractTypeDeclaration) ASTNodes.getParent( NodeFinder.perform(fFactoryCU, factoryType.getNameRange()), AbstractTypeDeclaration.class); String factoryPkg = factoryType.getPackageFragment().getElementName(); String ctorPkg = fCtorOwningClass.resolveBinding().getPackage().getName(); if (!factoryPkg.equals(ctorPkg)) fConstructorVisibility = Modifier.PUBLIC; else if (fFactoryOwningClass != fCtorOwningClass) fConstructorVisibility = 0; // No such thing as Modifier.PACKAGE... if (fFactoryOwningClass != fCtorOwningClass) fConstructorVisibility = 0; // No such thing as Modifier.PACKAGE... } catch (JavaModelException e) { return RefactoringStatus.createFatalErrorStatus(e.getMessage()); } return new RefactoringStatus(); } }
From source file:org.eclipse.jpt.jpa.core.jpql.spi.JpaType.java
License:Open Source License
protected ITypeBinding buildTypeBinding() { // This code was copied from ASTNodes.getTypeBinding(CompilationUnit, IType) try {/*from www . j a v a2s . c om*/ CompilationUnit compilationUnit = buildCompilationUnit(); if (this.type.isAnonymous()) { IJavaElement parent = this.type.getParent(); if ((parent instanceof IField) && Flags.isEnum(((IMember) parent).getFlags())) { ASTNode node = NodeFinder.perform(compilationUnit, this.type.getNameRange()); EnumConstantDeclaration constant = (EnumConstantDeclaration) node; if (constant != null) { AnonymousClassDeclaration declaration = constant.getAnonymousClassDeclaration(); if (declaration != null) { return declaration.resolveBinding(); } } } else { ASTNode node = NodeFinder.perform(compilationUnit, this.type.getNameRange()); ClassInstanceCreation creation = (ClassInstanceCreation) getParent(node, ClassInstanceCreation.class); if (creation != null) { return creation.resolveTypeBinding(); } } } else { ASTNode node = NodeFinder.perform(compilationUnit, this.type.getNameRange()); AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getParent(node, AbstractTypeDeclaration.class); if (declaration != null) { return declaration.resolveBinding(); } } } catch (Exception e) { // Simply ignore } return null; }
From source file:org.eclipse.objectteams.otdt.debug.ui.internal.actions.OTToggleBreakpointAdapter.java
License:Open Source License
/** * Returns the binary name for the {@link IType} derived from its {@link ITypeBinding}. * <br><br>//from ww w .ja v a2 s .c o m * If the {@link ITypeBinding} cannot be derived this method falls back to calling * {@link #createQualifiedTypeName(IType)} to try and compose the type name. * @param type * @return the binary name for the given {@link IType} * @since 3.6 */ String getQualifiedName(IType type) throws JavaModelException { IJavaProject project = type.getJavaProject(); if (project != null && project.isOnClasspath(type) && needsBindings(type)) { CompilationUnit cuNode = parseCompilationUnit(type.getTypeRoot()); ISourceRange nameRange = type.getNameRange(); if (SourceRange.isAvailable(nameRange)) { ASTNode node = NodeFinder.perform(cuNode, nameRange); if (node instanceof SimpleName) { IBinding binding; if (node.getLocationInParent() == SimpleType.NAME_PROPERTY && node.getParent().getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { binding = ((ClassInstanceCreation) node.getParent().getParent()).resolveTypeBinding(); } else { binding = ((SimpleName) node).resolveBinding(); } if (binding instanceof ITypeBinding) { String name = ((ITypeBinding) binding).getBinaryName(); if (name != null) { return name; } } } } } return createQualifiedTypeName(type); }
From source file:org.eclipse.objectteams.otdt.internal.refactoring.adaptor.pullup.PullUpAdaptor.java
License:Open Source License
static CalloutMappingDeclaration getCalloutDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException { ASTNode node = NodeFinder.perform(cuNode, iMethod.getNameRange()); return (CalloutMappingDeclaration)ASTNodes.getParent(node, CalloutMappingDeclaration.class); }
From source file:org.eclipse.objectteams.otdt.internal.refactoring.util.RefactoringUtil.java
License:Open Source License
public static ASTNode getASTNode(IMember member, Class<? extends ASTNode> nodeClass) { if (member == null || member.getCompilationUnit() == null) { return null; }//from w w w . j a va2s.c o m ICompilationUnit source = member.getCompilationUnit(); ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setSource(source); parser.setResolveBindings(true); CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); // NOTE(SH): cannot use member.getSourceRange() together with the AST: // member uses declarationSourceEnd (incl. comment) whereas // the ASTConverter uses bodyEnd instead (excluding comment/trailing // '}') // However, getNameRange() should be safe. try { ASTNode name = NodeFinder.perform(astRoot, member.getNameRange()); return getParent(name, nodeClass); } catch (JavaModelException e) { return null; } }
From source file:org.eclipse.objectteams.otdt.internal.refactoring.util.RefactoringUtil.java
License:Open Source License
public static MethodDeclaration methodToDeclaration(IMethod method, CompilationUnit node) throws JavaModelException { ICompilationUnit methodCU = (ICompilationUnit) method.getAncestor(IJavaElement.COMPILATION_UNIT); if (!methodCU.equals(node.getJavaElement())) { node = RefactoringASTParser.parseWithASTProvider(methodCU, true, null); }/*from w w w. ja va 2 s.c o m*/ Name result = (Name) NodeFinder.perform(node, method.getNameRange()); return (MethodDeclaration) getParent(result, MethodDeclaration.class); }
From source file:org.eclipse.objectteams.otdt.internal.refactoring.util.RefactoringUtil.java
License:Open Source License
public static AbstractMethodMappingDeclaration methodMappingToDeclaration(IMethodMapping methodMapping, CompilationUnit node) throws JavaModelException { Name result = (Name) NodeFinder.perform(node, methodMapping.getNameRange()); return (AbstractMethodMappingDeclaration) getParent(result, AbstractMethodMappingDeclaration.class); }
From source file:org.eclipse.objectteams.otdt.internal.refactoring.util.RefactoringUtil.java
License:Open Source License
public static ASTNode typeToDeclaration(IType type, CompilationUnit node) throws JavaModelException { Name result = (Name) NodeFinder.perform(node, type.getNameRange()); if (type.isAnonymous()) return getParent(result, AnonymousClassDeclaration.class); return getParent(result, AbstractTypeDeclaration.class); }