Skip to content

Commit

Permalink
Fix: isGetter was missed
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Mar 16, 2024
1 parent 1e26256 commit 8cc62cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package com.fasterxml.jackson.jr.ob.impl;

import com.fasterxml.jackson.jr.ob.impl.POJODefinition.Prop;
import com.fasterxml.jackson.jr.ob.impl.POJODefinition.PropBuilder;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;

import com.fasterxml.jackson.jr.ob.impl.POJODefinition.Prop;
import com.fasterxml.jackson.jr.ob.impl.POJODefinition.PropBuilder;

import static com.fasterxml.jackson.jr.ob.JSON.Feature.INCLUDE_STATIC_FIELDS;
import static java.lang.Character.isLowerCase;
import static java.lang.Character.isUpperCase;
import static java.lang.Character.toLowerCase;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;

import static com.fasterxml.jackson.jr.ob.JSON.Feature.INCLUDE_STATIC_FIELDS;
import static java.lang.reflect.Modifier.*;

/**
* Helper class that jackson-jr uses by default to introspect POJO properties
Expand Down Expand Up @@ -63,26 +59,23 @@ private static void _populatePropWithGettersAndSetters(Class<?> currType, Map<St
// then get methods from within this class
for (Method m : currType.getDeclaredMethods()) {
final int flags = m.getModifiers();
final Class<?> returnType = m.getReturnType();

// 13-Jun-2015, tatu:
// Skip synthetic, bridge methods altogether, for now
// at least (add more complex handling only if absolutely necessary)
if (isStatic(flags) || m.isSynthetic() || m.isBridge() || !isPublic(flags) || isGroovyMetaClass(m.getReturnType())) {
if (isStatic(flags) || m.isSynthetic() || m.isBridge() || !isPublic(flags) || isGroovyMetaClass(returnType)) {
continue;
}

final Class<?>[] argTypes = m.getParameterTypes();
if (argTypes.length == 0) { // getter?
// getters must be public to be used
if (m.getReturnType() == Void.class) {
continue;
}

generatePropsWithGetter(m, "get", props);
generatePropsWithGetter(m, "is", props);
if (argTypes.length == 0 && returnType != Void.class) { // getter?
generatePropsWithGetter(m, props);
generatePropsWithIsGetter(m, props);
} else if (argTypes.length == 1) { // setter?
// Non-public setters are fine if we can force access, don't yet check
// let's also not bother about return type; setters that return value are fine
generatePropsWithSetter(m, "set", props);
generatePropsWithSetter(m, props);
}
}
}
Expand Down Expand Up @@ -125,17 +118,27 @@ private static boolean isGroovyMetaClass(Class<?> clazz) {
return "groovy.lang.MetaClass".equals(clazz.getName());
}

private static void generatePropsWithGetter(final Method method, final String prefix, final Map<String, PropBuilder> props) {
private static void generatePropsWithGetter(final Method method, final Map<String, PropBuilder> props) {
final String getterPrefix = "get";
final String name = method.getName();
if (name.startsWith(getterPrefix) && name.length() > getterPrefix.length()) {
propFrom(props, _decap(name.substring(getterPrefix.length()))).withGetter(method);
}
}

private static void generatePropsWithIsGetter(final Method method, final Map<String, PropBuilder> props) {
final String isGetterPrefix = "is";
final String name = method.getName();
if (name.startsWith(prefix) && name.length() > prefix.length()) {
propFrom(props, _decap(name.substring(prefix.length()))).withGetter(method);
if (name.startsWith(isGetterPrefix) && name.length() > isGetterPrefix.length()) {
propFrom(props, _decap(name.substring(isGetterPrefix.length()))).withIsGetter(method);
}
}

private static void generatePropsWithSetter(final Method method, final String prefix, final Map<String, PropBuilder> props) {
private static void generatePropsWithSetter(final Method method, final Map<String, PropBuilder> props) {
final String setterPrefix = "set";
final String name = method.getName();
if (name.startsWith(prefix) && name.length() > prefix.length()) {
propFrom(props, _decap(name.substring(prefix.length()))).withSetter(method);
if (name.startsWith(setterPrefix) && name.length() > setterPrefix.length()) {
propFrom(props, _decap(name.substring(setterPrefix.length()))).withSetter(method);
}
}

Expand All @@ -160,7 +163,7 @@ private static POJODefinition introspectDefinition(Class<?> beanType, boolean fo
}
}

Prop[] props = propsByName.values().stream().map(PropBuilder::build).toArray(Prop[]::new);
final Prop[] props = propsByName.values().stream().map(PropBuilder::build).toArray(Prop[]::new);
return new POJODefinition(beanType, props, forSerialization ? null : constructors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IndentationTest extends TestBase
{
public void testSimpleList() throws Exception
{
Map<String,Object> map = new LinkedHashMap<String,Object>();
Map<String,Object> map = new LinkedHashMap<>();
map.put("a", 1);
map.put("b", 2);

Expand Down

0 comments on commit 8cc62cf

Please sign in to comment.