/** * @return an AbstractTypeDeclaration, a CompilationUnit, or null */ private ASTNode getDestinationNodeForSourceElement(IJavaElement destination, int kind, CompilationUnit unit) throws JavaModelException { final IType ancestor= getAncestorType(destination); if (ancestor != null) return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(ancestor, unit); if (kind == IJavaElement.TYPE || kind == IJavaElement.PACKAGE_DECLARATION || kind == IJavaElement.IMPORT_DECLARATION || kind == IJavaElement.IMPORT_CONTAINER) return unit; return null; }
/** * @param destination the destination element * @param kind the type of the source to paste * @param unit the parsed CU * @return an AbstractTypeDeclaration, a CompilationUnit, or null * @throws JavaModelException if something fails */ private ASTNode getDestinationNodeForSourceElement(IJavaElement destination, int kind, CompilationUnit unit) throws JavaModelException { final IType ancestor= getAncestorType(destination); if (ancestor != null) return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(ancestor, unit); if (kind == IJavaElement.TYPE || kind == IJavaElement.PACKAGE_DECLARATION || kind == IJavaElement.IMPORT_DECLARATION || kind == IJavaElement.IMPORT_CONTAINER) return unit; return null; }
/** * @param destination the destination element * @param kind the type of the source to paste * @param unit the parsed CU * @return an AbstractTypeDeclaration, a CompilationUnit, or null * @throws JavaModelException if something fails */ private ASTNode getDestinationNodeForSourceElement(IJavaElement destination, int kind, CompilationUnit unit) throws JavaModelException { final IType ancestor= getAncestorType(destination); if (ancestor != null) return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(ancestor, unit); if (kind == IJavaElement.TYPE || kind == IJavaElement.PACKAGE_DECLARATION || kind == IJavaElement.IMPORT_DECLARATION || kind == IJavaElement.IMPORT_CONTAINER) return unit; return null; }
protected static List getDeclarationNodes(final CompilationUnit cuNode, final List members) throws JavaModelException { final List result= new ArrayList(members.size()); for (final Iterator iterator= members.iterator(); iterator.hasNext();) { final IMember member= (IMember) iterator.next(); ASTNode node= null; if (member instanceof IField) { if (Flags.isEnum(member.getFlags())) node= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) member, cuNode); else node= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, cuNode); } else if (member instanceof IType) node= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, cuNode); else if (member instanceof IMethod) node= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, cuNode); if (node != null) result.add(node); } return result; }
protected static List<ASTNode> getDeclarationNodes(final CompilationUnit cuNode, final List<IMember> members) throws JavaModelException { final List<ASTNode> result= new ArrayList<>(members.size()); for (final Iterator<IMember> iterator= members.iterator(); iterator.hasNext();) { final IMember member= iterator.next(); ASTNode node= null; if (member instanceof IField) { if (Flags.isEnum(member.getFlags())) node= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) member, cuNode); else node= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, cuNode); } else if (member instanceof IType) node= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, cuNode); else if (member instanceof IMethod) node= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, cuNode); if (node != null) result.add(node); } return result; }
protected static List<ASTNode> getDeclarationNodes(final CompilationUnit cuNode, final List<IMember> members) throws JavaModelException { final List<ASTNode> result= new ArrayList<>(members.size()); for (final Iterator<IMember> iterator= members.iterator(); iterator.hasNext();) { final IMember member= iterator.next(); ASTNode node= null; if (member instanceof IField) { if (Flags.isEnum(member.getFlags())) node= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) member, cuNode); else node= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, cuNode); } else if (member instanceof IType) node= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, cuNode); else if (member instanceof IMethod) node= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, cuNode); if (node != null) result.add(node); } return result; }
@Override public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { try { final Collection<IType> types= unitToTypes.get(unit); if (types != null) { for (final Iterator<IType> innerIterator= types.iterator(); innerIterator.hasNext();) { final IType currentType= innerIterator.next(); final AbstractTypeDeclaration currentDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(currentType, node); if (currentDeclaration != null) createModifiedSubType(unit, node, extractedType, extractBindings[0], currentDeclaration, status); } } } catch (CoreException exception) { JavaPlugin.log(exception); status.merge(RefactoringStatus.createFatalErrorStatus(exception.getLocalizedMessage())); } finally { subsubMonitor.worked(1); } }
private static void addTypeParameters(final CompilationUnit unit, final IType type, final Map<String, ITypeBinding> map) throws JavaModelException { Assert.isNotNull(unit); Assert.isNotNull(type); Assert.isNotNull(map); final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, unit); if (declaration instanceof TypeDeclaration) { ITypeBinding binding= null; TypeParameter parameter= null; for (final Iterator<TypeParameter> iterator= ((TypeDeclaration) declaration).typeParameters().iterator(); iterator.hasNext();) { parameter= iterator.next(); binding= parameter.resolveBinding(); if (binding != null && !map.containsKey(binding.getKey())) map.put(binding.getKey(), binding); } final IType declaring= type.getDeclaringType(); if (declaring != null && !JdtFlags.isStatic(type)) addTypeParameters(unit, declaring, map); } }
public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { try { final Collection types= (Collection) unitToTypes.get(unit); if (types != null) { for (final Iterator innerIterator= types.iterator(); innerIterator.hasNext();) { final IType currentType= (IType) innerIterator.next(); final AbstractTypeDeclaration currentDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(currentType, node); if (currentDeclaration != null) createModifiedSubType(unit, node, extractedType, extractBindings[0], currentDeclaration, status); } } } catch (CoreException exception) { JavaPlugin.log(exception); status.merge(RefactoringStatus.createFatalErrorStatus(exception.getLocalizedMessage())); } finally { subsubMonitor.worked(1); } }
@Override public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { try { final Collection<IType> types= unitToTypes.get(unit); if (types != null) { for (final Iterator<IType> innerIterator= types.iterator(); innerIterator.hasNext();) { final IType currentType= innerIterator.next(); final AbstractTypeDeclaration currentDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(currentType, node); if (currentDeclaration != null) createModifiedSubType(unit, node, extractedType, extractBindings[0], currentDeclaration, status); } } } catch (CoreException exception) { JavaPlugin.log(exception); status.merge(RefactoringStatus.createFatalErrorStatus(exception.getLocalizedMessage())); } finally { subsubMonitor.worked(1); } }
private static void addTypeParameters(final CompilationUnit unit, final IType type, final Map<String, ITypeBinding> map) throws JavaModelException { Assert.isNotNull(unit); Assert.isNotNull(type); Assert.isNotNull(map); final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, unit); if (declaration instanceof TypeDeclaration) { ITypeBinding binding= null; TypeParameter parameter= null; for (final Iterator<TypeParameter> iterator= ((TypeDeclaration) declaration).typeParameters().iterator(); iterator.hasNext();) { parameter= iterator.next(); binding= parameter.resolveBinding(); if (binding != null && !map.containsKey(binding.getKey())) map.put(binding.getKey(), binding); } final IType declaring= type.getDeclaringType(); if (declaring != null && !JdtFlags.isStatic(type)) addTypeParameters(unit, declaring, map); } }
private static void addTypeParameters(final CompilationUnit unit, final IType type, final Map map) throws JavaModelException { Assert.isNotNull(unit); Assert.isNotNull(type); Assert.isNotNull(map); final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, unit); if (declaration instanceof TypeDeclaration) { ITypeBinding binding= null; TypeParameter parameter= null; for (final Iterator iterator= ((TypeDeclaration) declaration).typeParameters().iterator(); iterator.hasNext();) { parameter= (TypeParameter) iterator.next(); binding= parameter.resolveBinding(); if (binding != null && !map.containsKey(binding.getKey())) map.put(binding.getKey(), binding); } final IType declaring= type.getDeclaringType(); if (declaring != null && !Flags.isStatic(type.getFlags())) addTypeParameters(unit, declaring, map); } }
public static List<BodyDeclaration> getBodyDeclarationList(IType iType, CompilationUnit cuNode) throws JavaModelException { if (iType.isAnonymous()) return getClassInstanceCreationNode(iType, cuNode).getAnonymousClassDeclaration().bodyDeclarations(); else return getAbstractTypeDeclarationNode(iType, cuNode).bodyDeclarations(); }
public static List getBodyDeclarationList(IType iType, CompilationUnit cuNode) throws JavaModelException { if (iType.isAnonymous()) return getClassInstanceCreationNode(iType, cuNode).getAnonymousClassDeclaration().bodyDeclarations(); else return getAbstractTypeDeclarationNode(iType, cuNode).bodyDeclarations(); }
public static List<BodyDeclaration> getBodyDeclarationList(IType iType, CompilationUnit cuNode) throws JavaModelException { if (iType.isAnonymous()) return getClassInstanceCreationNode(iType, cuNode).getAnonymousClassDeclaration().bodyDeclarations(); else return getAbstractTypeDeclarationNode(iType, cuNode).bodyDeclarations(); }
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException { switch(element.getElementType()){ case IJavaElement.FIELD: return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)}; case IJavaElement.IMPORT_CONTAINER: return getImportNodes((IImportContainer)element, cuNode); case IJavaElement.IMPORT_DECLARATION: return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)}; case IJavaElement.INITIALIZER: return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)}; case IJavaElement.METHOD: return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)}; case IJavaElement.PACKAGE_DECLARATION: return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)}; case IJavaElement.TYPE: return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)}; default: Assert.isTrue(false, String.valueOf(element.getElementType())); return null; } }
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException { switch(element.getElementType()){ case IJavaElement.FIELD: return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)}; case IJavaElement.IMPORT_CONTAINER: return getImportNodes((IImportContainer)element, cuNode); case IJavaElement.IMPORT_DECLARATION: return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)}; case IJavaElement.INITIALIZER: return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)}; case IJavaElement.METHOD: return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)}; case IJavaElement.PACKAGE_DECLARATION: return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)}; case IJavaElement.TYPE: return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)}; default: Assert.isTrue(false, String.valueOf(element.getElementType())); return null; } }
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException { switch(element.getElementType()){ case IJavaElement.FIELD: return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)}; case IJavaElement.IMPORT_CONTAINER: return getImportNodes((IImportContainer)element, cuNode); case IJavaElement.IMPORT_DECLARATION: return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)}; case IJavaElement.INITIALIZER: return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)}; case IJavaElement.METHOD: return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)}; case IJavaElement.PACKAGE_DECLARATION: return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)}; case IJavaElement.TYPE: return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)}; default: Assert.isTrue(false, String.valueOf(element.getElementType())); return null; } }
@Override public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { try { final CompilationUnitRewrite subRewrite= new CompilationUnitRewrite(fOwner, unit, node); final AbstractTypeDeclaration subDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(fSubType, subRewrite.getRoot()); if (subDeclaration != null) { final ITypeBinding subBinding= subDeclaration.resolveBinding(); if (subBinding != null) { final ITypeBinding superBinding= findTypeInHierarchy(subBinding, fSuperType.getFullyQualifiedName('.')); if (superBinding != null) { solveSuperTypeConstraints(subRewrite.getCu(), subRewrite.getRoot(), fSubType, subBinding, superBinding, new SubProgressMonitor(monitor, 100), status); if (!status.hasFatalError()) { rewriteTypeOccurrences(manager, this, subRewrite, subRewrite.getCu(), subRewrite.getRoot(), new HashSet<String>(), status, new SubProgressMonitor(monitor, 200)); final TextChange change= subRewrite.createChange(true); if (change != null) manager.manage(subRewrite.getCu(), change); } } } } } catch (CoreException exception) { JavaPlugin.log(exception); status.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.UseSuperTypeProcessor_internal_error)); } }
protected final void adjustTypeVisibility(final ITypeBinding binding) throws JavaModelException { Assert.isNotNull(binding); final IJavaElement element= binding.getJavaElement(); if (element instanceof IType) { final IType type= (IType) element; if (!type.isBinary() && !type.isReadOnly() && !Flags.isPublic(type.getFlags())) { boolean same= false; final CompilationUnitRewrite rewrite= getCompilationUnitRewrite(fRewrites, type.getCompilationUnit()); final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, rewrite.getRoot()); if (declaration != null) { final ITypeBinding declaring= declaration.resolveBinding(); if (declaring != null && Bindings.equals(declaring.getPackage(), fTarget.getType().getPackage())) same= true; final Modifier.ModifierKeyword keyword= same ? null : Modifier.ModifierKeyword.PUBLIC_KEYWORD; final String modifier= same ? RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_default : RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_public; if (MemberVisibilityAdjustor.hasLowerVisibility(binding.getModifiers(), same ? Modifier.NONE : keyword == null ? Modifier.NONE : keyword.toFlagValue()) && MemberVisibilityAdjustor.needsVisibilityAdjustments(type, keyword, fAdjustments)) fAdjustments.put(type, new MemberVisibilityAdjustor.OutgoingMemberVisibilityAdjustment(type, keyword, RefactoringStatus.createWarningStatus(Messages.format(RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning, new String[] { BindingLabelProvider.getBindingLabel(declaration.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED), modifier }), JavaStatusContext.create(type.getCompilationUnit(), declaration)))); } } } }