|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eclipse.emf.codegen.ecore.generator.Generator
public class Generator
An extensible code generator that delegates its work to model-specific adapters
created
provided by an adapter factory
.
Adapter factories are obtained for model objects from a particular EMF or Java package by consulting a
registry
, which can be local to the generator or globally
shared. Local registries typically delegate
to the
global registry, overriding the adapter factories returned for certain packages. Global registration is usually
done via the org.eclipse.emf.codegen.ecore.generatorAdapters
extension point.
Here is a typical usage example, where we generate code for a standard GenModel-decorated Ecore model:
// Globally register the default generator adapter factory for GenModel // elements (only needed in stand-alone). // GeneratorAdapterFactory.Descriptor.Registry.INSTANCE.addDescriptor (GenModelPackage.eNS_URI, GenModelGeneratorAdapterFactory.DESCRIPTOR); // Create the generator and set the model-level input object. // Generator generator = new Generator(); generator.setInput(genModel); // Generator model code. // generator.generate (genModel, GenBaseGeneratorAdapter.MODEL_PROJECT_TYPE, new BasicMonitor.Printing(System.out));
The adapter factories for the input object handle initializing the generator's options
, and the
generator walks the tree of objects defined by the relevant adapters, invoking code generation for each one.
Nested Class Summary | |
---|---|
static class |
Generator.Options
A set of code generation options that should be shared among the generator, adapter factories and adapters. |
Field Summary | |
---|---|
protected GeneratorAdapterFactory.Descriptor.Registry |
adapterFactoryDescriptorRegistry
The local registry from which generator adapter factories are created. |
protected java.util.Set<java.lang.String> |
badFacadeHelperClasses
|
protected boolean |
initializeNeeded
Whether the adapter factories for the input need to be called to initialize before generating code. |
protected java.lang.Object |
input
|
protected JControlModel |
jControlModel
|
protected Generator.Options |
options
|
protected java.util.Map<java.lang.String,java.util.Collection<GeneratorAdapterFactory>> |
packageIDToAdapterFactories
The cached set of generator adapter factories for this generator, keyed by package ID. |
Constructor Summary | |
---|---|
Generator()
Creates a generator that delegates directly to the global
adapter factory descriptor registry. |
|
Generator(GeneratorAdapterFactory.Descriptor.Registry adapterFactoryDescriptorRegistry)
Creates a generator that delegates to the given adapter factory descriptor registry. |
Method Summary | |
---|---|
protected boolean |
canContinue(Diagnostic diagnostic)
Determines whether code generation can continue, based on the given Diagnostic . |
boolean |
canGenerate(java.lang.Object object,
java.lang.Object projectType)
Returns whether code can be generated for the given object and project type. |
void |
dispose()
Disposes all of the generator's adapter factories, by calling dispose()
on each. |
Diagnostic |
generate(java.lang.Object object,
java.lang.Object projectType,
Monitor monitor)
Performs code generation for the given object and project type. |
Diagnostic |
generate(java.lang.Object object,
java.lang.Object projectType,
java.lang.String projectTypeName,
Monitor monitor)
Performs code generation for the given object and project type. |
protected java.util.Collection<GeneratorAdapterFactory> |
getAdapterFactories(java.lang.Object object)
Returns the generator adapter factories for a given object. |
GeneratorAdapterFactory.Descriptor.Registry |
getAdapterFactoryDescriptorRegistry()
Returns the generator's adapter factory descriptor registry. |
protected java.util.Collection<GeneratorAdapter> |
getAdapters(java.lang.Object object)
Returns the generator adapters for the given object. |
java.lang.Object |
getInput()
Returns the current model-level input object. |
JControlModel |
getJControlModel()
Returns a JControlModel that the generator's adapters can
use for merging. |
Generator.Options |
getOptions()
Returns the Generator.Options for this generator. |
protected java.lang.String |
getPackageID(java.lang.Object object)
Returns the package ID for the given object. |
protected void |
initialize()
Invokes initialize(object) on each adapter factory for the
input object. |
void |
requestInitialize()
Signals that initialize(object) should be called on any adapter
factories for the input object during the next generate(Object, Object, Monitor) or
generate(Object, Object, String, Monitor) , when the full tree of objects and adapters has been discovered. |
void |
setInput(java.lang.Object input)
Sets the model-level input object, and invokes initialize(Object)
on any of its adapter factories. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected GeneratorAdapterFactory.Descriptor.Registry adapterFactoryDescriptorRegistry
protected java.util.Map<java.lang.String,java.util.Collection<GeneratorAdapterFactory>> packageIDToAdapterFactories
protected java.lang.Object input
protected Generator.Options options
protected boolean initializeNeeded
requestInitialize()
protected JControlModel jControlModel
protected java.util.Set<java.lang.String> badFacadeHelperClasses
Constructor Detail |
---|
public Generator()
global
adapter factory descriptor registry.
GeneratorAdapterFactory.Descriptor.Registry.INSTANCE
public Generator(GeneratorAdapterFactory.Descriptor.Registry adapterFactoryDescriptorRegistry)
Method Detail |
---|
public java.lang.Object getInput()
setInput(Object)
public void setInput(java.lang.Object input)
initialize(Object)
on any of its adapter factories. This initialization will also be repeated in the next
generate(Object, Object, Monitor)
or generate(Object, Object, String, Monitor)
, when the full
tree of objects and adapters has been discovered.
getInput()
,
generate(Object, Object, Monitor)
,
generate(Object, Object, String, Monitor)
,
GeneratorAdapterFactory.initialize(Object)
public void requestInitialize()
initialize(object)
should be called on any adapter
factories for the input
object during the next generate(Object, Object, Monitor)
or
generate(Object, Object, String, Monitor)
, when the full tree of objects and adapters has been discovered.
getInput()
,
generate(Object, Object, Monitor)
,
generate(Object, Object, String, Monitor)
,
GeneratorAdapterFactory.initialize(Object)
protected void initialize()
initialize(object)
on each adapter factory for the
input
object. Note that adapter factory order is non-deterministic, so if more than
one tries to set the same options, the result is undefined.
getInput()
,
GeneratorAdapterFactory.initialize(Object)
public Generator.Options getOptions()
Generator.Options
for this generator. Note that these are accessible to (and can be set by) all of the
adapter factories and adapters supporting this generator and by clients of the generator itself.
public JControlModel getJControlModel()
JControlModel
that the generator's adapters can
use for merging. It is initialized with the facade helper class
and
merge rules URI
specified in the options
.
getOptions()
,
Generator.Options.mergerFacadeHelperClass
,
Generator.Options.mergeRulesURI
public GeneratorAdapterFactory.Descriptor.Registry getAdapterFactoryDescriptorRegistry()
protected java.util.Collection<GeneratorAdapterFactory> getAdapterFactories(java.lang.Object object)
registry
for the generator is used to obtain the descriptors
for the object's
package ID
, and those descriptors are used to create the adapter factories, which are
cached.
getAdapterFactoryDescriptorRegistry()
,
getPackageID(Object)
,
GeneratorAdapterFactory.Descriptor
protected java.lang.String getPackageID(java.lang.Object object)
This implementation returns the EPackage nsURI
for an
EObject
, and the Java package name
for any other
object.
EPackage.getNsURI()
,
Package.getName()
protected java.util.Collection<GeneratorAdapter> getAdapters(java.lang.Object object)
getAdapterFactories(Object)
, and each may provide one adapter of type
GeneratorAdapter.class
.
getAdapterFactories(Object)
,
GeneratorAdapter
public boolean canGenerate(java.lang.Object object, java.lang.Object projectType)
This result is obtained as follows:
adapters
for the object are obtained.
getCanGenerateParent(Object, Object)
and
getCanGenerateChildren(Object, Object)
methods
on the adapters for the object, the adapters for the object's parent and children, and so on. It is the
adapters' responsibility to determine the relevant objects through their implementations of these methods.
canGenerate(Object, Object)
method is invoked on all
the adapters for every object in the set formed in step 2. If any adapter returns true
for any
object, this method returns true
.
false
is returned.
GeneratorAdapter.getCanGenerateParent(Object, Object)
,
GeneratorAdapter.getCanGenerateChildren(Object, Object)
,
GeneratorAdapter.canGenerate(Object, Object)
public Diagnostic generate(java.lang.Object object, java.lang.Object projectType, Monitor monitor)
Monitor
. Its final status is returned as a Diagnostic
.
This method operates exactly as generate(Object, Object, String, Monitor)
does when null is specified
as the projectTypeName
.
It is not necessary to call canGenerate(Object, Object)
before this method. If that method would
return false
, this method should generate nothing when invoked with the same arguments.
generate(Object, Object, String, Monitor)
,
canGenerate(Object, Object)
public Diagnostic generate(java.lang.Object object, java.lang.Object projectType, java.lang.String projectTypeName, Monitor monitor)
Monitor
. Its final status is returned as a Diagnostic
.
The projectTypeName
, if non-null, is used only to provide to the Monitor
a more specific
message for the task.
It is not necessary to call canGenerate(Object, Object)
before this method. If that method would
return false
, this method should generate nothing when invoked with the same arguments.
Code generation is performed as follows:
adapters
for the object are obtained.
getGenerateParent(Object, Object)
and
getGenerateChildren(Object, Object)
methods on the
adapters for the object, the adapters for the object's parent and children, and so on. It is the adapters'
responsibility to determine the relevant objects through their implementations of these methods.
generate(Object, Object, String, Monitor)
or initialization has
been requested
, initialize(Object)
is invoked on each adapter factory for the input
object.
preGenerate(Object, Object)
method is invoked on all
the adapters for every object in the set formed in step 2, giving the adapters a chance to perform setup
before any code is generated.
generate(Object, Object, Monitor)
method is
invoked on all the adapters for every object in the set formed in step 2. This is where code generation
actually occurs.
postGenerate(Object, Object)
method is invoked on all
the adapters for every object in the set formed in step 2, giving adapters a chance to clean up from code
generation.
The operation may be canceled during step 4 or 5, either based on the Monitor
or the
Diagnostic
returned by any generator adapter invocation. By default, only a CANCEL
severity
will cause code generation to stop; however,
this can be customized by overriding canContinue(Diagnostic)
. Even if code
generation is canceled, postGenerate(Object, Object)
will
still be called on all the adapters on which preGenerate(Object, Object)
has been called.
canGenerate(Object, Object)
,
requestInitialize()
,
GeneratorAdapter.getGenerateParent(Object, Object)
,
GeneratorAdapter.getGenerateChildren(Object, Object)
,
GeneratorAdapter.preGenerate(Object, Object)
,
GeneratorAdapter.generate(Object, Object, Monitor)
,
GeneratorAdapter.postGenerate(Object, Object)
,
GeneratorAdapterFactory.initialize(Object)
protected boolean canContinue(Diagnostic diagnostic)
Diagnostic
. This implementation
returns true unless the severity
is
CANCEL
.
Diagnostic.getSeverity()
public void dispose()
dispose()
on each.
GeneratorAdapterFactory.dispose()
|
Copyright 2001-2006 IBM Corporation and others. All Rights Reserved. |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |