Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin architecture #94

Merged
merged 7 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/main/java/org/fulib/ClassModelGenerator.java
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);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/fulib/Plugin.java
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);
}
31 changes: 31 additions & 0 deletions src/main/java/org/fulib/builder/ClassModelDecorator.java
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)
{
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/fulib/builder/ClassModelDecorators.java
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();
}
14 changes: 14 additions & 0 deletions src/main/java/org/fulib/builder/ClassModelManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fulib.builder;

import org.fulib.Fulib;
import org.fulib.Plugin;
import org.fulib.StrUtil;
import org.fulib.classmodel.*;
import org.fulib.util.Validator;
Expand Down Expand Up @@ -243,6 +244,19 @@ public ClassModelBuilder asBuilder()
return new ClassModelBuilder(this.classModel);
}

/**
* Applies the given plugin to this manager.
*
* @param plugin
* the plugin to apply
*
* @since 1.6
*/
public void apply(Plugin<? super ClassModelManager> plugin)
{
plugin.apply(this);
}

// --------------- Settings ---------------

/**
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/fulib/tables/TableGeneratorPlugin.java
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());
}
}