protected IPackageDeclaration transplantHandle(ICompilationUnit parent, IPackageDeclaration element) { return parent.getPackageDeclaration(element.getElementName()); }
protected IPackageDeclaration transplantHandle(ICompilationUnit parent, IPackageDeclaration element) { return parent.getPackageDeclaration(element.getElementName()); }
protected IPackageDeclaration transplantHandle(ICompilationUnit parent, IPackageDeclaration element) { return parent.getPackageDeclaration(element.getElementName()); }
protected IPackageDeclaration transplantHandle(ICompilationUnit parent, IPackageDeclaration element) { return parent.getPackageDeclaration(element.getElementName()); }
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 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 } }
@Override Set<String> determineImplicitImportContainers(ICompilationUnit compilationUnit) { Set<String> implicitImportContainerNames = new HashSet<String>(); implicitImportContainerNames.add("java.lang"); //$NON-NLS-1$ IJavaElement packageFragment = compilationUnit.getParent(); String compilationUnitPackageName = packageFragment.getElementName(); if (compilationUnitPackageName.isEmpty() && !packageFragment.exists() && compilationUnit.exists()) { /* * For a file outside of the build path, JavaCore#create(IFile) creates an * ICompilationUnit with the file's parent folder as package fragment root, and a default package. * That "wrong" package is problematic for the ImportRewrite, since it doesn't get filtered * and eventually leads to unused import statements. */ try { IPackageDeclaration[] packageDeclarations = compilationUnit.getPackageDeclarations(); if (packageDeclarations.length > 0) { implicitImportContainerNames.add(packageDeclarations[0].getElementName()); return implicitImportContainerNames; } } catch (JavaModelException e) { // continue } } implicitImportContainerNames.add(compilationUnitPackageName); return implicitImportContainerNames; } },
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 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))); } }
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))); } }
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 if (CorrectPackageDeclarationProposal.isValidProposal(cu)) { 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))); } }