org.eclipse.jdt.core

## Class WorkingCopyOwner

- java.lang.Object
- - org.eclipse.jdt.core.WorkingCopyOwner

    public abstract class WorkingCopyOwner
    extends Object

The owner of an `ICompilationUnit` handle in working copy mode. An owner is used to identify a working copy and to create its buffer.

Clients should subclass this class to instantiate a working copy owner that is specific to their need and that they can pass in to various APIs (e.g. `IType.resolveType(String, WorkingCopyOwner)`. Clients can also override the default implementation of `createBuffer(ICompilationUnit)`.

Note: even though this class has no abstract method, which means that it provides functional default behavior, it is still an abstract class, as clients are intended to own their owner implementation.

Since:
  3.0
See Also:
  `ICompilationUnit.becomeWorkingCopy(org.eclipse.core.runtime.IProgressMonitor)`, `ICompilationUnit.discardWorkingCopy()`, `ICompilationUnit.getWorkingCopy(org.eclipse.core.runtime.IProgressMonitor)`

### Constructor Summary

Constructors
Constructor and Description
`WorkingCopyOwner()`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
`IBuffer` `createBuffer(ICompilationUnit workingCopy)` Creates a buffer for the given working copy.
`String` `findSource(String typeName, String packageName)` Returns the source of the compilation unit that defines the given type in the given package, or `null` if the type is unknown to this owner.
`IProblemRequestor` `getProblemRequestor(ICompilationUnit workingCopy)` Returns the problem requestor used by a working copy of this working copy owner.
`boolean` `isPackage(String[] pkg)` Returns whether the given package segments represent a package.
`ICompilationUnit` `newWorkingCopy(String name, IClasspathEntry[] classpath, IProblemRequestor problemRequestor, IProgressMonitor monitor)` Deprecated. Use `newWorkingCopy(String, IClasspathEntry[], IProgressMonitor)` instead. Note that if this deprecated method is used, problems may be reported twice if the given requestor is not the same as the current working copy owner one.
`ICompilationUnit` `newWorkingCopy(String name, IClasspathEntry[] classpath, IProgressMonitor monitor)` Returns a new working copy with the given name using this working copy owner to create its buffer.
`static void` `setPrimaryBufferProvider(WorkingCopyOwner primaryBufferProvider)` Sets the buffer provider of the primary working copy owner.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### WorkingCopyOwner

    public WorkingCopyOwner()

### Method Detail

#### setPrimaryBufferProvider

    public static void setPrimaryBufferProvider(WorkingCopyOwner primaryBufferProvider)

Sets the buffer provider of the primary working copy owner. Note that even if the buffer provider is a working copy owner, only its `createBuffer(ICompilationUnit)` method is used by the primary working copy owner. It doesn't replace the internal primary working owner.

This method is for internal use by the jdt-related plug-ins. Clients outside of the jdt should not reference this method.

Parameters:
  `primaryBufferProvider` - the primary buffer provider

#### createBuffer

    public IBuffer createBuffer(ICompilationUnit workingCopy)

Creates a buffer for the given working copy. The new buffer will be initialized with the contents of the underlying file if and only if it was not already initialized by the compilation owner (a buffer is uninitialized if its content is `null`).

Note: This buffer will be associated to the working copy for its entire life-cycle. Another working copy on same unit but owned by a different owner would not share the same buffer unless its owner decided to implement such a sharing behaviour.

Parameters:
  `workingCopy` - the working copy of the buffer
Returns:
  IBuffer the created buffer for the given working copy
See Also:
  `IBuffer`

#### getProblemRequestor

    public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy)

Returns the problem requestor used by a working copy of this working copy owner.

By default, no problem requestor is configured. Clients can override this method to provide a requestor.

Parameters:
  `workingCopy` - The problem requestor used for the given working copy.
Returns:
  the problem requestor to be used by working copies of this working copy owner or `null` if no problem requestor is configured.
Since:
  3.3

#### findSource

    public String findSource(String typeName,
                             String packageName)

Returns the source of the compilation unit that defines the given type in the given package, or `null` if the type is unknown to this owner.

