GitHub Created At GitHub last commit Jitpack GitHub Release Date Made by Tulis

What is it?

It's a Java project for initializing classes automatically, a little like Spring uses Beans. You can avoid a mess like this:

new Command();
new Command2();

new Event();
new Event2();

and instead use @Init annotation to initialize the class (using your constructor configuration) automatically:

new AutomaticInitializer.Builder()
        .setPackageName("(the main package to look for classes)")
        .addInitVariable(Integer.class, 123) // You can add as many as you want
        .addInitVariable(MyMainClass.class, mainClassObject) // Or none if you want the default constructor
        .run();

and for the class:

@Init
public class Test {
    public Test(Integer i, MyMainClass myMainClass) {
        System.out.println("Hi from test! My int value: " + i);
        myMainClass.registerEvent(this); // You can initialize the event here for example!
    }
}

Installation

Install TuliAutoInitializer by Maven or Gradle using Jitpack, or my self-hosted gitea (on git.tulisiowice.top):

Repos:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<repositories>
    <repository>
        <id>gitea</id>
        <url>https://git.tulisiowice.top/api/packages/Tulis/maven</url>
    </repository>
</repositories>

The package:

<dependency> // Jitpack
    <groupId>com.github.Tulis12</groupId>
    <artifactId>TuliAutoInitializer</artifactId>
    <version>latest-version</version> // If you don't know it, check the Jitpack badge!
</dependency>
<dependency> // Gitea
	<groupId>dev.tulis</groupId>
	<artifactId>TuliAutoInitializer</artifactId>
	<version>latest-version</version>
</dependency>

All annotation's parameters

initializeWithoutParameters = true

Tries to use the default constructor of class (the one without parameters), defaults to false (using the configuration of the builder).

@Init(initializeWithoutParameters = true)
public class Test {
    public Test() {
        System.out.println("Will use this instead!");
    }

    public Test(Integer i1, Integer i2) {
        System.out.println("Won't use this!");
    }
}

The Initializer won't force use of default constructor (for example, if its private), will instead throw NoSuchMethodException, IllegalAccessException respectively.

initializeOnlyWith = { Integer.class, MyMainClass.class }

Will use only those two, even if there are more. In case of multiple values with the same class, will take the one first added in the builder, defaults to an empty array (will use the configuration of builder).

@Init(initializeOnlyWith = { Integer.class, MyMainClass.class })
public class Test {
    public Test(Integer integer, MyMainClass myMainClass) {
        System.out.println("Will use this, even if in builder there is more.");
    }

    public Test(Integer integer, String string, MyMainClass myMainClass) {
        System.out.println("Won't use this!");
    }
}

Remember, if there is no class (with a value) provided in the builder, but you will try to use it here, the builder will throw MissingParameterException.

License

Released under the MIT license.

Description
Initializing Java classes automatically, for example for registering commands or events, just by adding an annotation, especially useful when writing a Spigot or Paper plugin!
Readme 81 KiB
Languages
Java 100%