Eclipse JDT
2.0

org.eclipse.jdt.core
Interface IPackageFragment

All Superinterfaces:
IAdaptable, IJavaElement, IOpenable, IParent, ISourceManipulation

public interface IPackageFragment
extends IParent, IJavaElement, IOpenable, ISourceManipulation

A package fragment is a portion of the workspace corresponding to an entire package, or to a portion thereof. The distinction between a package fragment and a package is that a package with some name is the union of all package fragments in the class path which have the same name.

Package fragments elements need to be opened before they can be navigated or manipulated. The children are of type ICompilationUnit (representing a source file) or IClassFile (representing a binary class file). The children are listed in no particular order.

This interface is not intended to be implemented by clients.


Field Summary
static String DEFAULT_PACKAGE_NAME
           The name of package fragment for the default package (value: the empty string, "").
 
Fields inherited from interface org.eclipse.jdt.core.IJavaElement
CLASS_FILE, COMPILATION_UNIT, FIELD, IMPORT_CONTAINER, IMPORT_DECLARATION, INITIALIZER, JAVA_MODEL, JAVA_PROJECT, METHOD, PACKAGE_DECLARATION, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT, TYPE
 
Method Summary
 boolean containsJavaResources()
          Returns whether this fragment contains at least one Java resource.
 ICompilationUnit createCompilationUnit(String name, String contents, boolean force, IProgressMonitor monitor)
          Creates and returns a compilation unit in this package fragment with the specified name and contents.
 IClassFile getClassFile(String name)
          Returns the class file with the specified name in this package (for example, "Object.class").
 IClassFile[] getClassFiles()
          Returns all of the class files in this package fragment.
 ICompilationUnit getCompilationUnit(String name)
          Returns the compilation unit with the specified name in this package (for example, "Object.java").
 ICompilationUnit[] getCompilationUnits()
          Returns all of the compilation units in this package fragment.
 String getElementName()
          Returns the dot-separated package name of this fragment, for example "java.lang", or "" (the empty string), for the default package.
 int getKind()
          Returns this package fragment's root kind encoded as an integer.
 Object[] getNonJavaResources()
          Returns an array of non-Java resources contained in this package fragment.
 boolean hasSubpackages()
          Returns whether this package fragment's name is a prefix of other package fragments in this package fragment's root.
 boolean isDefaultPackage()
          Returns whether this package fragment is a default package.
 
Methods inherited from interface org.eclipse.jdt.core.IParent
getChildren, hasChildren
 
Methods inherited from interface org.eclipse.jdt.core.IJavaElement
exists, getAncestor, getCorrespondingResource, getElementType, getHandleIdentifier, getJavaModel, getJavaProject, getOpenable, getParent, getPath, getResource, getUnderlyingResource, isReadOnly, isStructureKnown
 
Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapter
 
Methods inherited from interface org.eclipse.jdt.core.IOpenable
close, getBuffer, hasUnsavedChanges, isConsistent, isOpen, makeConsistent, open, save
 
Methods inherited from interface org.eclipse.jdt.core.ISourceManipulation
copy, delete, move, rename
 

Field Detail

DEFAULT_PACKAGE_NAME

public static final String DEFAULT_PACKAGE_NAME

The name of package fragment for the default package (value: the empty string, "").

See Also:
Constant Field Values
Method Detail

containsJavaResources

public boolean containsJavaResources()
                              throws JavaModelException
Returns whether this fragment contains at least one Java resource.

Returns:
true if this fragment contains at least one Java resource, false otherwise
JavaModelException

createCompilationUnit

public ICompilationUnit createCompilationUnit(String name,
                                              String contents,
                                              boolean force,
                                              IProgressMonitor monitor)
                                       throws JavaModelException
Creates and returns a compilation unit in this package fragment with the specified name and contents. No verification is performed on the contents.

It is possible that a compilation unit with the same name already exists in this package fragment. The value of the force parameter effects the resolution of such a conflict:

Parameters:
contents - the given contents
force - specify how to handle conflict is the same name already exists
monitor - the given progress monitor
name - the given name
Returns:
a compilation unit in this package fragment with the specified name and contents
Throws:
JavaModelException - if the element could not be created. Reasons include:
  • This Java element does not exist (ELEMENT_DOES_NOT_EXIST)
  • A CoreException occurred while creating an underlying resource
  • The name is not a valid compilation unit name (INVALID_NAME)
  • The contents are null (INVALID_CONTENTS)

getClassFile

public IClassFile getClassFile(String name)
Returns the class file with the specified name in this package (for example, "Object.class"). The ".class" suffix is required. This is a handle-only method. The class file may or may not be present.

Parameters:
name - the given name
Returns:
the class file with the specified name in this package

getClassFiles

public IClassFile[] getClassFiles()
                           throws JavaModelException
Returns all of the class files in this package fragment.

Note: it is possible that a package fragment contains only compilation units (i.e. its kind is K_SOURCE), in which case this method returns an empty collection.

Returns:
all of the class files in this package fragment
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.

getCompilationUnit

public ICompilationUnit getCompilationUnit(String name)
Returns the compilation unit with the specified name in this package (for example, "Object.java"). The name has to be a valid compilation unit name. This is a handle-only method. The compilation unit may or may not be present.

Parameters:
name - the given name
Returns:
the compilation unit with the specified name in this package
See Also:
JavaConventions.validateCompilationUnitName(java.lang.String)

getCompilationUnits

public ICompilationUnit[] getCompilationUnits()
                                       throws JavaModelException
Returns all of the compilation units in this package fragment.

Note: it is possible that a package fragment contains only class files (i.e. its kind is K_BINARY), in which case this method returns an empty collection.

Returns:
all of the compilation units in this package fragment
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.

getElementName

public String getElementName()
Returns the dot-separated package name of this fragment, for example "java.lang", or "" (the empty string), for the default package.

Specified by:
getElementName in interface IJavaElement
Returns:
the dot-separated package name of this fragment

getKind

public int getKind()
            throws JavaModelException
Returns this package fragment's root kind encoded as an integer. A package fragment can contain .java source files, or .class files. This is a convenience method.

Returns:
this package fragment's root kind encoded as an integer
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.
See Also:
IPackageFragmentRoot.K_SOURCE, IPackageFragmentRoot.K_BINARY

getNonJavaResources

public Object[] getNonJavaResources()
                             throws JavaModelException
Returns an array of non-Java resources contained in this package fragment.

Returns:
an array of non-Java resources contained in this package fragment
JavaModelException

hasSubpackages

public boolean hasSubpackages()
                       throws JavaModelException
Returns whether this package fragment's name is a prefix of other package fragments in this package fragment's root.

Returns:
true if this package fragment's name is a prefix of other package fragments in this package fragment's root, false otherwise
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.

isDefaultPackage

public boolean isDefaultPackage()
Returns whether this package fragment is a default package. This is a handle-only method.

Returns:
true if this package fragment is a default package

Eclipse JDT
2.0

Copyright (c) IBM Corp. and others 2000, 2002. All Rights Reserved.