Skip to content

Commit

Permalink
Changed: Recoded base structure of YamlStorage
Browse files Browse the repository at this point in the history
Signed-off-by: DevDrizzy <[email protected]>
  • Loading branch information
DevDrizzy committed Dec 8, 2024
1 parent 28cbaa4 commit 4b561d8
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 373 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ buildNumber.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.mvn/
17 changes: 0 additions & 17 deletions StorageAPI.iml

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/annotations/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xyz.refinedev.api.storage.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface Comment {

boolean lineBreak() default true;

String[] value();
}
12 changes: 12 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/annotations/Header.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xyz.refinedev.api.storage.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface Header {
String[] value();
}
92 changes: 0 additions & 92 deletions src/main/java/xyz/refinedev/api/storage/data/PluginData.java

This file was deleted.

76 changes: 0 additions & 76 deletions src/main/java/xyz/refinedev/api/storage/impl/BasicYamlStorage.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.refinedev.api.storage;
package xyz.refinedev.api.storage.json;

import com.google.gson.Gson;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.refinedev.api.storage;
package xyz.refinedev.api.storage.mongo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/utils/ReflectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package xyz.refinedev.api.storage.utils;

import lombok.experimental.UtilityClass;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
* <p>
* This class is the property of Refine Development.<br>
* Copyright © 2024, All Rights Reserved.<br>
* Redistribution of this class without permission is not allowed.<br>
* </p>
*
* @author Drizzy
* @version StorageAPI
* @since 12/8/2024
*/

@UtilityClass
public class ReflectionUtils {

private static final Logger LOGGER = LogManager.getLogger(ReflectionUtils.class);

public Field getField(String[] split, Object instance) {
try {
Field field = instance.getClass().getField(toFieldName(split[split.length - 1]));
setAccessible(field);
return field;
} catch (Exception e) {
LOGGER.error("Error (Invalid Field): {}", e.getMessage(), e);
return null;
}
}

public String toFieldName(String node) {
return node.toUpperCase().replaceAll("-", "_");
}

public String toNodeName(String field) {
return field.toUpperCase().replace("_", "-");
}

public void setAccessible(Field field) {
setAccessibleNonFinal(field);
}

/**
* Make the given field accessible and remove the final modifier from it.
*
* @param field {@link Field field}
*/
public void setAccessibleNonFinal(Field field) {
// let's make the field accessible
field.setAccessible(true);

// next we change the modifier in the Field instance to
// not be final anymore, thus tricking reflection into
// letting us modify the static final field
if (Modifier.isFinal(field.getModifiers())) {
try {
if (getVersion() > 11) {
// Requires ImagineBreaker to do this shit
// blank out the final bit in the modifiers int
((MethodHandles.Lookup) LOOKUP_FIELD.get(null))
.findSetter(Field.class, "modifiers", int.class)
.invokeExact(field, field.getModifiers() & ~Modifier.FINAL);
} else {
// Normal Java 8 reflection breaker
MODIFIERS_FIELD.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
} catch (Throwable e) {
LOGGER.error(e.getMessage(), e);
}
}
}
public static int getVersion() {
String version = System.getProperty("java.version");
if(version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if(dot != -1) { version = version.substring(0, dot); }
} return Integer.parseInt(version);
}

private static Field LOOKUP_FIELD;
private static Field MODIFIERS_FIELD;

static {
if (getVersion() > 11) {
try {
LOOKUP_FIELD = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); // This one has trusted access
LOOKUP_FIELD.setAccessible(true);
} catch (Throwable e) {
System.out.println("[Carbon] Failed to find trusted lookup field.");
e.printStackTrace();
}
} else {
try {
MODIFIERS_FIELD = Field.class.getDeclaredField("modifiers");
MODIFIERS_FIELD.setAccessible(true);
} catch (Throwable e) {
System.out.println("[Carbon] Failed to find modifiers field.");
e.printStackTrace();
}
}

}
}
Loading

0 comments on commit 4b561d8

Please sign in to comment.