Package org.eclipse.jdt.core

# Interface ISourceReference

All Known Subinterfaces:
  `IAnnotation`, `IClassFile`, `ICompilationUnit`, `IField`, `IImportContainer`, `IImportDeclaration`, `IInitializer`, `ILocalVariable`, `IMember`, `IMethod`, `IModularClassFile`, `IModuleDescription`, `IOrdinaryClassFile`, `IPackageDeclaration`, `IType`, `ITypeParameter`, `ITypeRoot`

public interface ISourceReference
Common protocol for Java elements that have associated source code. This set consists of `IClassFile`, `ICompilationUnit`, `IPackageDeclaration`, `IImportDeclaration`, `IImportContainer`, `IType`, `IField`, `IMethod`, `IInitializer`, `ITypeParameter`, `ILocalVariable`, and `IAnnotation`.

Note: For `IClassFile`, `IType` and other members derived from a binary type, the implementation returns source iff the element has attached source code.

Source reference elements may be working copies if they were created from a compilation unit that is a working copy.

See Also:
  - `IPackageFragmentRoot.attachSource(org.eclipse.core.runtime.IPath, org.eclipse.core.runtime.IPath, org.eclipse.core.runtime.IProgressMonitor)`
Restriction:
  This interface is not intended to be implemented by clients.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`exists()`
Returns whether this element exists in the model.
`ISourceRange`
`getNameRange()`
Returns the name range associated with this element.
`String`
`getSource()`
Returns the source code associated with this element.
`ISourceRange`
`getSourceRange()`
Returns the source range associated with this element.

## Method Details

### exists

boolean exists()
Returns whether this element exists in the model.

Returns:
  `true` if this element exists in the Java model
Since:
  2.0

### getSource

String getSource() throws JavaModelException
Returns the source code associated with this element. This extracts the substring from the source buffer containing this source element. This corresponds to the source range that would be returned by `getSourceRange`.

For class files, this returns the source of the entire compilation unit associated with the class file (if there is one).

Returns:
  the source code, or `null` if this element has no associated source code
Throws:
  `JavaModelException` - if an exception occurs while accessing its corresponding resource

### getSourceRange

ISourceRange getSourceRange() throws JavaModelException
Returns the source range associated with this element.

For class files, this returns the range of the entire compilation unit associated with the class file (if there is one).

If this element has no associated source code, either `null` is returned, or a source range with a -1 offset and a 0 length. `SourceRange.isAvailable(ISourceRange)` can be used to detect that case.

Returns:
  the source range, or either `null` or [-1, 0] if this element has no associated source code
Throws:
  `JavaModelException` - if an exception occurs while accessing its corresponding resource
See Also:
  - `SourceRange.isAvailable(ISourceRange)`

### getNameRange

ISourceRange getNameRange() throws JavaModelException
Returns the name range associated with this element.

If the element is an `IMember`, it returns the source range of this member's simple name, or `null` if this member does not have a name (for example, an initializer), or if this member does not have associated source code (for example, a binary type).

If this element is an `IImportDeclaration`, the source range of this import declaration's name, or `null` if this import declaration does not have associated source code (for example, a binary type).
The source range for the name includes the trailing '*' if the call to `IImportDeclaration.isOnDemand()` returns true.

If this element is an `IPackageDeclaration`, the source range of this package declaration's name, or `null` if this package declaration does not have associated source code (for example, a binary type).

If this element is an `IAnnotation`, the source range of this annotation's name, or `null` if this annotation does not have associated source code (for example, in a binary type).

If this element is an `ITypeParameter`, the source range of this type parameter's name, or `null` if this type parameter does not have associated source code (for example, in a binary type).

If this element is an `ITypeRoot` or `IImportContainer`, it returns null.

Returns:
  the name range associated with this element, or `null` if not available
Throws:
  `JavaModelException`
Since:
  3.7