-
-
Notifications
You must be signed in to change notification settings - Fork 2
Reflection-based examinable property source #2
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
Draft
kashike
wants to merge
1
commit into
main
Choose a base branch
from
feature/reflection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
/api/out/ | ||
/api/run/ | ||
/run/ | ||
/reflection/build/ | ||
/string/build/ | ||
/string/out/ | ||
/string/run/ | ||
|
This file contains hidden or 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
42 changes: 42 additions & 0 deletions
42
api/src/main/java/net/kyori/examination/ExaminablePropertySource.java
This file contains hidden or 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,42 @@ | ||
/* | ||
* This file is part of examination, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2018-2020 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.examination; | ||
|
||
import java.util.stream.Stream; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
/** | ||
* A source of {@link ExaminableProperty properties}. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
public interface ExaminablePropertySource { | ||
/** | ||
* Gets a stream of examinable properties. | ||
* | ||
* @return a stream of examinable properties | ||
* @since 1.1.0 | ||
*/ | ||
@NonNull Stream<? extends ExaminableProperty> examinableProperties(); | ||
} |
This file contains hidden or 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,10 @@ | ||
dependencies { | ||
api project(':examination-api') | ||
testImplementation(project(':examination-string')) | ||
} | ||
|
||
jar { | ||
manifest.attributes( | ||
'Automatic-Module-Name': 'net.kyori.examination.reflection' | ||
) | ||
} |
48 changes: 48 additions & 0 deletions
48
reflection/src/main/java/net/kyori/examination/reflection/Examine.java
This file contains hidden or 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,48 @@ | ||
/* | ||
* This file is part of examination, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2018-2020 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.examination.reflection; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks a field which should be examined. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface Examine { | ||
/** | ||
* Gets the name. | ||
* | ||
* <p>The name of the field will be used if no value is provided.</p> | ||
* | ||
* @return the name | ||
* @since 1.1.0 | ||
*/ | ||
String name() default ""; | ||
} |
46 changes: 46 additions & 0 deletions
46
...ection/src/main/java/net/kyori/examination/reflection/ReflectiveExaminableProperties.java
This file contains hidden or 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,46 @@ | ||
/* | ||
* This file is part of examination, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2018-2020 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.examination.reflection; | ||
|
||
import net.kyori.examination.ExaminableProperty; | ||
import net.kyori.examination.ExaminablePropertySource; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
/** | ||
* An examinable property source which provides {@link ExaminableProperty properties} by reflectively examining an object. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
public interface ReflectiveExaminableProperties extends ExaminablePropertySource { | ||
/** | ||
* Creates an examinable property source from the fields in {@code object} annotated with {@link Examine}. | ||
* | ||
* @param object the object to be examined | ||
* @return an examinable property source | ||
* @since 1.1.0 | ||
*/ | ||
static @NonNull ReflectiveExaminableProperties forFields(final @NonNull Object object) { | ||
return ReflectiveExaminablePropertiesImpl.forFields(object); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...on/src/main/java/net/kyori/examination/reflection/ReflectiveExaminablePropertiesImpl.java
This file contains hidden or 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,90 @@ | ||
/* | ||
* This file is part of examination, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2018-2020 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.examination.reflection; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
import net.kyori.examination.ExaminableProperty; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
final class ReflectiveExaminablePropertiesImpl implements ReflectiveExaminableProperties { | ||
private final List<Supplier<ExaminableProperty>> properties; | ||
|
||
private ReflectiveExaminablePropertiesImpl(final List<Supplier<ExaminableProperty>> properties) { | ||
this.properties = properties; | ||
} | ||
|
||
public static ReflectiveExaminablePropertiesImpl forFields(final Object object) { | ||
final List<Supplier<ExaminableProperty>> properties = new ArrayList<>(); | ||
for(final Field field : object.getClass().getDeclaredFields()) { | ||
final Examine examine = field.getAnnotation(Examine.class); | ||
if(examine != null) { | ||
field.setAccessible(true); // TODO(kashike): does this break on new Java versions? | ||
|
||
final String name = name(field, examine); | ||
final MethodHandle handle; | ||
try { | ||
handle = MethodHandles.lookup().unreflectGetter(field); | ||
} catch(final IllegalAccessException e) { | ||
// TODO(kashike): how to handle errors? | ||
e.printStackTrace(); | ||
continue; | ||
} | ||
properties.add(() -> { | ||
final Object value; | ||
try { | ||
value = handle.invoke(object); | ||
} catch(final Throwable e) { | ||
// TODO(kashike): how to handle errors? | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
return ExaminableProperty.of(name, value); | ||
}); | ||
} | ||
} | ||
return new ReflectiveExaminablePropertiesImpl(properties); | ||
} | ||
|
||
@Override | ||
public @NonNull Stream<? extends ExaminableProperty> examinableProperties() { | ||
return this.properties.stream() | ||
.map(Supplier::get) | ||
.filter(Objects::nonNull); | ||
} | ||
|
||
private static String name(final Field field, final Examine examine) { | ||
String name = examine.name(); | ||
if(name.isEmpty()) { | ||
name = field.getName(); | ||
} | ||
return name; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../test/java/net/kyori/examination/reflection/ReflectiveExaminablePropertiesSourceTest.java
This file contains hidden or 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,57 @@ | ||
/* | ||
* This file is part of examination, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2018-2020 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.examination.reflection; | ||
|
||
import java.util.stream.Stream; | ||
import net.kyori.examination.Examinable; | ||
import net.kyori.examination.ExaminableProperty; | ||
import net.kyori.examination.string.StringExaminer; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ReflectiveExaminablePropertiesSourceTest { | ||
@Test | ||
void test() { | ||
final TestExaminable te = new TestExaminable("kashike", 0); | ||
te.examinableProperties().forEach(ep -> { | ||
System.out.println(ep.name() + ": " + ep.examine(StringExaminer.simpleEscaping())); | ||
}); | ||
} | ||
|
||
static class TestExaminable implements Examinable { | ||
private final ReflectiveExaminableProperties reps = ReflectiveExaminableProperties.forFields(this); | ||
private final @Examine String name; | ||
private final @Examine int age; | ||
|
||
TestExaminable(final String name, final int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public @NonNull Stream<? extends ExaminableProperty> examinableProperties() { | ||
return this.reps.examinableProperties(); | ||
} | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i believe this sorta works -- modular applications that want to use examination would have to open their modules to deep reflection from the reflective properties provider module.