Skip to content
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

Issues/951/upgrades breaking #962

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver;
import com.github.jknack.handlebars.context.MethodValueResolver;
import com.github.jknack.handlebars.context.RecordValueResolver;

/**
* A hook interface for resolving values from the {@link Context context stack}.
Expand Down Expand Up @@ -79,15 +79,15 @@ public interface ValueResolver {
*
* - {@link MapValueResolver}
* - {@link JavaBeanValueResolver}
* - {@link MethodValueResolver}. On Java 14 or higher.
* - {@link RecordValueResolver}.
*
* @return Immutable list of value resolvers.
*/
static List<ValueResolver> defaultValueResolvers() {
if (Handlebars.Utils.javaVersion14) {
return unmodifiableList(asList(MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE));
}
return unmodifiableList(asList(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE));
return unmodifiableList(
asList(
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
RecordValueResolver.INSTANCE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public abstract class MemberValueResolver<M extends Member> implements ValueReso

@Override
public final Object resolve(final Object context, final String name) {
if (!matches(context)) {
return UNRESOLVED;
}
Class<?> key = context.getClass();
Map<String, M> mcache = cache(key);
M member = mcache.get(name);
Expand Down Expand Up @@ -122,6 +125,16 @@ protected boolean isUseSetAccessible(final M member) {
*/
protected abstract Object invokeMember(M member, Object context);

/**
* True, if the context is worth examing by this resolver.
*
* @param context The context object.
* @return True, if the context is suitable.
*/
protected boolean matches(final Object context) {
return true;
}

/**
* True, if the member matches the one we look for.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.jknack.handlebars.context;

/**
* A resolver for Record types.
*
* This resolver is safe to use in JDKs that do not support records yet.
*
* @author agentgt
*
*/
public class RecordValueResolver extends MethodValueResolver {

/**
* The default instance.
*/
public static final RecordValueResolver INSTANCE = new RecordValueResolver();

@Override
protected boolean matches(final Object context) {
Class<?> superClass = context.getClass().getSuperclass();
return superClass != null && superClass.getName().equals("java.lang.Record");
}

}