Java Code Examples for org.eclipse.jdt.core.dom.ASTParser#setWorkingCopyOwner()

The following examples show how to use org.eclipse.jdt.core.dom.ASTParser#setWorkingCopyOwner() . 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: JavaHistoryActionImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
static CompilationUnit parsePartialCompilationUnit(ICompilationUnit unit) {

		if (unit == null) {
			throw new IllegalArgumentException();
		}
		try {
			ASTParser c= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
			c.setSource(unit);
			c.setFocalPosition(0);
			c.setResolveBindings(false);
			c.setWorkingCopyOwner(null);
			ASTNode result= c.createAST(null);
			return (CompilationUnit) result;
		} catch (IllegalStateException e) {
			// convert ASTParser's complaints into old form
			throw new IllegalArgumentException();
		}
	}
 
Example 2
Source File: ASTCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static CompilationUnit getCuNode(WorkingCopyOwner workingCopyOwner, ICompilationUnit cu) {
	ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	p.setSource(cu);
	p.setResolveBindings(true);
	p.setWorkingCopyOwner(workingCopyOwner);
	p.setCompilerOptions(RefactoringASTParser.getCompilerOptions(cu));
	return (CompilationUnit) p.createAST(null);
}
 
Example 3
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 4
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();
	}
}