Skip to content

Commit

Permalink
Change method for find Classes with annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Chapchuk committed Feb 15, 2021
1 parent 3665f89 commit 1bf3c17
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 125 deletions.
6 changes: 1 addition & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'java'
id 'idea'
id 'maven-publish'
}
group = 'org.zendal'
version = '0.5.1-beta'
version = '0.5.3-beta'

sourceCompatibility = '11'
targetCompatibility = '11'
Expand Down Expand Up @@ -51,13 +50,10 @@ configurations {
testCompile.extendsFrom compileOnly
}
dependencies {
extraLibs 'org.reflections:reflections:0.9.12'

compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
compileOnly 'org.jetbrains:annotations:16.0.2'
compileOnly 'de.tr7zw:item-nbt-api-plugin:2.7.1'
compileOnly 'org.projectlombok:lombok:1.18.16'
compileOnly 'org.reflections:reflections:0.9.12'


annotationProcessor 'org.projectlombok:lombok:1.18.16'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
8 changes: 2 additions & 6 deletions src/main/java/org/zendal/customitems/CustomItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import org.zendal.customitems.listener.PlayerListener;
import org.zendal.customitems.listener.ResourcePackListener;
import org.zendal.customitems.reflection.ReflectionHelper;
import org.zendal.customitems.reflection.ReflectionHelperImpl;
import org.zendal.customitems.test.TestListener;
import org.zendal.customitems.reflection.GoogleClassPathReflectionHelper;

import java.io.IOException;
import java.util.logging.Logger;
Expand All @@ -27,14 +26,11 @@ public final class CustomItems extends JavaPlugin {

@Override
public void onEnable() {
var reflection = new ReflectionHelperImpl();
var reflection = new GoogleClassPathReflectionHelper();
var storage = new HashMapCustomItemStackStorage();
this.api = this.buildCustomItemsApi(this.getLogger(), reflection, storage);

this.api.getCustomItemStackManager().scanPackagesForCustomItemStack("org.zendal");

this.getServer().getPluginManager().registerEvents(new PlayerListener(this.api.getCustomItemStackManager()), this);
this.getServer().getPluginManager().registerEvents(new TestListener(), this);
this.getServer().getPluginManager().registerEvents(new ResourcePackListener(this, this.api), this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface CustomItemStackManager {
* @param packages array of packages prefix
* @see org.zendal.customitems.item.annotation.CustomItem
*/
void scanPackagesForCustomItemStack(String... packages);
void scanPackagesForCustomItemStack(ClassLoader classLoader, String... packages);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public CustomItemStackManagerImpl(Logger pluginLogger, ReflectionHelper reflecti
}

@Override
public void scanPackagesForCustomItemStack(String... packages) {
public void scanPackagesForCustomItemStack(ClassLoader classLoader, String... packages) {
logger.info("[CustomItemManager] Start scanning packages: " + Arrays.toString(packages));
for (String onePackage : packages) {
var classes = reflectionHelper.getAllClassesWithAnnotation(onePackage, CustomItem.class);
var classes = reflectionHelper.getAllClassesWithAnnotation(classLoader, onePackage, CustomItem.class);

classes.forEach(clazz -> {
var annotation = clazz.getAnnotation(CustomItem.class);
Expand Down Expand Up @@ -90,6 +90,7 @@ public AbstractCustomItemStack build(ItemStack itemStack) {
return tryFindDefaultConstructorCustomItem(clazz, itemStack);
}
});
logger.info("[CustomItemManager] Successful loaded item, with default factory : " + clazz);
}

@Override
Expand All @@ -100,6 +101,7 @@ public void registerCustomItemStack(Class<? extends AbstractCustomItemStack> cla
" Please select defaultFactory = false in CustomItem annotation");
}
this.customItemStackStorage.registerCustomItemStack(annotation.type(), annotation.customModelData(), factory);
logger.info("[CustomItemManager] Successful loaded item, with custom factory: " + clazz);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.zendal.customitems.reflection;

import com.google.common.reflect.ClassPath;
import lombok.SneakyThrows;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GoogleClassPathReflectionHelper implements ReflectionHelper {
@SneakyThrows
@Override
public Collection<Class<?>> getAllClassesWithAnnotation(ClassLoader classLoader, String prefixPackage, Class<? extends Annotation> annotation) {

List<Class<?>> classes = new ArrayList<>();

ClassPath path = ClassPath.from(classLoader);
for (ClassPath.ClassInfo info : path.getTopLevelClassesRecursive(prefixPackage)) {
Class<?> clazz = Class.forName(info.getName(), true, classLoader);
if (clazz.getAnnotation(annotation) != null) {
classes.add(clazz);
}
}

return classes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

public interface ReflectionHelper {

Collection<Class<?>> getAllClassesWithAnnotation(String prefixPackage, Class<? extends Annotation> annotation);
Collection<Class<?>> getAllClassesWithAnnotation(ClassLoader classLoader, String prefixPackage, Class<? extends Annotation> annotation);

}

This file was deleted.

This file was deleted.

29 changes: 0 additions & 29 deletions src/main/java/org/zendal/customitems/test/ServiceItemStack.java

This file was deleted.

52 changes: 0 additions & 52 deletions src/main/java/org/zendal/customitems/test/TestListener.java

This file was deleted.

0 comments on commit 1bf3c17

Please sign in to comment.