-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from fujaba/feat/plugins
Plugin architecture
- Loading branch information
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.fulib; | ||
|
||
import org.fulib.classmodel.ClassModel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Provides a model for the code generation phase. | ||
* | ||
* @since 1.6 | ||
*/ | ||
public class ClassModelGenerator | ||
{ | ||
private final ClassModel classModel; | ||
private final List<AbstractGenerator> generators = new ArrayList<>(); | ||
|
||
public ClassModelGenerator(ClassModel classModel) | ||
{ | ||
this.classModel = classModel; | ||
} | ||
|
||
public ClassModel getClassModel() | ||
{ | ||
return this.classModel; | ||
} | ||
|
||
public List<AbstractGenerator> getGenerators() | ||
{ | ||
return Collections.unmodifiableList(this.generators); | ||
} | ||
|
||
public ClassModelGenerator withGenerator(AbstractGenerator generator) | ||
{ | ||
this.generators.add(generator); | ||
return this; | ||
} | ||
|
||
public ClassModelGenerator withoutGenerator(AbstractGenerator generator) | ||
{ | ||
this.generators.remove(generator); | ||
return this; | ||
} | ||
|
||
/** | ||
* Applies the given plugin to this generator. | ||
* | ||
* @param plugin | ||
* the plugin to apply | ||
*/ | ||
public void apply(Plugin<? super ClassModelGenerator> plugin) | ||
{ | ||
plugin.apply(this); | ||
} | ||
|
||
/** | ||
* Invokes all generators. | ||
* | ||
* @see AbstractGenerator#generate(ClassModel) | ||
*/ | ||
public void generate() | ||
{ | ||
for (final AbstractGenerator generator : this.generators) | ||
{ | ||
generator.generate(this.classModel); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.fulib; | ||
|
||
/** | ||
* A generic plugin. | ||
* | ||
* @param <T> | ||
* The type of component this plugin can be used with. | ||
* | ||
* @since 1.6 | ||
*/ | ||
public interface Plugin<T> | ||
{ | ||
/** | ||
* Applies this plugin to the component. | ||
* | ||
* @param component | ||
* the target component | ||
*/ | ||
void apply(T component); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.fulib.builder; | ||
|
||
import org.fulib.ClassModelGenerator; | ||
|
||
/** | ||
* ClassModelDecorator specifies hooks for various steps of the fulib lifecycle. | ||
* | ||
* @since 1.6 | ||
*/ | ||
public interface ClassModelDecorator | ||
{ | ||
/** | ||
* Hook for modifying the class meta model. | ||
* | ||
* @param m | ||
* the class model manager | ||
*/ | ||
default void decorate(ClassModelManager m) | ||
{ | ||
} | ||
|
||
/** | ||
* Hook for modifying the code generation phase. | ||
* | ||
* @param generator | ||
* the generator | ||
*/ | ||
default void decorate(ClassModelGenerator generator) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.fulib.builder; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation for specifying which classes in a package are {@link ClassModelDecorator}s. | ||
* | ||
* @since 1.6 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PACKAGE) | ||
public @interface ClassModelDecorators | ||
{ | ||
/** | ||
* @return the classes in this package that implement {@link ClassModelDecorator} | ||
*/ | ||
Class<? extends ClassModelDecorator>[] value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.fulib.tables; | ||
|
||
import org.fulib.ClassModelGenerator; | ||
import org.fulib.Plugin; | ||
import org.fulib.TablesGenerator; | ||
|
||
/** | ||
* A plugin that uses registers the {@link TablesGenerator}. | ||
* | ||
* @since 1.6 | ||
*/ | ||
public class TableGeneratorPlugin implements Plugin<ClassModelGenerator> | ||
{ | ||
@Override | ||
public void apply(ClassModelGenerator component) | ||
{ | ||
component.withGenerator(new TablesGenerator()); | ||
} | ||
} |