This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new d9b31a3 ClassInfo refactoring.
d9b31a3 is described below
commit d9b31a344f17b5bc3c6af0d94d56c32fd76814c3
Author: JamesBognar <[email protected]>
AuthorDate: Mon Jan 31 09:49:04 2022 -0500
ClassInfo refactoring.
---
.../main/java/org/apache/juneau/BeanContext.java | 4 +-
.../main/java/org/apache/juneau/BeanFilter.java | 18 +-
.../java/org/apache/juneau/BeanPropertyMeta.java | 9 +-
.../main/java/org/apache/juneau/BeanRegistry.java | 5 +-
.../src/main/java/org/apache/juneau/ClassMeta.java | 4 +-
.../src/main/java/org/apache/juneau/Value.java | 12 ++
.../java/org/apache/juneau/cp/BeanCreator.java | 229 +++++++++++++++------
.../main/java/org/apache/juneau/cp/BeanStore.java | 12 +-
.../apache/juneau/html/HtmlBeanPropertyMeta.java | 9 +-
.../juneau/httppart/bean/RequestBeanMeta.java | 12 +-
.../httppart/bean/RequestBeanPropertyMeta.java | 4 +-
.../httppart/bean/ResponseBeanPropertyMeta.java | 9 +-
.../org/apache/juneau/internal/ClassUtils.java | 170 ---------------
.../org/apache/juneau/parser/ParserSession.java | 3 +-
.../juneau/serializer/SerializerSession.java | 4 +-
.../rest/client/remote/RemoteOperationArg.java | 4 +-
.../java/org/apache/juneau/rest/RestOpContext.java | 4 +-
.../org/apache/juneau/utils/ClassUtilsTest.java | 78 -------
18 files changed, 222 insertions(+), 368 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
index 21251e6..c725b24 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
@@ -14,7 +14,6 @@ package org.apache.juneau;
import static org.apache.juneau.Visibility.*;
import static org.apache.juneau.collections.OMap.*;
-import static org.apache.juneau.internal.ClassUtils.*;
import static org.apache.juneau.internal.CollectionUtils.*;
import static org.apache.juneau.internal.ThrowableUtils.*;
import static org.apache.juneau.internal.StringUtils.*;
@@ -31,6 +30,7 @@ import java.util.stream.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.json.*;
@@ -3608,7 +3608,7 @@ public class BeanContext extends Context {
} else {
ClassInfo ci = ClassInfo.of((Class<?>)o);
if (ci.isChildOf(ObjectSwap.class))
-
_swaps.add(castOrCreate(ObjectSwap.class, ci.inner()));
+
_swaps.add(BeanCreator.of(ObjectSwap.class).type(ci).run());
else if (ci.isChildOf(Surrogate.class))
_swaps.addAll(SurrogateSwap.findObjectSwaps(ci.inner(), this));
else
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanFilter.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanFilter.java
index 9453c72..0ebda71 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanFilter.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanFilter.java
@@ -15,11 +15,11 @@ package org.apache.juneau;
import java.beans.*;
import java.util.*;
-import static org.apache.juneau.internal.ClassUtils.*;
import static org.apache.juneau.internal.StringUtils.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.swap.*;
/**
@@ -71,9 +71,10 @@ public final class BeanFilter {
writeOnlyProperties = ASet.of();
Class<?> implClass, interfaceClass, stopClass;
boolean sortProperties, fluentSetters;
- Object propertyNamer;
+ BeanCreator<PropertyNamer> propertyNamer =
BeanCreator.of(PropertyNamer.class);
List<Class<?>> dictionary;
- Object interceptor;
+ @SuppressWarnings("rawtypes")
+ BeanCreator<BeanInterceptor> interceptor =
BeanCreator.of(BeanInterceptor.class);
/**
* Constructor.
@@ -434,7 +435,7 @@ public final class BeanFilter {
* @return This object.
*/
public Builder propertyNamer(Class<? extends PropertyNamer>
value) {
- this.propertyNamer = value;
+ this.propertyNamer.type(value);
return this;
}
@@ -711,7 +712,7 @@ public final class BeanFilter {
* @return This object.
*/
public Builder interceptor(Class<?> value) {
- this.interceptor = value;
+ this.interceptor.type(value);
return this;
}
@@ -755,15 +756,12 @@ public final class BeanFilter {
this.stopClass = builder.stopClass;
this.sortProperties = builder.sortProperties;
this.fluentSetters = builder.fluentSetters;
- this.propertyNamer = castOrCreate(PropertyNamer.class,
builder.propertyNamer);
+ this.propertyNamer = builder.propertyNamer.orElse(null);
this.beanDictionary =
builder.dictionary == null
? null
: builder.dictionary.toArray(new
Class<?>[builder.dictionary.size()]);
- this.interceptor =
- builder.interceptor == null
- ? BeanInterceptor.DEFAULT
- : castOrCreate(BeanInterceptor.class,
builder.interceptor);
+ this.interceptor =
builder.interceptor.orElse(BeanInterceptor.DEFAULT);
}
/**
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
index ee4acb9..71224c4 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
@@ -27,6 +27,7 @@ import java.util.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.reflect.*;
@@ -318,7 +319,7 @@ public final class BeanPropertyMeta {
private ObjectSwap getPropertySwap(Beanp p) throws Exception {
if (! p.format().isEmpty())
- return castOrCreate(ObjectSwap.class,
StringFormatSwap.class, false, p.format());
+ return
BeanCreator.of(ObjectSwap.class).type(StringFormatSwap.class).arg(String.class,
p.format()).run();
return null;
}
@@ -330,7 +331,7 @@ public final class BeanPropertyMeta {
return null;
ClassInfo ci = ClassInfo.of(c);
if (ci.isChildOf(ObjectSwap.class)) {
- ObjectSwap ps = castOrCreate(ObjectSwap.class,
c);
+ ObjectSwap ps =
BeanCreator.of(ObjectSwap.class).type(c).run();
if (ps.forMediaTypes() != null)
throw runtimeException("TODO - Media
types on swaps not yet supported on bean properties.");
if (ps.withTemplate() != null)
@@ -746,7 +747,7 @@ public final class BeanPropertyMeta {
}
} else {
if (propMap == null) {
- propMap =
castOrCreate(Map.class, propertyClass);
+ propMap =
BeanCreator.of(Map.class).type(propertyClass).run();
} else {
propMap.clear();
}
@@ -802,7 +803,7 @@ public final class BeanPropertyMeta {
propList.clear();
} else {
if (propList == null) {
- propList =
castOrCreate(Collection.class, propertyClass);
+ propList =
BeanCreator.of(Collection.class).type(propertyClass).run();
invokeSetter(bean,
pName, propList);
} else {
propList.clear();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
index eee2938..349bddc 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
@@ -20,6 +20,7 @@ import java.util.*;
import java.util.concurrent.*;
import org.apache.juneau.annotation.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.reflect.*;
/**
@@ -71,7 +72,7 @@ public class BeanRegistry {
ClassInfo ci = ClassInfo.of(c);
if (ci.isChildOf(Collection.class)) {
@SuppressWarnings("rawtypes")
- Collection cc =
castOrCreate(Collection.class, c);
+ Collection cc =
BeanCreator.of(Collection.class).type(c).run();
for (Object o : cc) {
if (o instanceof Class)
addClass((Class<?>)o);
@@ -79,7 +80,7 @@ public class BeanRegistry {
throw new
BeanRuntimeException("Collection class ''{0}'' passed to BeanRegistry does not
contain Class objects.", className(c));
}
} else if (ci.isChildOf(Map.class)) {
- Map<?,?> m = castOrCreate(Map.class, c);
+ Map<?,?> m =
BeanCreator.of(Map.class).type(c).run();
for (Map.Entry<?,?> e : m.entrySet()) {
String typeName =
stringify(e.getKey());
Object v = e.getValue();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index 36e7934..394f057 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -13,7 +13,6 @@
package org.apache.juneau;
import static org.apache.juneau.ClassMeta.ClassCategory.*;
-import static org.apache.juneau.internal.ClassUtils.*;
import static org.apache.juneau.internal.ThrowableUtils.*;
import static org.apache.juneau.internal.ObjectUtils.*;
import static java.util.Optional.*;
@@ -32,6 +31,7 @@ import java.util.function.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.json.*;
@@ -729,7 +729,7 @@ public final class ClassMeta<T> implements Type {
ClassInfo ci = ClassInfo.of(c);
if (ci.isChildOf(ObjectSwap.class)) {
- ObjectSwap ps = castOrCreate(ObjectSwap.class,
c);
+ ObjectSwap ps =
BeanCreator.of(ObjectSwap.class).type(c).run();
if (s.mediaTypes().length > 0)
ps.forMediaTypes(MediaType.ofAll(s.mediaTypes()));
if (! s.template().isEmpty())
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Value.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Value.java
index c519566..291f9dc 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Value.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Value.java
@@ -152,6 +152,18 @@ public class Value<T> {
}
/**
+ * Sets the value if it's not already set.
+ *
+ * @param t The new value.
+ * @return This object.
+ */
+ public Value<T> setIfEmpty(T t) {
+ if (isEmpty())
+ set(t);
+ return this;
+ }
+
+ /**
* Returns the value.
*
* @return The value, or <jk>null</jk> if it is not set.
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanCreator.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanCreator.java
index cca79ac..94ec17f 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanCreator.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanCreator.java
@@ -13,6 +13,9 @@
package org.apache.juneau.cp;
import static java.util.stream.Collectors.*;
+import static org.apache.juneau.Visibility.*;
+import static java.util.Optional.*;
+
import java.util.*;
import java.util.function.*;
@@ -111,11 +114,30 @@ import org.apache.juneau.reflect.*;
* @param <T> The bean type being created.
*/
public class BeanCreator<T> {
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Static
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Shortcut for calling <c>BeanStore.INSTANCE.createBean(beanType)</c>.
+ *
+ * @param beanType The bean type to create.
+ * @return A new creator.
+ */
+ public static <T> BeanCreator<T> of(Class<T> beanType) {
+ return BeanStore.INSTANCE.createBean(beanType);
+ }
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Instance
+
//-----------------------------------------------------------------------------------------------------------------
private final BeanStore store;
private ClassInfo type;
private Object builder;
private T impl;
+ private boolean silent;
/**
* Constructor.
@@ -142,14 +164,46 @@ public class BeanCreator<T> {
}
/**
+ * Allows you to specify a subclass of the specified bean type to
create.
+ *
+ * @param value The value for this setting.
+ * @return This object.
+ */
+ public BeanCreator<T> type(ClassInfo value) {
+ return type(value == null ? null : value.inner());
+ }
+
+ /**
* Allows you to specify a specific instance for the build method to
return.
*
* @param value The value for this setting.
* @return This object.
*/
- @SuppressWarnings("unchecked")
- public BeanCreator<T> impl(Object value) {
- impl = (T)value;
+ public BeanCreator<T> impl(T value) {
+ impl = value;
+ return this;
+ }
+
+ /**
+ * Adds an argument to this creator.
+ *
+ * @param beanType The parameter type.
+ * @param bean The parameter value.
+ * @return This object.
+ */
+ public <T2> BeanCreator<T> arg(Class<T2> beanType, T2 bean) {
+ store.add(beanType, bean);
+ return this;
+ }
+
+ /**
+ * Suppresses throwing of {@link ExecutableException
ExecutableExceptions} from the {@link #run()} method when
+ * a form of creation cannot be found.
+ *
+ * @return This object.
+ */
+ public BeanCreator<T> silent() {
+ silent = true;
return this;
}
@@ -176,9 +230,29 @@ public class BeanCreator<T> {
}
/**
+ * Same as {@link #run()} but returns the alternate value if a method
of creation could not be found.
+ *
+ * @param other The other bean to use.
+ * @return Either the created or other bean.
+ */
+ public T orElse(T other) {
+ return execute().orElse(other);
+ }
+
+ /**
+ * Same as {@link #run()} but returns the value wrapped in an {@link
Optional}.
+ *
+ * @return A new bean wrapped in an {@link Optional}.
+ */
+ public Optional<T> execute() {
+ return ofNullable(silent().run());
+ }
+
+ /**
* Creates the bean.
*
* @return A new bean.
+ * @throws ExecutableException if bean could not be created and {@link
#silent()} was not enabled.
*/
public T run() {
@@ -188,7 +262,7 @@ public class BeanCreator<T> {
if (type == null)
return null;
- String found = null;
+ Value<String> found = Value.empty();
// Look for getInstance(Builder).
if (builder != null) {
@@ -222,91 +296,79 @@ public class BeanCreator<T> {
if (builder == null) {
// Look for static creator methods.
- MethodInfo matchedCreator = null;
- int matchedCreatorParams = -1;
+ Match<MethodInfo> match = new Match<>();
// Look for static creator method.
- for (MethodInfo m : type.getPublicMethods()) {
- if (isStaticCreateMethod(m)) {
- found = "STATIC_CREATOR";
- if (store.hasAllParams(m)) {
- if (m.getParamCount() >
matchedCreatorParams) {
- matchedCreatorParams =
m.getParamCount();
- matchedCreator = m;
- }
- }
- }
- }
+ type.getPublicMethods(x -> isStaticCreateMethod(x), x
-> {
+ found.set("STATIC_CREATOR");
+ if (hasAllParams(x))
+ match.add(x);
+ });
- if (matchedCreator != null)
- return matchedCreator.invoke(null,
store.getParams(matchedCreator));
+ if (match.isPresent())
+ return match.get().invoke(null,
getParams(match.get()));
}
- if (type.isInterface())
+ if (type.isInterface()) {
+ if (silent)
+ return null;
throw new ExecutableException("Could not instantiate
class {0}: {1}.", type.getName(), "Class is an interface");
+ }
- if (type.isAbstract())
+ if (type.isAbstract()) {
+ if (silent)
+ return null;
throw new ExecutableException("Could not instantiate
class {0}: {1}.", type.getName(), "Class is abstract");
-
- ConstructorInfo matchedConstructor = null;
- int matchedConstructorParams = -1;
+ }
// Look for public constructor.
- for (ConstructorInfo cc : type.getPublicConstructors()) {
- if (found == null)
- found = "PUBLIC_CONSTRUCTOR";
- if (store.hasAllParams(cc)) {
- if (cc.getParamCount() >
matchedConstructorParams) {
- matchedConstructorParams =
cc.getParamCount();
- matchedConstructor = cc;
- }
- }
- }
+ Match<ConstructorInfo> constructorMatch = new Match<>();
+ type.getPublicConstructors(x -> true, x -> {
+ found.setIfEmpty("PUBLIC_CONSTRUCTOR");
+ if (hasAllParams(x))
+ constructorMatch.add(x);
+ });
// Look for protected constructor.
- if (matchedConstructor == null) {
- for (ConstructorInfo cc :
type.getDeclaredConstructors()) {
- if (cc.isProtected()) {
- if (found == null)
- found = "PROTECTED_CONSTRUCTOR";
- if (store.hasAllParams(cc)) {
- if (cc.getParamCount() >
matchedConstructorParams) {
-
matchedConstructorParams = cc.getParamCount();
- matchedConstructor =
cc.accessible();
- }
- }
- }
- }
+ if (! constructorMatch.isPresent()) {
+ type.getDeclaredConstructors(x -> x.isProtected(), x ->
{
+ found.setIfEmpty("PROTECTED_CONSTRUCTOR");
+ if (hasAllParams(x))
+ constructorMatch.add(x);
+ });
}
// Execute.
- if (matchedConstructor != null)
- return
matchedConstructor.invoke(store.getParams(matchedConstructor));
+ if (constructorMatch.isPresent())
+ return
constructorMatch.get().invoke(getParams(constructorMatch.get()));
if (builder == null) {
// Look for static-builder/protected-constructor pair.
- for (ConstructorInfo cc2 :
type.getDeclaredConstructors()) {
- if (cc2.hasNumParams(1) &&
cc2.isVisible(Visibility.PROTECTED)) {
- Class<?> pt =
cc2.getParam(0).getParameterType().inner();
- MethodInfo m = type.getPublicMethod(x
-> isStaticCreateMethod(x, pt));
- if (m != null) {
- Object builder = m.invoke(null);
- return
cc2.accessible().invoke(builder);
- }
+ Value<T> value = Value.empty();
+ type.getDeclaredConstructors(x -> x.hasNumParams(1) &&
x.isVisible(PROTECTED), x -> {
+ Class<?> pt =
x.getParam(0).getParameterType().inner();
+ MethodInfo m = type.getPublicMethod(y ->
isStaticCreateMethod(y, pt));
+ if (m != null) {
+ Object builder = m.invoke(null);
+
value.set(x.accessible().invoke(builder));
}
- }
+ });
+ if (value.isPresent())
+ return value.get();
}
+ if (silent)
+ return null;
+
String msg = null;
- if (found == null) {
+ if (found.isEmpty()) {
msg = "No public/protected constructors found";
- } else if (found.equals("STATIC_CREATOR")) {
-
- msg = "Static creator found but could not find
prerequisites: " + type.getPublicMethods().stream().filter(x ->
isStaticCreateMethod(x)).map(x ->
store.getMissingParams(x)).sorted().collect(joining(" or "));
- } else if (found.equals("PUBLIC_CONSTRUCTOR")) {
- msg = "Public constructor found but could not find
prerequisites: " + type.getPublicConstructors().stream().map(x ->
store.getMissingParams(x)).sorted().collect(joining(" or "));
+ } else if (found.get().equals("STATIC_CREATOR")) {
+ msg = "Static creator found but could not find
prerequisites: " + type.getPublicMethods().stream().filter(x ->
isStaticCreateMethod(x)).map(x ->
getMissingParams(x)).sorted().collect(joining(" or "));
+ } else if (found.get().equals("PUBLIC_CONSTRUCTOR")) {
+ msg = "Public constructor found but could not find
prerequisites: " + type.getPublicConstructors().stream().map(x ->
getMissingParams(x)).sorted().collect(joining(" or "));
} else {
- msg = "Protected constructor found but could not find
prerequisites: " + type.getDeclaredConstructors().stream().filter(x ->
x.isProtected()).map(x -> store.getMissingParams(x)).sorted().collect(joining("
or "));
+ msg = "Protected constructor found but could not find
prerequisites: " + type.getDeclaredConstructors().stream().filter(x ->
x.isProtected()).map(x -> getMissingParams(x)).sorted().collect(joining(" or
"));
}
throw new ExecutableException("Could not instantiate class {0}:
{1}.", type.getName(), msg);
}
@@ -331,4 +393,41 @@ public class BeanCreator<T> {
&& m.hasNoAnnotation(BeanIgnore.class)
&& m.hasName("create");
}
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Helpers
+
//-----------------------------------------------------------------------------------------------------------------
+
+ static class Match<T extends ExecutableInfo> {
+ T executable = null;
+ int numMatches = -1;
+
+ @SuppressWarnings("unchecked")
+ void add(T ei) {
+ if (ei.getParamCount() > numMatches) {
+ numMatches = ei.getParamCount();
+ executable = (T)ei.accessible();
+ }
+ }
+
+ boolean isPresent() {
+ return executable != null;
+ }
+
+ T get() {
+ return executable;
+ }
+ }
+
+ private boolean hasAllParams(ExecutableInfo ei) {
+ return store.hasAllParams(ei);
+ }
+
+ private Object[] getParams(ExecutableInfo ei) {
+ return store.getParams(ei);
+ }
+
+ private String getMissingParams(ExecutableInfo ei) {
+ return store.getMissingParams(ei);
+ }
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanStore.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanStore.java
index 7c3c2b8..df41354 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanStore.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BeanStore.java
@@ -601,9 +601,8 @@ public class BeanStore {
* @return A comma-delimited list of types that are missing from this
factory.
*/
public boolean hasAllParams(ExecutableInfo executable) {
- List<ParamInfo> params = executable.getParams();
- loop: for (int i = 0; i < params.size(); i++) {
- ParamInfo pi = params.get(i);
+ loop: for (int i = 0; i < executable.getParamCount(); i++) {
+ ParamInfo pi = executable.getParam(i);
ClassInfo pt = pi.getParameterType();
if (i == 0 && outer.isPresent() &&
pt.isInstance(outer.get()))
continue loop;
@@ -627,10 +626,9 @@ public class BeanStore {
* @return The corresponding beans in this factory for the specified
param types.
*/
public Object[] getParams(ExecutableInfo executable) {
- List<ParamInfo> params = executable.getParams();
- Object[] o = new Object[params.size()];
- for (int i = 0; i < params.size(); i++) {
- ParamInfo pi = params.get(i);
+ Object[] o = new Object[executable.getParamCount()];
+ for (int i = 0; i < executable.getParamCount(); i++) {
+ ParamInfo pi = executable.getParam(i);
ClassInfo pt = pi.getParameterType();
if (i == 0 && outer.isPresent() &&
pt.isInstance(outer.get())) {
o[i] = outer.get();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
index 8f609a8..8af0fc6 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlBeanPropertyMeta.java
@@ -12,9 +12,8 @@
//
***************************************************************************************************************************
package org.apache.juneau.html;
-import static org.apache.juneau.internal.ClassUtils.*;
-
import org.apache.juneau.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.html.annotation.*;
/**
@@ -59,7 +58,7 @@ public final class HtmlBeanPropertyMeta extends
ExtendedBeanPropertyMeta {
this.format = b.format;
this.noTables = b.noTables;
this.noTableHeaders = b.noTableHeaders;
- this.render = castOrCreate(HtmlRender.class, b.render);
+ this.render = b.render.orElse(null);
this.link = b.link;
this.anchorText = b.anchorText;
}
@@ -77,7 +76,7 @@ public final class HtmlBeanPropertyMeta extends
ExtendedBeanPropertyMeta {
static final class Builder {
boolean noTables, noTableHeaders;
HtmlFormat format = HtmlFormat.HTML;
- Class<? extends HtmlRender> render = HtmlRender.class;
+ BeanCreator<HtmlRender> render =
BeanCreator.of(HtmlRender.class);
String link, anchorText;
void findHtmlInfo(Html html) {
@@ -89,7 +88,7 @@ public final class HtmlBeanPropertyMeta extends
ExtendedBeanPropertyMeta {
if (html.noTableHeaders())
noTableHeaders = html.noTableHeaders();
if (html.render() != HtmlRender.class)
- render = html.render();
+ render.type(html.render());
if (! html.link().isEmpty())
link = html.link();
if (! html.anchorText().isEmpty())
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
index f54cc38..7311918 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
@@ -71,8 +71,8 @@ public class RequestBeanMeta {
RequestBeanMeta(Builder b) {
this.cm = b.cm;
- this.serializer =
BeanStore.INSTANCE.createBean(HttpPartSerializer.class).type(b.serializer).run();
- this.parser =
BeanStore.INSTANCE.createBean(HttpPartParser.class).type(b.parser).run();
+ this.serializer = b.serializer.orElse(null);
+ this.parser = b.parser.orElse(null);
Map<String,RequestBeanPropertyMeta> properties = new
LinkedHashMap<>();
for (Map.Entry<String,RequestBeanPropertyMeta.Builder> e :
b.properties.entrySet())
properties.put(e.getKey(),
e.getValue().build(serializer, parser));
@@ -82,8 +82,8 @@ public class RequestBeanMeta {
static class Builder {
ClassMeta<?> cm;
AnnotationWorkList annotations;
- Class<? extends HttpPartSerializer> serializer;
- Class<? extends HttpPartParser> parser;
+ BeanCreator<HttpPartSerializer> serializer =
BeanCreator.of(HttpPartSerializer.class);
+ BeanCreator<HttpPartParser> parser =
BeanCreator.of(HttpPartParser.class);
Map<String,RequestBeanPropertyMeta.Builder> properties = new
LinkedHashMap<>();
Builder(AnnotationWorkList annotations) {
@@ -127,9 +127,9 @@ public class RequestBeanMeta {
Builder apply(Request a) {
if (a != null) {
if (a.serializer() !=
HttpPartSerializer.Null.class)
- serializer = a.serializer();
+ serializer.type(a.serializer());
if (a.parser() != HttpPartParser.Null.class)
- parser = a.parser();
+ parser.type(a.parser());
}
return this;
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanPropertyMeta.java
index 1d4d57d..1949b9b 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanPropertyMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanPropertyMeta.java
@@ -54,8 +54,8 @@ public class RequestBeanPropertyMeta {
this.partType = b.partType;
this.schema = b.schema;
this.getter = b.getter;
- this.serializer = ofNullable(schema.getSerializer() == null ?
serializer :
BeanStore.INSTANCE.createBean(HttpPartSerializer.class).type(schema.getSerializer()).run());
- this.parser = schema.getParser() == null ? parser :
BeanStore.INSTANCE.createBean(HttpPartParser.class).type(schema.getParser()).run();
+ this.serializer = ofNullable(schema.getSerializer() == null ?
serializer :
BeanCreator.of(HttpPartSerializer.class).type(schema.getSerializer()).run());
+ this.parser = schema.getParser() == null ? parser :
BeanCreator.of(HttpPartParser.class).type(schema.getParser()).run();
}
static class Builder {
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/ResponseBeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/ResponseBeanPropertyMeta.java
index 0737b57..4ea141f 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/ResponseBeanPropertyMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/ResponseBeanPropertyMeta.java
@@ -12,15 +12,12 @@
//
***************************************************************************************************************************
package org.apache.juneau.httppart.bean;
-import static org.apache.juneau.internal.ClassUtils.*;
-
import java.lang.reflect.*;
import java.util.*;
-import static java.util.Optional.*;
-
import org.apache.juneau.http.annotation.*;
import org.apache.juneau.httppart.*;
import org.apache.juneau.reflect.*;
+import org.apache.juneau.cp.*;
/**
* Represents the metadata gathered from a getter method of a class annotated
with {@link Response}.
@@ -54,8 +51,8 @@ public class ResponseBeanPropertyMeta {
this.partType = b.partType;
this.schema = b.schema;
this.getter = b.getter;
- this.serializer = serializer.isPresent() ? serializer :
ofNullable(castOrCreate(HttpPartSerializer.class, schema.getSerializer(),
true));
- this.parser = parser.isPresent() ? parser :
ofNullable(castOrCreate(HttpPartParser.class, schema.getParser(), true));
+ this.serializer = serializer.isPresent() ? serializer :
BeanCreator.of(HttpPartSerializer.class).type(schema.getSerializer()).execute();
+ this.parser = parser.isPresent() ? parser :
BeanCreator.of(HttpPartParser.class).type(schema.getParser()).execute();
}
static class Builder {
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
index 5592b60..88d74f1 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
@@ -12,12 +12,9 @@
//
***************************************************************************************************************************
package org.apache.juneau.internal;
-import static org.apache.juneau.internal.ThrowableUtils.*;
import java.lang.reflect.*;
-import java.util.*;
import org.apache.juneau.*;
-import org.apache.juneau.collections.*;
import org.apache.juneau.reflect.*;
/**
@@ -43,159 +40,6 @@ public final class ClassUtils {
}
/**
- * Creates an instance of the specified class.
- *
- * @param c
- * The class to cast to.
- * @param c2
- * The class to instantiate.
- * Can also be an instance of the class.
- * @return
- * The new class instance, or <jk>null</jk> if the class was
<jk>null</jk> or is abstract or an interface.
- * @throws
- * RuntimeException if constructor could not be found or called.
- */
- public static <T> T castOrCreate(Class<T> c, Object c2) {
- return castOrCreateFromOuter(null, c, c2, false);
- }
-
- /**
- * Creates an instance of the specified class.
- *
- * @param c
- * The class to cast to.
- * @param c2
- * The class to instantiate.
- * Can also be an instance of the class.
- * @param fuzzyArgs
- * Use fuzzy constructor arg matching.
- * <br>When <jk>true</jk>, constructor args can be in any order
and extra args are ignored.
- * <br>No-arg constructors are also used if no other constructors
are found.
- * @param args
- * The arguments to pass to the constructor.
- * @return
- * The new class instance, or <jk>null</jk> if the class was
<jk>null</jk> or is abstract or an interface.
- * @throws
- * RuntimeException if constructor could not be found or called.
- */
- public static <T> T castOrCreate(Class<T> c, Object c2, boolean
fuzzyArgs, Object...args) {
- return castOrCreateFromOuter(null, c, c2, fuzzyArgs, args);
- }
-
- /**
- * Creates an instance of the specified class from within the context
of another object.
- *
- * @param outer
- * The outer object.
- * Can be <jk>null</jk>.
- * @param c
- * The class to cast to.
- * @param c2
- * The class to instantiate.
- * Can also be an instance of the class.
- * @param fuzzyArgs
- * Use fuzzy constructor arg matching.
- * <br>When <jk>true</jk>, constructor args can be in any order
and extra args are ignored.
- * <br>No-arg constructors are also used if no other constructors
are found.
- * @param args
- * The arguments to pass to the constructor.
- * @return
- * The new class instance, or <jk>null</jk> if the class was
<jk>null</jk> or is abstract or an interface.
- * @throws
- * RuntimeException if constructor could not be found or called.
- */
- @SuppressWarnings("unchecked")
- public static <T> T castOrCreateFromOuter(Object outer, Class<T> c,
Object c2, boolean fuzzyArgs, Object...args) {
- if (c2 == null)
- return null;
- if (c2 instanceof Class) {
- try {
- ClassInfo c3 = ClassInfo.of((Class<?>)c2);
-
- MethodInfo mi = fuzzyArgs ?
getStaticCreatorFuzzy(c3, args) : getStaticCreator(c3, args);
-
- if (mi != null)
- return fuzzyArgs ?
(T)mi.invokeFuzzy(null, args) : mi.invoke(null, args);
-
- if (c3.isInterface() || c3.isAbstract())
- return null;
-
- // First look for an exact match.
- Object[] args2 = args;
- ConstructorInfo con = c3.getPublicConstructor(x
-> x.canAccept(args2));
- if (con != null)
- return con.<T>invoke(args);
-
- // Next look for an exact match including the
outer.
- if (outer != null) {
- args =
AList.of(outer).append(args).toArray();
- Object[] args3 = args;
- con = c3.getPublicConstructor(x ->
x.canAccept(args3));
- if (con != null)
- return con.<T>invoke(args);
- }
-
- // Finally use fuzzy matching.
- if (fuzzyArgs) {
- mi = getStaticCreatorFuzzy(c3, args);
- if (mi != null)
- return mi.invoke(null,
getMatchingArgs(mi.getParamTypes(), args));
-
- con = getPublicConstructorFuzzy(c3,
args);
- if (con != null)
- return con.<T>invokeFuzzy(args);
- }
-
- throw runtimeException("Could not instantiate
class {0}/{1}. Constructor not found.", className(c), c2);
- } catch (Exception e) {
- throw runtimeException(e, "Could not
instantiate class {0}", className(c));
- }
- } else if (ClassInfo.of(c).isParentOf(c2.getClass())) {
- return (T)c2;
- } else {
- throw runtimeException("Object of type {0} found but
was expecting {1}.", className(c2), className(c));
- }
- }
-
- private static ConstructorInfo getPublicConstructorFuzzy(ClassInfo c,
Object...args) {
- int bestCount = -1;
- ConstructorInfo bestMatch = null;
- for (ConstructorInfo n : c.getPublicConstructors()) {
- int m = n.fuzzyArgsMatch(args);
- if (m > bestCount) {
- bestCount = m;
- bestMatch = n;
- }
- }
- return bestMatch;
- }
-
- private static MethodInfo getStaticCreatorFuzzy(ClassInfo c,
Object...args) {
- int bestCount = -1;
- MethodInfo bestMatch = null;
- for (MethodInfo m : c.getPublicMethods()) {
- if (m.matches(x -> x.isStatic() && x.isNotDeprecated()
&& x.hasReturnType(c) && x.hasName("create","getInstance"))) {
- int mn = m.canAcceptFuzzy(args);
- if (mn > bestCount) {
- bestCount = mn;
- bestMatch = m;
- }
- }
- }
- return bestMatch;
- }
-
- private static MethodInfo getStaticCreator(ClassInfo c, Object...args) {
- return c.getPublicMethod(
- x -> x.isStatic()
- && x.isNotDeprecated()
- && x.hasReturnType(c)
- && x.hasName("create","getInstance")
- && x.canAccept(args)
- );
- }
-
- /**
* Matches arguments to a list of parameter types.
*
* <p>
@@ -230,20 +74,6 @@ public final class ClassUtils {
return params;
}
- private static Object[] getMatchingArgs(List<ClassInfo> paramTypes,
Object... args) {
- Object[] params = new Object[paramTypes.size()];
- for (int i = 0; i < paramTypes.size(); i++) {
- ClassInfo pt =
paramTypes.get(i).getWrapperInfoIfPrimitive();
- for (int j = 0; j < args.length; j++) {
- if (pt.isParentOf(args[j].getClass())) {
- params[i] = args[j];
- break;
- }
- }
- }
- return params;
- }
-
/**
* Attempts to call <code>x.setAccessible(<jk>true</jk>)</code> and
quietly ignores security exceptions.
*
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
index 505f800..553a29d 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserSession.java
@@ -26,6 +26,7 @@ import java.util.function.*;
import org.apache.juneau.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.httppart.*;
import org.apache.juneau.internal.*;
@@ -253,7 +254,7 @@ public class ParserSession extends BeanSession {
javaMethod = builder.javaMethod;
outer = builder.outer;
schema = builder.schema;
- listener = ClassUtils.castOrCreate(ParserListener.class,
ctx.getListener());
+ listener =
BeanCreator.of(ParserListener.class).type(ctx.getListener()).orElse(null);
sbStack = new Stack<>();
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
index 03fce51..608fb89 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
@@ -13,7 +13,6 @@
package org.apache.juneau.serializer;
import static org.apache.juneau.collections.OMap.*;
-import static org.apache.juneau.internal.ClassUtils.*;
import static org.apache.juneau.internal.StringUtils.*;
import java.io.*;
@@ -25,6 +24,7 @@ import java.util.stream.*;
import org.apache.juneau.*;
import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.httppart.*;
import org.apache.juneau.internal.*;
@@ -291,7 +291,7 @@ public class SerializerSession extends BeanTraverseSession {
javaMethod = builder.javaMethod;
UriContext uriContext = builder.uriContext;
uriResolver = UriResolver.of(ctx.getUriResolution(),
ctx.getUriRelativity(), uriContext);
- listener = castOrCreate(SerializerListener.class,
ctx.getListener());
+ listener =
BeanCreator.of(SerializerListener.class).type(ctx.getListener()).orElse(null);
vrs = builder.resolver;
schema = builder.schema;
}
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/remote/RemoteOperationArg.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/remote/RemoteOperationArg.java
index 92f414f..9451ac9 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/remote/RemoteOperationArg.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/remote/RemoteOperationArg.java
@@ -12,8 +12,6 @@
//
***************************************************************************************************************************
package org.apache.juneau.rest.client.remote;
-import static java.util.Optional.*;
-
import java.util.*;
import static org.apache.juneau.httppart.HttpPartType.*;
@@ -42,7 +40,7 @@ public final class RemoteOperationArg {
RemoteOperationArg(int index, HttpPartType partType, HttpPartSchema
schema) {
this.index = index;
this.partType = partType;
- this.serializer =
ofNullable(BeanStore.INSTANCE.createBean(schema.getSerializer()).run());
+ this.serializer =
BeanCreator.of(HttpPartSerializer.class).type(schema.getSerializer()).execute();
this.schema = schema;
}
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
index 3f0f4c1..c6af797 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
@@ -12,7 +12,6 @@
//
***************************************************************************************************************************
package org.apache.juneau.rest;
-import static org.apache.juneau.internal.ClassUtils.*;
import static org.apache.juneau.internal.ThrowableUtils.*;
import static org.apache.juneau.internal.ObjectUtils.*;
import static org.apache.juneau.internal.StringUtils.*;
@@ -2737,8 +2736,7 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
//-----------------------------------------------------------------------------------------------------------------
private static HttpPartSerializer createPartSerializer(Class<? extends
HttpPartSerializer> c, HttpPartSerializer _default) {
- HttpPartSerializer hps = castOrCreate(HttpPartSerializer.class,
c, true);
- return hps == null ? _default : hps;
+ return
BeanCreator.of(HttpPartSerializer.class).type(c).orElse(_default);
}
private UrlPathMatch matchPattern(RestSession call) {
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/utils/ClassUtilsTest.java
b/juneau-utest/src/test/java/org/apache/juneau/utils/ClassUtilsTest.java
deleted file mode 100755
index e18775c..0000000
--- a/juneau-utest/src/test/java/org/apache/juneau/utils/ClassUtilsTest.java
+++ /dev/null
@@ -1,78 +0,0 @@
-//
***************************************************************************************************************************
-// * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file *
-// * distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file *
-// * to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance *
-// * with the License. You may obtain a copy of the License at
*
-// *
*
-// * http://www.apache.org/licenses/LICENSE-2.0
*
-// *
*
-// * Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an *
-// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
-// * specific language governing permissions and limitations under the
License. *
-//
***************************************************************************************************************************
-package org.apache.juneau.utils;
-
-import static org.apache.juneau.assertions.Assertions.*;
-import static org.junit.Assert.*;
-import static org.junit.Assert.assertEquals;
-import static org.junit.runners.MethodSorters.*;
-
-import org.apache.juneau.internal.*;
-import org.junit.*;
-
-@FixMethodOrder(NAME_ASCENDING)
-public class ClassUtilsTest {
-
-
//====================================================================================================
- // Fuzzy constructor args
-
//====================================================================================================
- @Test
- public void castOrCreateWithFuzzyArgs() throws Exception {
- FA t = null;
-
- t = ClassUtils.castOrCreate(FA.class, FA.class, true);
- assertEquals(1, t.c);
-
- t = ClassUtils.castOrCreate(FA.class, FA.class, true, "foo");
- assertEquals(2, t.c);
-
- t = ClassUtils.castOrCreate(FA.class, FA.class, true, 123,
"foo");
- assertEquals(3, t.c);
-
- t = ClassUtils.castOrCreate(FA.class, FA.class, true, "foo",
123);
- assertEquals(3, t.c);
-
- FB t2 = null;
-
- assertThrown(()->ClassUtils.castOrCreate(FB.class, FB.class,
true)).message().is("Could not instantiate class
org.apache.juneau.utils.ClassUtilsTest$FB");
-
- t2 = ClassUtils.castOrCreate(FB.class, FB.class, true, "foo");
- assertEquals(1, t2.c);
-
- t2 = ClassUtils.castOrCreate(FB.class, FB.class, true, 123,
"foo");
- assertEquals(1, t2.c);
-
- t2 = ClassUtils.castOrCreate(FB.class, FB.class, true, "foo",
123);
- assertEquals(1, t2.c);
- }
-
- public static class FA {
- int c;
- public FA() {
- c = 1;
- }
- public FA(String foo) {
- c = 2;
- }
- public FA(int foo, String bar) {
- c = 3;
- }
- }
-
- public static class FB {
- int c;
- public FB(String foo) {
- c = 1;
- }
- }
-}