Skip to content

Commit

Permalink
Functioning parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jul 6, 2022
1 parent 054892c commit a250d32
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 120 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@
<changelog.version>1.60</changelog.version>
<commons-io.version>2.11.0</commons-io.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-text.version>1.9</commons-text.version>
<guava.version>31.1-jre</guava.version>
<guice.version>5.1.0</guice.version>
<httpcore.version>4.4.15</httpcore.version>
<jackson.version>2.13.3</jackson.version>
<jakarta-ws-rs.version>2.1.6</jakarta-ws-rs.version>
<jansi.version>1.18</jansi.version>
<java.version>11</java.version>
<java.version>12</java.version>
<javapoet.version>1.13.0</javapoet.version>
<logback-classic.version>1.2.11</logback-classic.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
Expand Down
9 changes: 9 additions & 0 deletions proteus-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>proteus-core</artifactId>
<name>Proteus Core</name>
<properties>
</properties>
<packaging>jar</packaging>

<build>
Expand Down Expand Up @@ -153,6 +155,13 @@ Proteus Changelog.
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.reflect.Invokable;
import com.google.common.reflect.MutableTypeToInstanceMap;
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import com.google.inject.name.Named;
Expand All @@ -25,6 +24,7 @@
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.server.endpoints.EndpointInfo;
import io.sinistral.proteus.utilities.ClassUtilities;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -289,7 +289,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
TypeHandler handler = TypeHandler.forType(t);
return (handler.equals(TypeHandler.ModelType) || handler.equals(TypeHandler.OptionalModelType) || handler.equals(TypeHandler.NamedModelType) || handler.equals(TypeHandler.OptionalNamedModelType));

}).collect(Collectors.toMap(java.util.function.Function.identity(), HandlerGenerator::typeReferenceNameForParameterizedType));
}).collect(Collectors.toMap(java.util.function.Function.identity(), ClassUtilities::typeReferenceNameForParameterizedType));


java.util.regex.Pattern internalTypesPattern = java.util.regex.Pattern.compile("concurrent|<");
Expand All @@ -299,7 +299,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
.flatMap(
m -> Invokable.from(m).getParameters().stream())
.filter(p -> internalTypesPattern.matcher(p.getType().getType().getTypeName()).find() )
.distinct().collect(Collectors.toMap(p -> p.getType().toString(), com.google.common.reflect.Parameter::getType));
.distinct().collect(Collectors.toMap(p -> p.getType().toString(), com.google.common.reflect.Parameter::getType, (p1, p2) -> p1 ));



Expand Down Expand Up @@ -346,7 +346,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
|| handler.equals(TypeHandler.OptionalBeanListValueOfType)
|| handler.equals(TypeHandler.OptionalBeanListFromStringType))
{
parameterizedLiteralsNameMap.put(p.getParameterizedType(), HandlerGenerator.typeReferenceNameForParameterizedType(p.getParameterizedType()));
parameterizedLiteralsNameMap.put(p.getParameterizedType(), ClassUtilities.typeReferenceNameForParameterizedType(p.getParameterizedType()));
}
}

Expand Down Expand Up @@ -1334,120 +1334,6 @@ else if (matches > 2)
return null;
}

public static String typeReferenceNameForParameterizedType(Type type)
{

log.info("creating name for reference: {}", type);
String typeName = type.getTypeName();

if (typeName.contains("Optional"))
{
log.warn("Type is for an optional named {}", typeName);
}

Matcher matcher = TYPE_NAME_PATTERN.matcher(typeName);

if (matcher.find())
{

int matches = matcher.groupCount();

if (matches == 2)
{
String genericInterface = matcher.group(1);
String erasedType = matcher.group(2).replaceAll("\\$", ".");

String[] genericParts = genericInterface.split("\\.");
String[] erasedParts = erasedType.split("\\.");

String genericTypeName = genericParts[genericParts.length - 1];
String erasedTypeName;

if (erasedParts.length > 1)
{
erasedTypeName = erasedParts[erasedParts.length - 2] + erasedParts[erasedParts.length - 1];
}
else
{
erasedTypeName = erasedParts[0];
}

typeName = String.format("%s%s%s", Character.toLowerCase(erasedTypeName.charAt(0)), erasedTypeName.substring(1), genericTypeName);

return typeName;
}

}

matcher = CONCURRENT_TYPE_NAME_PATTERN.matcher(typeName);

if (matcher.find())
{

int matches = matcher.groupCount();

if (matches == 2)
{
String genericInterface = matcher.group(1);
String erasedType = matcher.group(2).replaceAll("\\$", ".");

String[] genericParts = genericInterface.split("\\.");
String[] erasedParts = erasedType.split("\\.");

String genericTypeName = genericParts[genericParts.length - 1];
String erasedTypeName;

if (erasedParts.length > 1)
{
erasedTypeName = erasedParts[erasedParts.length - 2] + erasedParts[erasedParts.length - 1];
}
else
{
erasedTypeName = erasedParts[0];
}

typeName = String.format("%s%s%s", Character.toLowerCase(erasedTypeName.charAt(0)), erasedTypeName.substring(1), genericTypeName);
return typeName;
}

}

if (type.getTypeName().startsWith("sun"))
{
return typeName;
}

if (type instanceof ParameterizedType)
{
ParameterizedType pType = (ParameterizedType) type;
log.debug("pType: {}", pType);

Type actualTypeArgument0 = pType.getActualTypeArguments()[0];

if (actualTypeArgument0 instanceof Class)
{
Class<?> genericType = (Class<?>) pType.getActualTypeArguments()[0];
Class<?> rawType = (Class<?>) pType.getRawType();
Class<?> erasedType = (Class<?>) HandlerGenerator.extractErasedType(genericType);

if (!(pType.getRawType() instanceof ParameterizedType))
{
log.info("not a raw type that is parameterized {} {}", rawType, genericType);
return Character.toLowerCase(rawType.getSimpleName().charAt(0)) + rawType.getSimpleName().substring(1) + genericType.getSimpleName();
}
}
else
{
log.error(
"failed to process {} ptype: {}", type, pType
);
}

}

return typeName;
}

protected static String typeReferenceNameForType(Type type)
{

Expand Down
Loading

0 comments on commit a250d32

Please sign in to comment.