Skip to content

Commit

Permalink
Add multi-release jar
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo committed Oct 11, 2023
1 parent 9278493 commit bc4661b
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 115 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ subprojects {
java {
withJavadocJar()
withSourcesJar()
toolchain {languageVersion = JavaLanguageVersion.of(8)}
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

package com.navercorp.fixturemonkey.api.introspector;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;
Expand Down Expand Up @@ -76,7 +74,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
invocationHandlerBuilder.put(methodProperty.getMethodName(), combined);
}

if (invocationHandlerBuilder.generatedValuesByMethodName.isEmpty()) {
if (invocationHandlerBuilder.isEmpty()) {
return null;
}

Expand All @@ -92,19 +90,4 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
);
}

private static final class InvocationHandlerBuilder {
private final Map<String, Object> generatedValuesByMethodName;

private InvocationHandlerBuilder(Map<String, Object> generatedValuesByMethodName) {
this.generatedValuesByMethodName = generatedValuesByMethodName;
}

private void put(String methodName, Object value) {
generatedValuesByMethodName.put(methodName, value);
}

private InvocationHandler build() {
return (proxy, method, args) -> generatedValuesByMethodName.get(method.getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.navercorp.fixturemonkey.api.introspector;

import java.lang.reflect.InvocationHandler;
import java.util.Map;

final class InvocationHandlerBuilder {
private final Map<String, Object> generatedValuesByMethodName;

InvocationHandlerBuilder(Map<String, Object> generatedValuesByMethodName) {
this.generatedValuesByMethodName = generatedValuesByMethodName;
}

void put(String methodName, Object value) {
generatedValuesByMethodName.put(methodName, value);
}

InvocationHandler build() {
return (proxy, method, args) -> generatedValuesByMethodName.get(method.getName());
}

boolean isEmpty() {
return generatedValuesByMethodName.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.navercorp.fixturemonkey.api.type;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.Arrays;

import javax.annotation.Nullable;

public final class AnnotatedTypes {
public static AnnotatedArrayType from(
AnnotatedType annotatedGenericComponentType,
Type type,
Annotation[] annotations,
Annotation[] declaredAnnotations,
AnnotatedType annotatedOwnerType
) {
return new AnnotatedArrayType() {
@Override
public AnnotatedType getAnnotatedGenericComponentType() {
return annotatedGenericComponentType;
}

@Override
public Type getType() {
return type;
}

@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return Arrays.stream(annotations)
.filter(it -> it.getClass() == annotationClass)
.findAny()
.map(annotationClass::cast)
.orElse(null);
}

@Override
public Annotation[] getAnnotations() {
return annotations;
}

@Override
public Annotation[] getDeclaredAnnotations() {
return declaredAnnotations;
}
};
}

public static AnnotatedParameterizedType from(
AnnotatedType[] annotatedActualTypeArguments,
Type type,
Annotation[] annotations,
Annotation[] declaredAnnotations,
AnnotatedType annotatedOwnerType
) {
return new AnnotatedParameterizedType() {
@Override
public AnnotatedType[] getAnnotatedActualTypeArguments() {
return annotatedActualTypeArguments;
}

@Override
public Type getType() {
return type;
}

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return (T)Arrays.stream(annotations)
.filter(it -> it.getClass() == annotationClass)
.findAny()
.orElse(null);
}

@Override
public Annotation[] getAnnotations() {
return annotations;
}

@Override
public Annotation[] getDeclaredAnnotations() {
return declaredAnnotations;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private static AnnotatedType resolvesParentTypeGenerics(
}

if (genericType instanceof GenericArrayType) {
return resolveGenericsArrayType(currentAnnotatedType, parentGenericsTypes);
return resolveGenericsArrayType(parentAnnotatedType, currentAnnotatedType, parentGenericsTypes);
}

AnnotatedParameterizedType fieldParameterizedType = (AnnotatedParameterizedType)currentAnnotatedType;
Expand Down Expand Up @@ -320,43 +320,17 @@ public Type getOwnerType() {
}
};

return new AnnotatedParameterizedType() {
@Override
public AnnotatedType[] getAnnotatedActualTypeArguments() {
return resolvedGenericsTypes;
}

// For compatibility with JDK >= 9. A breaking change in the JDK :-(
// @Override
@SuppressWarnings("Since15")
public AnnotatedType getAnnotatedOwnerType() {
// TODO: Return annotatedType.getAnnotatedOwnerType() as soon as Java >= 9 is being used
return null;
}

@Override
public Type getType() {
return resolveType;
}

@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return fieldParameterizedType.getAnnotation(annotationClass);
}

@Override
public Annotation[] getAnnotations() {
return fieldParameterizedType.getAnnotations();
}

@Override
public Annotation[] getDeclaredAnnotations() {
return fieldParameterizedType.getDeclaredAnnotations();
}
};
return AnnotatedTypes.from(
resolvedGenericsTypes,
resolveType,
fieldParameterizedType.getAnnotations(),
fieldParameterizedType.getDeclaredAnnotations(),
parentAnnotatedType
);
}

private static AnnotatedArrayType resolveGenericsArrayType(
AnnotatedType parentAnnotatedType,
AnnotatedType currentAnnotatedType,
AnnotatedType[] ownerGenericsTypes
) {
Expand Down Expand Up @@ -387,38 +361,13 @@ public Type getOwnerType() {

Type resolveType = (GenericArrayType)() -> genericComponentTypeWithGeneric;

return new AnnotatedArrayType() {
@Override
public AnnotatedType getAnnotatedGenericComponentType() {
return Types.generateAnnotatedTypeWithoutAnnotation(genericComponentTypeWithGeneric);
}

@SuppressWarnings("Since15")
public AnnotatedType getAnnotatedOwnerType() {
// TODO: Return annotatedType.getAnnotatedOwnerType() as soon as Java >= 9 is being used
return null;
}

@Override
public Type getType() {
return resolveType;
}

@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return currentAnnotatedType.getAnnotation(annotationClass);
}

@Override
public Annotation[] getAnnotations() {
return currentAnnotatedType.getAnnotations();
}

@Override
public Annotation[] getDeclaredAnnotations() {
return currentAnnotatedType.getDeclaredAnnotations();
}
};
return AnnotatedTypes.from(
Types.generateAnnotatedTypeWithoutAnnotation(genericComponentTypeWithGeneric),
resolveType,
currentAnnotatedType.getAnnotations(),
currentAnnotatedType.getDeclaredAnnotations(),
parentAnnotatedType
);
}

public static AnnotatedType resolveWithTypeReferenceGenerics(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.navercorp.fixturemonkey.api.introspector;

import java.lang.reflect.InvocationHandler;
import java.util.Map;

@SuppressWarnings("unused")
final class InvocationHandlerBuilder {
private final Map<String, Object> generatedValuesByMethodName;

InvocationHandlerBuilder(Map<String, Object> generatedValuesByMethodName) {
this.generatedValuesByMethodName = generatedValuesByMethodName;
}

void put(String methodName, Object value) {
generatedValuesByMethodName.put(methodName, value);
}

InvocationHandler build() {
return (proxy, method, args) -> {
if (method.isDefault()) {
return InvocationHandler.invokeDefault(proxy, method, args);
}
return generatedValuesByMethodName.get(method.getName());
};
}

boolean isEmpty() {
return generatedValuesByMethodName.isEmpty();
}
}
Loading

0 comments on commit bc4661b

Please sign in to comment.