org.eclipse.jdt.core.dom.ASTRequestor Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.ASTRequestor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example #2
Source File: LazyGenericTypeProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: PushDownRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@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
}
 
Example #4
Source File: UseSuperTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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();
	}
}
 
Example #5
Source File: UseSuperTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@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();
	}
}
 
Example #6
Source File: ExtractInterfaceProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@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();
	}
}
 
Example #7
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@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();
	}
}
 
Example #8
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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();
	}
}
 
Example #9
Source File: SuperTypeRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * 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;