-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
406 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../src/main/java/com/navercorp/fixturemonkey/api/introspector/InvocationHandlerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.introspector; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.util.Map; | ||
|
||
import org.apiguardian.api.API; | ||
import org.apiguardian.api.API.Status; | ||
|
||
/** | ||
* A builder generates {@link InvocationHandler} for generating an anonymous instance of interface dynamically. | ||
*/ | ||
@API(since = "0.6.10", status = Status.EXPERIMENTAL) | ||
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(); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/type/AnnotatedTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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; | ||
|
||
import org.apiguardian.api.API; | ||
import org.apiguardian.api.API.Status; | ||
|
||
@API(since = "0.6.10", status = Status.EXPERIMENTAL) | ||
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; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...rc/main/java17/com/navercorp/fixturemonkey/api/introspector/InvocationHandlerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.introspector; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.util.Map; | ||
|
||
import org.apiguardian.api.API; | ||
import org.apiguardian.api.API.Status; | ||
|
||
/** | ||
* A builder generates {@link InvocationHandler} for generating an anonymous instance of interface dynamically. | ||
*/ | ||
@SuppressWarnings("unused") | ||
@API(since = "0.6.10", status = Status.EXPERIMENTAL) | ||
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(); | ||
} | ||
} |
Oops, something went wrong.