This method is called before the normal lookup (i.e. before looking at the project's classpath and before looking at the working copies of this owner.)

This allows to provide types that are not normally available, or to hide types that would normally be available by returning an empty source for the given type and package.

Example of use:

     WorkingCopyOwner owner = new WorkingCopyOwner() {
       public String findSource(String typeName, String packageName) {
         if ("to.be".equals(packageName) && "Generated".equals(typeName)) {
           return
             "package to.be;\n" +
             "public class Generated {\n" +
             "}";
         }
         return super.findSource(typeName, packageName);
       }
       public boolean isPackage(String[] pkg) {
         switch (pkg.length) {
         case 1:
           return "to".equals(pkg[0]);
         case 2:
           return "to".equals(pkg[0]) && "be".equals(pkg[1]);
         }
         return false;
       }
     };
     // Working copy on X.java with the following contents:
     //    public class X extends to.be.Generated {
     //    }
     ICompilationUnit workingCopy = ...
     ASTParser parser = ASTParser.newParser(AST.JLS3);
     parser.setSource(workingCopy);
     parser.setResolveBindings(true);
     parser.setWorkingCopyOwner(owner);
     CompilationUnit cu = (CompilationUnit) parser.createAST(null);
     assert cu.getProblems().length == 0;

Parameters:
  `typeName` - the simple name of the type to lookup
  `packageName` - the dot-separated name of the package of type
Returns:
  the source of the compilation unit that defines the given type in the given package, or `null` if the type is unknown
Since:
  3.5
See Also:
  `isPackage(String[])`

#### isPackage

    public boolean isPackage(String[] pkg)

Returns whether the given package segments represent a package.

This method is called before the normal lookup (i.e. before looking at the project's classpath and before looking at the working copies of this owner.)

This allows to provide packages that are not normally available.

If `false` is returned, then normal lookup is used on this package.

Parameters:
  `pkg` - the segments of a package to lookup
Returns:
  whether the given package segments represent a package.
Since:
  3.5
See Also:
  `findSource(String, String)`

#### newWorkingCopy

    public final ICompilationUnit newWorkingCopy(String name,
                                                 IClasspathEntry[] classpath,
                                                 IProblemRequestor problemRequestor,
                                                 IProgressMonitor monitor)
                                          throws JavaModelException

Deprecated. Use `newWorkingCopy(String, IClasspathEntry[], IProgressMonitor)` instead. Note that if this deprecated method is used, problems may be reported twice if the given requestor is not the same as the current working copy owner one.
Returns a new working copy with the given name using this working copy owner to create its buffer.

This working copy always belongs to the default package in a package fragment root that corresponds to its Java project, and this Java project never exists. However this Java project has the given classpath that is used when resolving names in this working copy.

A DOM AST created using this working copy will have bindings resolved using the given classpath, and problem are reported to the given problem requestor.

`JavaCore#getOptions()` is used to create the DOM AST as it is not possible to set the options on the non-existing Java project.

When the working copy instance is created, an `added delta` is reported on this working copy.

Once done with the working copy, users of this method must discard it using `ICompilationUnit.discardWorkingCopy()`.

Note that when such working copy is committed, only its buffer is saved (see `IBuffer.save(IProgressMonitor, boolean)`) but no resource is created.

This method is not intended to be overriden by clients.

Parameters:
  `name` - the name of the working copy (e.g. "X.java")
  `classpath` - the classpath used to resolve names in this working copy
  `problemRequestor` - a requestor which will get notified of problems detected during reconciling as they are discovered. The requestor can be set to `null` indicating that the client is not interested in problems.
  `monitor` - a progress monitor used to report progress while opening the working copy or `null` if no progress should be reported
Returns:
  a new working copy
Throws:
  `JavaModelException` - if the contents of this working copy can not be determined.
Since:
  3.2
See Also:
  `ICompilationUnit.becomeWorkingCopy(IProblemRequestor, IProgressMonitor)`

#### newWorkingCopy

    public final ICompilationUnit newWorkingCopy(String name,
                                                 IClasspathEntry[] classpath,
                                                 IProgressMonitor monitor)
                                          throws JavaModelException

Returns a new working copy with the given name using this working copy owner to create its buffer.

This working copy always belongs to the default package in a package fragment root that corresponds to its Java project, and this Java project never exists. However this Java project has the given classpath that is used when resolving names in this working copy.

If a DOM AST is created using this working copy, then given classpath will be used if bindings need to be resolved. Problems will be reported to the problem requestor of the current working copy owner problem if it is not `null`.

Options used to create the DOM AST are got from `JavaCore.getOptions()` as it is not possible to set the options on a non-existing Java project.

When the working copy instance is created, an `added delta` is reported on this working copy.

Once done with the working copy, users of this method must discard it using `ICompilationUnit.discardWorkingCopy()`.

Note that when such working copy is committed, only its buffer is saved (see `IBuffer.save(IProgressMonitor, boolean)`) but no resource is created.

This method is not intended to be overriden by clients.

Parameters:
  `name` - the name of the working copy (e.g. "X.java")
  `classpath` - the classpath used to resolve names in this working copy
  `monitor` - a progress monitor used to report progress while opening the working copy or `null` if no progress should be reported
Returns:
  a new working copy
Throws:
  `JavaModelException` - if the contents of this working copy can not be determined.
Since:
  3.3
See Also:
  `ICompilationUnit.becomeWorkingCopy(IProgressMonitor)`