public static String getPackageName(ICompilationUnit cu) { if (cu == null) { throw new IllegalArgumentException("cu is null"); } try { IPackageDeclaration[] pkgDecl = cu.getPackageDeclarations(); // TODO: handle default package? if (pkgDecl != null && pkgDecl.length > 0) { String packageName = pkgDecl[0].getElementName(); return packageName; } } catch (JavaModelException e) { } return ""; }
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocationCore problem, Collection<CUCorrectionProposal> 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)); }
public String getPackageName() { String result = null; if (packageFragment != null) { result = packageFragment.getElementName(); } else if (compilationUnit != null) { try { IPackageDeclaration[] pkg = compilationUnit.getPackageDeclarations(); result = pkg.length > 0 ? pkg[0].getElementName() : null; } catch (JavaModelException e) { // ignore } } return result; }
public static Map<String, org.eclipse.jdt.core.ICompilationUnit> getWorkingCopyMap( org.eclipse.jdt.core.ICompilationUnit[] copies) { int length = copies == null ? 0 : copies.length; HashMap<String, org.eclipse.jdt.core.ICompilationUnit> result = new HashMap<>(length); try { if (copies != null) { for (int i = 0; i < length; i++) { org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$ String cuName = workingCopy.getElementName(); String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; result.put(qualifiedMainTypeName, workingCopy); // TODO : JAVA 9 - module-info.java has the same name across modules - Any issues here? } } } catch (JavaModelException e) { // working copy doesn't exist: cannot happen } return result; }
public static Map<String, org.eclipse.jdt.core.ICompilationUnit> getWorkingCopyMap( org.eclipse.jdt.core.ICompilationUnit[] copies) { int length = copies == null ? 0 : copies.length; HashMap<String, org.eclipse.jdt.core.ICompilationUnit> result = new HashMap<>(length); try { if (copies != null) { for (int i = 0; i < length; i++) { org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$ String cuName = workingCopy.getElementName(); String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; result.put(qualifiedMainTypeName, workingCopy); // TODO : JAVA 9 - module-info.java has the same name across modules - Any issues here? } } } catch (JavaModelException e) { // working copy doesn't exist: cannot happen } return result; }
/** * Gets the package name of the given {@link ICompilationUnit}. * * @param icu * The compilation unit for which the package should be returned. * @return The package or empty {@link String} if the compilation unit declares no package * @throws JavaModelException */ public static String getPackage(ICompilationUnit icu) throws JavaModelException { if (!exists(icu)) { return ""; } IPackageDeclaration[] packageDeclarations = icu.getPackageDeclarations(); if (packageDeclarations.length < 1) { return ""; } return packageDeclarations[0].getElementName(); }
public JavaSearchNameEnvironment(IJavaProject javaProject, org.eclipse.jdt.core.ICompilationUnit[] copies) { this.locationSet = computeClasspathLocations((JavaProject) javaProject); try { int length = copies == null ? 0 : copies.length; this.workingCopies = new HashMap(length); if (copies != null) { for (int i = 0; i < length; i++) { org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$ String cuName = workingCopy.getElementName(); String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; this.workingCopies.put(qualifiedMainTypeName, workingCopy); } } } catch (JavaModelException e) { // working copy doesn't exist: cannot happen } }
public JavaSearchNameEnvironment(IJavaProject javaProject, org.eclipse.jdt.core.ICompilationUnit[] copies) { computeClasspathLocations(javaProject.getProject().getWorkspace().getRoot(), (JavaProject) javaProject); try { int length = copies == null ? 0 : copies.length; this.workingCopies = new HashMap(length); if (copies != null) { for (int i = 0; i < length; i++) { org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$ String cuName = workingCopy.getElementName(); String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; this.workingCopies.put(qualifiedMainTypeName, workingCopy); } } } catch (JavaModelException e) { // working copy doesn't exist: cannot happen } }
public JavaSearchNameEnvironment(IJavaProject javaProject, org.eclipse.jdt.core.ICompilationUnit[] copies) { computeClasspathLocations(javaProject.getProject().getWorkspace().getRoot(), (JavaProject) javaProject); try { int length = copies == null ? 0 : copies.length; this.workingCopies = new HashMap(length); if (copies != null) { for (int i = 0; i < length; i++) { org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$ String cuName = workingCopy.getElementName(); String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; this.workingCopies.put(qualifiedMainTypeName, workingCopy); } } } catch (JavaModelException e) { // working copy doesn't exist: cannot happen } }
public JavaSearchNameEnvironment(IJavaProject javaProject, org.eclipse.jdt.core.ICompilationUnit[] copies) { computeClasspathLocations(javaProject.getProject().getWorkspace().getRoot(), (JavaProject) javaProject); try { int length = copies == null ? 0 : copies.length; this.workingCopies = new HashMap(length); if (copies != null) { for (int i = 0; i < length; i++) { org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$ String cuName = workingCopy.getElementName(); String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; this.workingCopies.put(qualifiedMainTypeName, workingCopy); } } } catch (JavaModelException e) { // working copy doesn't exist: cannot happen } }
private void removePackageDeclaration(final ICompilationUnit cu) throws JavaModelException, CoreException { IPackageDeclaration[] packageDeclarations= cu.getPackageDeclarations(); if (packageDeclarations.length != 0) { ITextFileBuffer buffer= null; try { buffer= RefactoringFileBuffers.acquire(cu); ISourceRange sourceRange= packageDeclarations[0].getSourceRange(); buffer.getDocument().replace(sourceRange.getOffset(), sourceRange.getLength(), ""); //$NON-NLS-1$ } catch (BadLocationException e) { JavaPlugin.log(e); } finally { if (buffer != null) RefactoringFileBuffers.release(cu); } } } };
private void removePackageDeclaration(final ICompilationUnit cu) throws JavaModelException, CoreException { IPackageDeclaration[] packageDeclarations= cu.getPackageDeclarations(); if (packageDeclarations.length != 0) { ITextFileBuffer buffer= null; try { buffer= RefactoringFileBuffers.acquire(cu); ISourceRange sourceRange= packageDeclarations[0].getSourceRange(); buffer.getDocument().replace(sourceRange.getOffset(), sourceRange.getLength(), ""); //$NON-NLS-1$ } catch (BadLocationException e) { JavaPlugin.log(e); } finally { if (buffer != null) RefactoringFileBuffers.release(cu); } } } };
public String getDisplayString() { 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, decls[0].getElementName()); } if (!parentPack.isDefaultPackage() && decls.length == 0) { return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, parentPack.getElementName())); } } catch(JavaModelException e) { JavaPlugin.log(e); } return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, parentPack.getElementName())); } }
@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))); }
@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))); } }
@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))); } }
@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())); }
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())); }
@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())); }
@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())); }