Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b264841
Merge pull request #1 from Der-Zauberer/parser
Der-Zauberer Oct 28, 2022
2a5dd9f
Improved FileUtil
Der-Zauberer Oct 31, 2022
d51930f
Improved parser IO
Der-Zauberer Oct 31, 2022
71ca1a8
Made parameters final
Der-Zauberer Oct 31, 2022
03943aa
Added documentation for the AccessorException
Der-Zauberer Oct 31, 2022
c36ce24
Updated to Java11
Der-Zauberer Nov 1, 2022
0873815
Added optionals
Der-Zauberer Nov 1, 2022
bb9060a
Improved events and removed EventHandler
Der-Zauberer Nov 1, 2022
3ec28e8
Replaced LoopHandler with LoopController
Der-Zauberer Nov 1, 2022
6125f1c
Replaced TickHandler with TickController
Der-Zauberer Nov 1, 2022
c664256
Improved documentations
Der-Zauberer Nov 1, 2022
af4b479
Improved documentation
Der-Zauberer Nov 2, 2022
558750b
Replaced Client and Server with ClientController and ServerController
Der-Zauberer Nov 2, 2022
d71bbcd
Improved documentation
Der-Zauberer Nov 2, 2022
1987b07
Replaced Console with ConsoleController and CommandHandler with
Der-Zauberer Nov 2, 2022
c89f5cd
Improved documentation
Der-Zauberer Nov 2, 2022
8ae987c
Removed input method from sender
Der-Zauberer Nov 3, 2022
66719dc
Improved documentation
Der-Zauberer Nov 3, 2022
b1a51ee
Added EventController
Der-Zauberer Nov 3, 2022
ff3fd65
Removed final keyword from parameters
Der-Zauberer Nov 3, 2022
9b4701b
Made that events call the global EventController
Der-Zauberer Nov 3, 2022
65a2ccb
Improved LoopTickController
Der-Zauberer Nov 3, 2022
e488f8c
Added object instantiation and improved types
Der-Zauberer Nov 4, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
Expand Down
131 changes: 120 additions & 11 deletions src/eu/derzauberer/javautils/accessible/Accessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.derzauberer.javautils.accessible;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -9,30 +11,79 @@
import java.util.function.Predicate;

/**
* The class takes an object and loads it's content with reflections to make
The class takes an object and loads it's content with reflections to make
* fields, methods and their annotations accessible. The object class can use
* the following annotations {@link AccessibleVisibility},
* {@link AccessibleWhitelist} and {@link AccessibleBlacklist} to define which
* fields and methods should be accessible.
*
* @param <T> the type of the object inside the accessor
*/
public class Accessor {
public class Accessor<T> {

private final Object object;
private final T object;
private final List<Class<?>> classes;
private final List<FieldAccessor> fields;
private final List<MethodAccessor> methods;
private final List<FieldAccessor<?, ?>> fields;
private final List<MethodAccessor<?>> methods;
private final List<String> whitelist;
private final List<String> blacklist;
private Visibility fieldVisibility;
private Visibility methodVisibility;

/**
* Tries to create an instance of the type. This instantiation does only work if
* the class has a standard constructor.
*
* @param type the type of the class to in instantiate
* @return the instantiated object
* @throws NoSuchMethodException if there is no standard constructor in the
* class
* @throws InstantiationException if the class is an interface or abstract
* class
* @throws IllegalAccessException if this C constructor object is enforcing
* Java language access control and the
* underlying constructor is inaccessible
* @throws InvocationTargetException if the constructor throws an exception
*/
public Accessor(Class<T> type)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
this(instantiate(type));
}

/**
* Tries to create an instance of the type. This instantiation does only work if
* the class constructor with the exact same parameters.
*
* @param type type the type of the class to in instantiate
* @param constructurTypes the types of the constructor arguments to identify
* the constructor
* @param constructerArgs the constructor arguments
* @return the instantiated object
* @throws NoSuchMethodException if there is no constructor with the given
* argument types in the class
* @throws InstantiationException if the class is an interface or abstract
* class
* @throws IllegalAccessException if this C constructor object is enforcing
* Java language access control and the
* underlying constructor is inaccessible
* @throws IllegalArgumentException if the wrong arguments where given to the
* constructor
* @throws InvocationTargetException if the constructor throws an exception
*/
public Accessor(Class<T> type, Class<?> constructurTypes, Object... constructerArgs)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this(instantiate(type, constructurTypes, constructerArgs));
}

