Skip to content

Commit

Permalink
#991 can now bind class with private constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Jul 12, 2016
1 parent cb59f69 commit a518ea8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
15 changes: 10 additions & 5 deletions framework/src/play/data/binding/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
Expand Down Expand Up @@ -302,13 +299,21 @@ private static Object internalBindBean(Class<?> clazz, ParamNode paramNode, Bind

private static <T> T createNewInstance(Class<T> clazz) {
try {
return clazz.newInstance();
Constructor<T> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (InstantiationException e) {
Logger.warn("Failed to create instance of %s: %s", clazz.getName(), e);
throw new UnexpectedException(e);
} catch (IllegalAccessException e) {
Logger.warn("Failed to create instance of %s: %s", clazz.getName(), e);
throw new UnexpectedException(e);
} catch (NoSuchMethodException e) {
Logger.error("Failed to create instance of %s: %s", clazz.getName(), e);
throw new UnexpectedException(e);
} catch (InvocationTargetException e) {
Logger.error("Failed to create instance of %s: %s", clazz.getName(), e);
throw new UnexpectedException(e);
}
}

Expand Down
11 changes: 11 additions & 0 deletions framework/test-src/play/data/binding/BinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ public void test_enum_set_binding() {
assertThat(binded.testEnumSet).isEqualTo(data.testEnumSet);
}

@Test
public void test_binding_class_with_private_constructor() {
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("user.name", new String[]{"john"});

RootParamNode rootParamNode = ParamNode.convert(params);

Data6 binded = (Data6) Binder.bind(rootParamNode, "user", Data6.class, Data6.class, noAnnotations);
assertThat(binded.name).isEqualTo("john");
}

/**
* Transforms map from Unbinder to Binder
* @param r map filled by Unbinder
Expand Down
15 changes: 15 additions & 0 deletions framework/test-src/play/data/binding/Data6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package play.data.binding;

class Data6 {

public String name;

private Data6() {
// this should be used by Play Binder
}

public Data6(String name) {
// this should be used by application code
this.name = name;
}
}

0 comments on commit a518ea8

Please sign in to comment.