-
Notifications
You must be signed in to change notification settings - Fork 137
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
6 changed files
with
128 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package j2html.attributes; | ||
|
||
// Written by Benjamin Weber (http://benjiweber.co.uk/blog/author/benji/) | ||
|
||
import j2html.reflection.MethodFinder; | ||
import java.util.function.Function; | ||
|
||
public interface LambdaAttribute extends MethodFinder, Function<String, Object> { | ||
|
||
default String name() { | ||
checkParametersEnabled(); | ||
return parameter(0).getName(); | ||
} | ||
|
||
default String value() { | ||
checkParametersEnabled(); | ||
return String.valueOf(this.apply(name())); | ||
} | ||
|
||
default void checkParametersEnabled() { | ||
if ("arg0".equals(parameter(0).getName())) { | ||
throw new IllegalStateException("You also need java 8u60 or newer for parameter reflection to work"); | ||
} | ||
} | ||
|
||
} |
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,48 @@ | ||
package j2html.reflection; | ||
|
||
// Written by Benjamin Weber (http://benjiweber.co.uk/blog/author/benji/) | ||
|
||
import java.io.Serializable; | ||
import java.lang.invoke.SerializedLambda; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public interface MethodFinder extends Serializable { | ||
|
||
default SerializedLambda serialized() { | ||
try { | ||
Method replaceMethod = getClass().getDeclaredMethod("writeReplace"); | ||
replaceMethod.setAccessible(true); | ||
return (SerializedLambda) replaceMethod.invoke(this); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
default Class<?> getContainingClass() { | ||
try { | ||
String className = serialized().getImplClass().replaceAll("/", "."); | ||
return Class.forName(className); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
default Method method() { | ||
SerializedLambda lambda = serialized(); | ||
Class<?> containingClass = getContainingClass(); | ||
return Arrays.stream(containingClass.getDeclaredMethods()) | ||
.filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName())) | ||
.findFirst() | ||
.orElseThrow(UnableToGuessMethodException::new); | ||
} | ||
|
||
default Parameter parameter(int n) { | ||
return method().getParameters()[n]; | ||
} | ||
|
||
class UnableToGuessMethodException extends RuntimeException { | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package j2html.reflection; | ||
|
||
import j2html.attributes.LambdaAttribute; | ||
import org.junit.Test; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class NamedValueTest { | ||
|
||
@Test | ||
public void testNamedValueWorks() { | ||
LambdaAttribute pair = five -> 5; | ||
assertThat("five", is(pair.name())); | ||
assertThat("5", is(pair.value())); | ||
} | ||
|
||
} |
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