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

Teeny json suport #3

Merged
merged 18 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<version>4.5.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module net.jonathangiles.tools.teenyhttpd {
exports net.jonathangiles.tools.teenyhttpd;
exports net.jonathangiles.tools.teenyhttpd.model;

requires java.logging;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.jonathangiles.tools.teenyhttpd.implementation;

public final class ParameterType {

private final Class<?> type;
private final Class<?> parentType;//nullable

ParameterType(Class<?> type, Class<?> parentType) {
this.type = type;
this.parentType = parentType;
}

public Class<?> getType() {
return type;
}

public Class<?> getParentType() {
return parentType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.jonathangiles.tools.teenyhttpd.implementation;

import java.lang.reflect.*;
import java.util.Arrays;

public final class ReflectionUtils {

@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> clazz) {

Constructor<?> constructor = Arrays.stream(clazz.getDeclaredConstructors())
.filter(cons -> cons.getParameterCount() == 0)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No default constructor found for " + clazz.getName()));

try {

constructor.setAccessible(true);

alex-cova marked this conversation as resolved.
Show resolved Hide resolved
return (T) constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Failed to create new instance of " + clazz.getName(), e);
}
}

public static ParameterType getParameterType(Parameter parameter) {
return getParameterType(parameter.getParameterizedType());
}

public static ParameterType getParameterType(Field field) {
return getParameterType(field.getGenericType());
}

public static ParameterType getParameterType(Type type) {
if (type instanceof Class<?>) {
return null;
}

Type actualTypeArgument = ((ParameterizedType) type)
.getActualTypeArguments()[0];

if (actualTypeArgument instanceof Class<?>) {

Class<?> c = (Class<?>) actualTypeArgument;

alex-cova marked this conversation as resolved.
Show resolved Hide resolved
return new ParameterType(c, null);
}

if (actualTypeArgument instanceof ParameterizedType) {

ParameterizedType parameterizedType = (ParameterizedType) actualTypeArgument;

alex-cova marked this conversation as resolved.
Show resolved Hide resolved
return new ParameterType((Class<?>) parameterizedType.getActualTypeArguments()[0],
(Class<?>) parameterizedType.getRawType());
}

throw new IllegalStateException("Unknown type: " + actualTypeArgument);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.jonathangiles.tools.teenyhttpd.json;

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.METHOD)
public @interface JsonAlias {
JonathanGiles marked this conversation as resolved.
Show resolved Hide resolved
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.jonathangiles.tools.teenyhttpd.json;

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.METHOD)
public @interface JsonIgnore {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.jonathangiles.tools.teenyhttpd.json;

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.TYPE)
public @interface JsonIncludeNonNull {

}
Loading