/**
* Wraps the object in the {@link Accessor} to gain access to fields and
* methods.
*
* @param object the object to wrap
*/
public Accessor(Object object) {
public Accessor(T object) {
this.object = object;
fields = new ArrayList<>();
methods = new ArrayList<>();
Expand All @@ -58,6 +109,9 @@ public Accessor(Object object) {
loadContents();
}

/**
* Loads all fields and methods, which are accessible in this class.
*/
private void loadContents() {
final Predicate<Field> fieldPredicate = field ->
(fieldVisibility == Visibility.ANY ||
Expand Down Expand Up @@ -87,14 +141,14 @@ private void loadContents() {
.filter(fieldPredicate)
.forEach(field -> {
field.setAccessible(true);
fields.add(new FieldAccessor(this, field));
fields.add(new FieldAccessor<>(this, field, field.getType()));
});
Arrays.asList(clazz.getDeclaredMethods())
.stream()
.filter(methodPredicate)
.forEach(method -> {
method.setAccessible(true);
methods.add(new MethodAccessor(this, method));
methods.add(new MethodAccessor<>(this, method));
});
}
}
Expand All @@ -113,7 +167,7 @@ public String getName() {
*
* @return the wrapped object of the {@link Accessor}
*/
public Object getObject() {
public T getObject() {
return object;
}

Expand Down Expand Up @@ -146,7 +200,7 @@ public List<Class<?>> getClasses() {
* @return a {@link List} of all {@link FieldAccessor}, which are usable for
* this class
*/
public List<FieldAccessor> getFields() {
public List<FieldAccessor<?, ?>> getFields() {
return new ArrayList<>(fields);
}

Expand All @@ -157,8 +211,63 @@ public List<FieldAccessor> getFields() {
* @return a {@link List} of all {@link MethodAccessor}, which are usable for
* this class
*/
public List<MethodAccessor> getMethods() {
public List<MethodAccessor<?>> getMethods() {
return new ArrayList<>(methods);
}

/**
* Tries to create an instance of the type. This instantiation does only work if
* the class has a standard constructor.
*
* @param <T> the type of the object inside the accessor
* @param type the type of the class to in instantiate
* @return the instantiated object
* @throws NoSuchMethodException if there is no standard constructor in the
* class
* @throws SecurityException if the accessor has no permission to access
* the standard constructor
* @throws InstantiationException if the class is an interface or abstract
* class
* @throws IllegalAccessException if this C constructor object is enforcing
* Java language access control and the
* underlying constructor is inaccessible
* @throws InvocationTargetException if the constructor throws an exception
*/
@SuppressWarnings("unchecked")
private static <T> T instantiate(Class<T> type)
throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
final Constructor<?> constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
return (T) constructor.newInstance();
}

/**
* Tries to create an instance of the type. This instantiation does only work if
* the class constructor with the exact same parameters.
*
* @param <T> the type of the object inside the accessor
* @param type type the type of the class to in instantiate
* @param constructurTypes the types of the constructor arguments to identify
* the constructor
* @param constructerArgs the constructor arguments
* @return the instantiated object
* @throws NoSuchMethodException if there is no constructor with the given
* argument types in the class
* @throws InstantiationException if the class is an interface or abstract
* class
* @throws IllegalAccessException if this C constructor object is enforcing
* Java language access control and the
* underlying constructor is inaccessible
* @throws IllegalArgumentException if the wrong arguments where given to the
* constructor
* @throws InvocationTargetException if the constructor throws an exception
*/
@SuppressWarnings("unchecked")
private static <T> T instantiate(Class<T> type, Class<?> constructurTypes, Object... constructerArgs)
throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
final Constructor<?> constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
return (T) constructor.newInstance(constructerArgs);
}

}
12 changes: 12 additions & 0 deletions src/eu/derzauberer/javautils/accessible/AccessorException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package eu.derzauberer.javautils.accessible;

/**
* A exception, which is thrown, when another exception was thrown inside the
* functions of the {@link Accessor}, {@link FieldAccessor} or
* {@link MethodAccessor}.
*/
public class AccessorException extends RuntimeException {

private static final long serialVersionUID = 1L;

/**
* Creates a new exception, which is typically thrown, when another exception
* was thrown inside the functions of the {@link Accessor},
* {@link FieldAccessor} or {@link MethodAccessor}.
*
* @param string the exception message
*/
public AccessorException(String string) {
super(string);
}
Expand Down
34 changes: 26 additions & 8 deletions src/eu/derzauberer/javautils/accessible/FieldAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

/**
* The class wraps the {@link Field} and make its usage easer to use.
*
* @param <A> the type of the parent accessor
* @param <F> the type of the fields value
*/
public class FieldAccessor {
public class FieldAccessor<A extends Accessor<?>, F> {

private final Accessor parent;
private final A parent;
private final Field field;

/**
Expand All @@ -18,10 +21,9 @@ public class FieldAccessor {
* @param parent the {@link Accessor} of the object, which the field is part of
* @param field the corresponding {@link Field}
*/
public FieldAccessor(final Accessor parent, final Field field) {
public FieldAccessor(A parent, Field field, Class<F> fieldtype) {
this.parent = parent;
this.field = field;

}

/**
Expand All @@ -31,7 +33,22 @@ public FieldAccessor(final Accessor parent, final Field field) {
* @throws IllegalArgumentException if the object is not assignable to the field
* due to a wrong type
*/
public void setValue(Object value) throws IllegalArgumentException {
public void setValue(F value) throws IllegalArgumentException {
try {
field.set(parent.getObject(), value);
} catch (IllegalAccessException exception) {
throw new AccessorException(exception.getMessage());
}
}

/**
* Sets the value of the {@link FieldAccessor}
*
* @param value the new value of the field
* @throws IllegalArgumentException if the object is not assignable to the field
* due to a wrong type
*/
public void setObjectValue(Object value) throws IllegalArgumentException {
try {
field.set(parent.getObject(), value);
} catch (IllegalAccessException exception) {
Expand All @@ -44,9 +61,10 @@ public void setValue(Object value) throws IllegalArgumentException {
*
* @return the value of the {@link FieldAccessor}
*/
public Object getValue() {
@SuppressWarnings("unchecked")
public F getValue() {
try {
return field.get(parent.getObject());
return (F) field.get(parent.getObject());
} catch (IllegalAccessException | IllegalArgumentException exception) {
throw new AccessorException(exception.getMessage());
}
Expand Down Expand Up @@ -93,7 +111,7 @@ public boolean isFinal() {
*
* @return the {@link Accessor} parent of this {@link FieldAccessor}
*/
public Accessor getParent() {
public Accessor<?> getParent() {
return parent;
}

Expand Down
8 changes: 5 additions & 3 deletions src/eu/derzauberer/javautils/accessible/MethodAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

/**
* The class wraps the {@link Method} and make its usage easer to use.
*
* @param <A> the type of the parent accessor
*/
public class MethodAccessor {
public class MethodAccessor<A> {

private final Accessor parent;
private final Accessor<A> parent;
private final Method method;

/**
Expand All @@ -20,7 +22,7 @@ public class MethodAccessor {
* @param parent the {@link Accessor} of the object, which the field is part of
* @param method the corresponding {@link Method}
*/
public MethodAccessor(final Accessor parent, final Method method) {
public MethodAccessor(Accessor<A> parent, Method method) {
this.parent = parent;
this.method = method;
}
Expand Down
Loading