/** * Goes through a list of compilation units and parses them. The act of parsing creates the AST structures from the * source code. * * @param compilationUnits the list of compilation units to parse * @return the mapping from compilation unit to the AST roots of each */ public static Map<ICompilationUnit, ASTNode> parseCompilationUnits(List<ICompilationUnit> compilationUnits) { if (compilationUnits == null) { throw new CrystalRuntimeException("null list of compilation units"); } ICompilationUnit[] compUnits = compilationUnits.toArray(new ICompilationUnit[0]); final Map<ICompilationUnit, ASTNode> parsedCompilationUnits = new HashMap<ICompilationUnit, ASTNode>(); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(true); parser.setProject(WorkspaceUtilities.javaProject); parser.createASTs(compUnits, new String[0], new ASTRequestor() { @Override public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { parsedCompilationUnits.put(unit, node); } @Override public final void acceptBinding(final String key, final IBinding binding) { // Do nothing } }, null); return parsedCompilationUnits; }
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(IASTSharedValues.SHARED_AST_LEVEL); parser.setProject(compilationUnit.getJavaProject()); parser.setResolveBindings(true); parser.setStatementsRecovery(true); final Map<String, IBinding> bindings= new HashMap<>(); 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; }
/** * Returns the type binding of the expected type as it is contained in the code completion * context. * * @return the binding of the expected type */ private ITypeBinding getExpectedType() { char[][] chKeys = fInvocationContext.getCoreContext().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 CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setProject(fCompilationUnit.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; }
/** * Creates ASTs for each compilation unit in <code>units</code>. * <p> * <code>ASTRequestor.acceptAST</code> is called in no particular order to * pass the compilation unit and the corresponding AST to <code>requestor</code>. * </p> * <p> * The <code>bindingKeys</code> parameter specifies bindings keys * ({@link IBinding#getKey()}) that are to be looked up. * </p> * * @param compilationUnits the compilation units to create ASTs for * @param bindingKeys the binding keys to create bindings for * @param requestor the AST requestor that collects abstract syntax trees and bindings * @param monitor the progress monitor used to report progress and request cancelation, * or <code>null</code> if none * @see ASTParser#createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor) */ public final void createASTs(ICompilationUnit[] compilationUnits, String[] bindingKeys, ASTRequestor requestor, IProgressMonitor monitor) { if (compilationUnits.length == 0) return; if (monitor == null) monitor= new NullProgressMonitor(); monitor.beginTask("", compilationUnits.length); //$NON-NLS-1$ try { ICompilationUnit[][] splited= splitByProject(compilationUnits); for (int i= 0; i < splited.length; i++) { ICompilationUnit[] units= splited[i]; if (units.length <= MAX_AT_ONCE) { createParser(units[0].getJavaProject()).createASTs(units, bindingKeys, requestor, new SubProgressMonitor(monitor, units.length)); } else { List<ICompilationUnit> list= Arrays.asList(units); int end= 0; int cursor= 0; while (cursor < units.length) { end= Math.min(end + MAX_AT_ONCE, units.length); List<ICompilationUnit> toParse= list.subList(cursor, end); createParser(units[0].getJavaProject()).createASTs(toParse.toArray(new ICompilationUnit[toParse.size()]), bindingKeys, requestor, new SubProgressMonitor(monitor, toParse.size())); cursor= end; } } } } finally { monitor.done(); } }
/** * Returns the type binding of the expected type as it is contained in the * code completion context. * * @return the binding of the expected type */ private ITypeBinding getExpectedType() { char[][] chKeys= fInvocationContext.getCoreContext().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(ASTProvider.SHARED_AST_LEVEL); parser.setProject(fCompilationUnit.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; }
/** * Creates ASTs for each compilation unit in <code>units</code>. * * <p><code>ASTRequestor.acceptAST</code> is called in no particular order to pass the compilation * unit and the corresponding AST to <code>requestor</code>. * * <p>The <code>bindingKeys</code> parameter specifies bindings keys ({@link IBinding#getKey()}) * that are to be looked up. * * @param compilationUnits the compilation units to create ASTs for * @param bindingKeys the binding keys to create bindings for * @param requestor the AST requestor that collects abstract syntax trees and bindings * @param monitor the progress monitor used to report progress and request cancelation, or <code> * null</code> if none * @see ASTParser#createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor) */ public final void createASTs( ICompilationUnit[] compilationUnits, String[] bindingKeys, ASTRequestor requestor, IProgressMonitor monitor) { if (compilationUnits.length == 0) return; if (monitor == null) monitor = new NullProgressMonitor(); monitor.beginTask("", compilationUnits.length); // $NON-NLS-1$ try { ICompilationUnit[][] splited = splitByProject(compilationUnits); for (int i = 0; i < splited.length; i++) { ICompilationUnit[] units = splited[i]; if (units.length <= MAX_AT_ONCE) { createParser(units[0].getJavaProject()) .createASTs( units, bindingKeys, requestor, new SubProgressMonitor(monitor, units.length)); } else { List<ICompilationUnit> list = Arrays.asList(units); int end = 0; int cursor = 0; while (cursor < units.length) { end = Math.min(end + MAX_AT_ONCE, units.length); List<ICompilationUnit> toParse = list.subList(cursor, end); createParser(units[0].getJavaProject()) .createASTs( toParse.toArray(new ICompilationUnit[toParse.size()]), bindingKeys, requestor, new SubProgressMonitor(monitor, toParse.size())); cursor = end; } } } } finally { monitor.done(); } }
/** * {@inheritDoc} */ @Override protected void rewriteTypeOccurrences(final TextEditBasedChangeManager manager, final ASTRequestor requestor, final CompilationUnitRewrite rewrite, final ICompilationUnit unit, final CompilationUnit node, final Set<String> replacements, final IProgressMonitor monitor) throws CoreException { // Not needed }
/** * Creates the text change manager for this processor. * * @param monitor * the progress monitor to display progress * @param status * the refactoring status * @return the created text change manager * @throws JavaModelException * if the method declaration could not be found * @throws CoreException * if the changes could not be generated */ protected final TextEditBasedChangeManager createChangeManager(final IProgressMonitor monitor, final RefactoringStatus status) throws JavaModelException, CoreException { Assert.isNotNull(status); Assert.isNotNull(monitor); try { monitor.beginTask("", 300); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.UseSuperTypeProcessor_creating); final TextEditBasedChangeManager manager= new TextEditBasedChangeManager(); final IJavaProject project= fSubType.getJavaProject(); final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setWorkingCopyOwner(fOwner); parser.setResolveBindings(true); parser.setProject(project); parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project)); if (fSubType.isBinary() || fSubType.isReadOnly()) { final IBinding[] bindings= parser.createBindings(new IJavaElement[] { fSubType, fSuperType }, new SubProgressMonitor(monitor, 50)); if (bindings != null && bindings.length == 2 && bindings[0] instanceof ITypeBinding && bindings[1] instanceof ITypeBinding) { solveSuperTypeConstraints(null, null, fSubType, (ITypeBinding) bindings[0], (ITypeBinding) bindings[1], new SubProgressMonitor(monitor, 100), status); if (!status.hasFatalError()) rewriteTypeOccurrences(manager, null, null, null, null, new HashSet<String>(), status, new SubProgressMonitor(monitor, 150)); } } else { parser.createASTs(new ICompilationUnit[] { fSubType.getCompilationUnit() }, new String[0], new ASTRequestor() { @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)); } } @Override public final void acceptBinding(final String key, final IBinding binding) { // Do nothing } }, new NullProgressMonitor()); } return manager; } finally { monitor.done(); } }
/** * {@inheritDoc} */ @Override protected final void rewriteTypeOccurrences(final TextEditBasedChangeManager manager, final ASTRequestor requestor, final CompilationUnitRewrite rewrite, final ICompilationUnit unit, final CompilationUnit node, final Set<String> replacements, final IProgressMonitor monitor) throws CoreException { try { monitor.beginTask("", 100); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating); final Collection<ITypeConstraintVariable> collection= fTypeOccurrences.get(unit); if (collection != null && !collection.isEmpty()) { final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 100); try { subMonitor.beginTask("", collection.size() * 10); //$NON-NLS-1$ subMonitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating); TType estimate= null; ISourceConstraintVariable variable= null; CompilationUnitRewrite currentRewrite= null; final ICompilationUnit sourceUnit= rewrite.getCu(); if (sourceUnit.equals(unit)) currentRewrite= rewrite; else currentRewrite= new CompilationUnitRewrite(fOwner, unit, node); for (final Iterator<ITypeConstraintVariable> iterator= collection.iterator(); iterator.hasNext();) { variable= iterator.next(); estimate= (TType) variable.getData(SuperTypeConstraintsSolver.DATA_TYPE_ESTIMATE); if (estimate != null && variable instanceof ITypeConstraintVariable) { final ASTNode result= NodeFinder.perform(node, ((ITypeConstraintVariable) variable).getRange().getSourceRange()); if (result != null) rewriteTypeOccurrence(estimate, currentRewrite, result, currentRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.SuperTypeRefactoringProcessor_update_type_occurrence, SET_SUPER_TYPE)); } subMonitor.worked(10); } if (!sourceUnit.equals(unit)) { final TextChange change= currentRewrite.createChange(true); if (change != null) manager.manage(unit, change); } } finally { subMonitor.done(); } } } finally { monitor.done(); } }
/** * {@inheritDoc} */ @Override protected final void rewriteTypeOccurrences(final TextEditBasedChangeManager manager, final ASTRequestor requestor, final CompilationUnitRewrite rewrite, final ICompilationUnit unit, final CompilationUnit node, final Set<String> replacements, final IProgressMonitor monitor) throws CoreException { try { monitor.beginTask("", 100); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating); CompilationUnitRewrite currentRewrite= null; final boolean isSubUnit= rewrite.getCu().equals(unit.getPrimary()); if (isSubUnit) currentRewrite= rewrite; else currentRewrite= new CompilationUnitRewrite(unit, node); final Collection<ITypeConstraintVariable> collection= fTypeOccurrences.get(unit); if (collection != null && !collection.isEmpty()) { final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 100); try { subMonitor.beginTask("", collection.size() * 10); //$NON-NLS-1$ subMonitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating); TType estimate= null; ISourceConstraintVariable variable= null; ITypeConstraintVariable constraint= null; for (final Iterator<ITypeConstraintVariable> iterator= collection.iterator(); iterator.hasNext();) { variable= iterator.next(); if (variable instanceof ITypeConstraintVariable) { constraint= (ITypeConstraintVariable) variable; estimate= (TType) constraint.getData(SuperTypeConstraintsSolver.DATA_TYPE_ESTIMATE); if (estimate != null) { final CompilationUnitRange range= constraint.getRange(); if (isSubUnit) rewriteTypeOccurrence(range, estimate, requestor, currentRewrite, node, replacements, currentRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.SuperTypeRefactoringProcessor_update_type_occurrence, SET_SUPER_TYPE)); else { final ASTNode result= NodeFinder.perform(node, range.getSourceRange()); if (result != null) rewriteTypeOccurrence(estimate, currentRewrite, result, currentRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.SuperTypeRefactoringProcessor_update_type_occurrence, SET_SUPER_TYPE)); } subMonitor.worked(10); } } } } finally { subMonitor.done(); } } if (!isSubUnit) { final TextChange change= currentRewrite.createChange(true); if (change != null) manager.manage(unit, change); } } finally { monitor.done(); } }
/** * {@inheritDoc} */ @Override protected void rewriteTypeOccurrences(final TextEditBasedChangeManager manager, final ASTRequestor requestor, final CompilationUnitRewrite rewrite, final ICompilationUnit unit, final CompilationUnit node, final Set<String> replacements, final IProgressMonitor monitor) throws CoreException { try { monitor.beginTask("", 100); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating); CompilationUnitRewrite currentRewrite= null; final CompilationUnitRewrite existingRewrite= fCompilationUnitRewrites.get(unit.getPrimary()); final boolean isTouched= existingRewrite != null; if (isTouched) currentRewrite= existingRewrite; else currentRewrite= new CompilationUnitRewrite(unit, node); final Collection<ITypeConstraintVariable> collection= fTypeOccurrences.get(unit); if (collection != null && !collection.isEmpty()) { final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 100); try { subMonitor.beginTask("", collection.size() * 10); //$NON-NLS-1$ subMonitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating); TType estimate= null; ISourceConstraintVariable variable= null; ITypeConstraintVariable constraint= null; for (final Iterator<ITypeConstraintVariable> iterator= collection.iterator(); iterator.hasNext();) { variable= iterator.next(); if (variable instanceof ITypeConstraintVariable) { constraint= (ITypeConstraintVariable) variable; estimate= (TType) constraint.getData(SuperTypeConstraintsSolver.DATA_TYPE_ESTIMATE); if (estimate != null) { final CompilationUnitRange range= constraint.getRange(); if (isTouched) rewriteTypeOccurrence(range, estimate, requestor, currentRewrite, node, replacements, currentRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.SuperTypeRefactoringProcessor_update_type_occurrence, SET_SUPER_TYPE)); else { final ASTNode result= NodeFinder.perform(node, range.getSourceRange()); if (result != null) rewriteTypeOccurrence(estimate, currentRewrite, result, currentRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.SuperTypeRefactoringProcessor_update_type_occurrence, SET_SUPER_TYPE)); } subMonitor.worked(10); } } } } finally { subMonitor.done(); } } if (!isTouched) { final TextChange change= currentRewrite.createChange(true); if (change != null) manager.manage(unit, change); } } finally { monitor.done(); } }
protected void rewriteTypeOccurrences(final TextEditBasedChangeManager manager, final CompilationUnitRewrite sourceRewrite, final ICompilationUnit copy, final Set<String> replacements, final RefactoringStatus status, final IProgressMonitor monitor) { try { monitor.beginTask("", 100); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.PullUpRefactoring_checking); final IType declaring= getDeclaringType(); final IJavaProject project= declaring.getJavaProject(); final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setWorkingCopyOwner(fOwner); parser.setResolveBindings(true); parser.setProject(project); parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project)); parser.createASTs(new ICompilationUnit[] { copy}, new String[0], new ASTRequestor() { @Override public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { try { final IType subType= (IType) JavaModelUtil.findInCompilationUnit(unit, declaring); final AbstractTypeDeclaration subDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(subType, node); if (subDeclaration != null) { final ITypeBinding subBinding= subDeclaration.resolveBinding(); if (subBinding != null) { String name= null; ITypeBinding superBinding= null; final ITypeBinding[] superBindings= Bindings.getAllSuperTypes(subBinding); for (int index= 0; index < superBindings.length; index++) { name= superBindings[index].getName(); if (name.startsWith(fDestinationType.getElementName())) superBinding= superBindings[index]; } if (superBinding != null) { solveSuperTypeConstraints(unit, node, subType, subBinding, superBinding, new SubProgressMonitor(monitor, 80), status); if (!status.hasFatalError()) rewriteTypeOccurrences(manager, this, sourceRewrite, unit, node, replacements, status, new SubProgressMonitor(monitor, 120)); } } } } catch (JavaModelException exception) { JavaPlugin.log(exception); status.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractInterfaceProcessor_internal_error)); } } @Override public final void acceptBinding(final String key, final IBinding binding) { // Do nothing } }, new NullProgressMonitor()); } finally { monitor.done(); } }
/** * Creates the necessary text edits to replace the subtype occurrence by a * supertype. * * @param manager * the text change manager to use * @param requestor * the ast requestor to use * @param rewrite * the compilation unit rewrite of the subtype (not in working * copy mode) * @param unit * the compilation unit * @param node * the compilation unit node * @param replacements * the set of variable binding keys of formal parameters which * must be replaced * @param monitor * the progress monitor to use * @throws CoreException * if the change could not be generated */ protected abstract void rewriteTypeOccurrences(TextEditBasedChangeManager manager, ASTRequestor requestor, CompilationUnitRewrite rewrite, ICompilationUnit unit, CompilationUnit node, Set<String> replacements, IProgressMonitor monitor) throws CoreException;