Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#getPackageDeclarations()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#getPackageDeclarations() . 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: CorrectPackageDeclarationProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
	super.addEdits(doc, root);

	ICompilationUnit cu= getCompilationUnit();

	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	IPackageDeclaration[] decls= cu.getPackageDeclarations();

	if (parentPack.isDefaultPackage() && decls.length > 0) {
		for (int i= 0; i < decls.length; i++) {
			ISourceRange range= decls[i].getSourceRange();
			root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
		}
		return;
	}
	if (!parentPack.isDefaultPackage() && decls.length == 0) {
		String lineDelim = "\n";
		String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
		root.addChild(new InsertEdit(0, str));
		return;
	}

	root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
 
Example 2
Source File: CorrectPackageDeclarationProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getName() {
	ICompilationUnit cu= getCompilationUnit();
	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	try {
		IPackageDeclaration[] decls= cu.getPackageDeclarations();
		if (parentPack.isDefaultPackage() && decls.length > 0) {
			return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName()));
		}
		if (!parentPack.isDefaultPackage() && decls.length == 0) {
			return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description,  JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
		}
	} catch(JavaModelException e) {
		JavaLanguageServerPlugin.log(e);
	}
	return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
 
Example 3
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static void convertMoveCompilationUnitChange(WorkspaceEdit edit, MoveCompilationUnitChange change) throws JavaModelException {
	IPackageFragment newPackage = change.getDestinationPackage();
	ICompilationUnit unit = change.getCu();
	CompilationUnit astCU = RefactoringASTParser.parseWithASTProvider(unit, true, new NullProgressMonitor());
	ASTRewrite rewrite = ASTRewrite.create(astCU.getAST());

	IPackageDeclaration[] packDecls = unit.getPackageDeclarations();
	String oldPackageName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
	if (!Objects.equals(oldPackageName, newPackage.getElementName())) {
		// update the package declaration
		if (updatePackageStatement(astCU, newPackage.getElementName(), rewrite, unit)) {
			convertTextEdit(edit, unit, rewrite.rewriteAST());
		}
	}

	RenameFile cuResourceChange = new RenameFile();
	cuResourceChange.setOldUri(JDTUtils.toURI(unit));
	IPath newCUPath = newPackage.getResource().getLocation().append(unit.getPath().lastSegment());
	String newUri = ResourceUtils.fixURI(newCUPath.toFile().toURI());
	cuResourceChange.setNewUri(newUri);
	edit.getDocumentChanges().add(Either.forRight(cuResourceChange));
}
 
Example 4
Source File: CorrectPackageDeclarationProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
	super.addEdits(doc, root);

	ICompilationUnit cu= getCompilationUnit();

	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	IPackageDeclaration[] decls= cu.getPackageDeclarations();

	if (parentPack.isDefaultPackage() && decls.length > 0) {
		for (int i= 0; i < decls.length; i++) {
			ISourceRange range= decls[i].getSourceRange();
			root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
		}
		return;
	}
	if (!parentPack.isDefaultPackage() && decls.length == 0) {
		String lineDelim= StubUtility.getLineDelimiterUsed(cu);
		String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
		root.addChild(new InsertEdit(0, str));
		return;
	}

	root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
 
Example 5
Source File: CorrectPackageDeclarationProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getName() {
	ICompilationUnit cu= getCompilationUnit();
	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	try {
		IPackageDeclaration[] decls= cu.getPackageDeclarations();
		if (parentPack.isDefaultPackage() && decls.length > 0) {
			return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName()));
		}
		if (!parentPack.isDefaultPackage() && decls.length == 0) {
			return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description,  JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
		}
	} catch(JavaModelException e) {
		JavaPlugin.log(e);
	}
	return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
 
Example 6
Source File: ReorgCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocationCore problem,
		Collection<ChangeCorrectionProposal> proposals) throws CoreException {
	ICompilationUnit cu= context.getCompilationUnit();

	// correct package declaration
	int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357
	proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));

	// move to package
	IPackageDeclaration[] packDecls= cu.getPackageDeclarations();
	String newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu);
	IPackageFragment newPack= root.getPackageFragment(newPackName);

	ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName());
	boolean isLinked= cu.getResource().isLinked();
	if (!newCU.exists() && !isLinked) {
		String label;
		if (newPack.isDefaultPackage()) {
			label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
		} else {
			String packageLabel= JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
			label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
		}

		proposals.add(new ChangeCorrectionProposal(label, CodeActionKind.QuickFix, new MoveCompilationUnitChange(cu, newPack), IProposalRelevance.MOVE_CU_TO_PACKAGE));
	}
}
 
Example 7
Source File: WizardUtils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static List<IPackageFragment> getModelPackages(List<IPackageFragment> packageFragments) {
	List<IPackageFragment> modelPackages = new ArrayList<>();
	for (IPackageFragment pFragment : packageFragments) {
		Optional<ICompilationUnit> foundPackageInfoCompilationUnit = Optional
				.of(pFragment.getCompilationUnit(PackageUtils.PACKAGE_INFO));

		if (!foundPackageInfoCompilationUnit.isPresent() || !foundPackageInfoCompilationUnit.get().exists()) {
			continue;
		}

		ICompilationUnit packageInfoCompilationUnit = foundPackageInfoCompilationUnit.get();

		try {
			for (IPackageDeclaration packDecl : packageInfoCompilationUnit.getPackageDeclarations()) {
				for (IAnnotation annot : packDecl.getAnnotations()) {
					boolean isModelPackage = isImportedNameResolvedTo(packageInfoCompilationUnit,
							annot.getElementName(), Model.class.getCanonicalName());

					if (isModelPackage) {
						modelPackages.add(pFragment);
						break;
					}
				}
			}
		} catch (JavaModelException ex) {
			return Collections.emptyList();
		}
	}
	return modelPackages;
}
 
Example 8
Source File: ElementTypeTeller.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean checkPackageInfo(ICompilationUnit compUnit) throws JavaModelException {
	for (IPackageDeclaration packDecl : compUnit.getPackageDeclarations()) {
		for (IAnnotation annot : packDecl.getAnnotations()) {
			// Because names are not resolved in IJavaElement AST
			// representation, we have to manually check if a given
			// annotation is really the Model annotation.
			if (isImportedNameResolvedTo(compUnit, annot.getElementName(), Model.class.getCanonicalName())) {
				return true;
			}
		}
	}
	return false;
}
 
Example 9
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
	ICompilationUnit cu= context.getCompilationUnit();
	boolean isLinked= cu.getResource().isLinked();

	// correct package declaration
	int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357
	proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));

	// move to package
	IPackageDeclaration[] packDecls= cu.getPackageDeclarations();
	String newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu);
	IPackageFragment newPack= root.getPackageFragment(newPackName);

	ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName());
	if (!newCU.exists() && !isLinked) {
		String label;
		if (newPack.isDefaultPackage()) {
			label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
		} else {
			String packageLabel= JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
			label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
		}
		CompositeChange composite= new CompositeChange(label);
		composite.add(new CreatePackageChange(newPack));
		composite.add(new MoveCompilationUnitChange(cu, newPack));

		proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
	}
}