private HasValue<?> makeFieldInstance(
Class<? extends HasValue<?>> fieldClass) {
try {
- return fieldClass.newInstance();
- } catch (InstantiationException | IllegalAccessException e) {
- throw new IllegalStateException(
- String.format("Couldn't create an '%s' type instance",
- fieldClass.getName()),
- e);
+ return ReflectTools.createInstance(fieldClass);
+ } catch (IllegalArgumentException e) {
+ // Rethrow as the exception type declared for bindInstanceFields
+ throw new IllegalStateException(e);
}
}
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.SingleComponentContainer;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
/**
* A navigator utility that allows switching of views in a part of an
@Override
public View getView(String viewName) {
if (this.viewName.equals(viewName)) {
- try {
- View view = viewClass.newInstance();
- return view;
- } catch (InstantiationException | IllegalAccessException e) {
- // TODO error handling
- throw new RuntimeException(e);
- }
- // TODO error handling
-
+ return ReflectTools.createInstance(viewClass);
}
return null;
}
setErrorProvider(new ViewProvider() {
@Override
public View getView(String viewName) {
- try {
- return viewClass.newInstance();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
+ return ReflectTools.createInstance(viewClass);
}
@Override
import com.vaadin.ui.HasComponents;
import com.vaadin.ui.LegacyComponent;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
*/
protected SharedState createState() {
try {
- return getStateType().newInstance();
+ return ReflectTools.createInstance(getStateType());
} catch (Exception e) {
throw new RuntimeException("Error creating state of type "
+ getStateType().getName() + " for " + getClass().getName(),
import com.vaadin.ui.Dependency;
import com.vaadin.ui.Dependency.Type;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
import elemental.json.Json;
import elemental.json.JsonException;
/**
* The URI resolver used in the bootstrap process.
- *
+ *
* @since 8.1
*/
protected static class BootstrapUriResolver extends VaadinUriResolver {
Class<? extends ViewportGenerator> viewportGeneratorClass = viewportGeneratorClassAnnotation
.value();
try {
- viewportContent = viewportGeneratorClass.newInstance()
+ viewportContent = ReflectTools
+ .createInstance(viewportGeneratorClass)
.getViewport(context.getRequest());
} catch (Exception e) {
throw new RuntimeException(
import com.vaadin.shared.communication.UidlValue;
import com.vaadin.ui.Component;
import com.vaadin.ui.ConnectorTracker;
+import com.vaadin.util.ReflectTools;
import elemental.json.Json;
import elemental.json.JsonArray;
Class<?> targetClass = getClassForType(targetType);
try {
- Object decodedObject = targetClass.newInstance();
+ Object decodedObject = ReflectTools.createInstance(targetClass);
for (BeanProperty property : getProperties(targetClass)) {
String fieldName = property.getName();
import com.vaadin.ui.HasComponents;
import com.vaadin.ui.SelectiveRenderer;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
}
try {
- SharedState referenceState = stateType.newInstance();
+ SharedState referenceState = ReflectTools.createInstance(stateType);
EncodeResult encodeResult = JsonCodec.encode(referenceState, null,
stateType, null);
return encodeResult.getEncodedValue();
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
+import com.vaadin.util.ReflectTools;
+
public class LegacyVaadinPortlet extends VaadinPortlet {
private static final LegacyApplicationUIProvider provider = new LegacyApplicationUIProvider() {
throws PortletException {
try {
Class<? extends LegacyApplication> applicationClass = getApplicationClass();
- return applicationClass.newInstance();
+ return ReflectTools.createInstance(applicationClass);
} catch (Exception e) {
throw new PortletException(e);
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
+import com.vaadin.util.ReflectTools;
+
public class LegacyVaadinServlet extends VaadinServlet {
private static final UIProvider provider = new LegacyApplicationUIProvider() {
throws ServletException {
try {
Class<? extends LegacyApplication> applicationClass = getApplicationClass();
- return applicationClass.newInstance();
+ return ReflectTools.createInstance(applicationClass);
} catch (Exception e) {
throw new ServletException(e);
}
import com.vaadin.shared.ApplicationConstants;
import com.vaadin.ui.Component;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
/**
* Contains helper methods shared by {@link VaadinServlet} and
Class<?> providerClass = classLoader.loadClass(uiProviderProperty);
Class<? extends UIProvider> subclass = providerClass
.asSubclass(UIProvider.class);
- return subclass.newInstance();
+ return ReflectTools.createInstance(subclass);
} catch (ClassNotFoundException e) {
throw new ServiceException(
"Could not load UIProvider class " + uiProviderProperty, e);
} catch (ClassCastException e) {
throw new ServiceException("UIProvider class " + uiProviderProperty
+ " does not extend UIProvider", e);
- } catch (InstantiationException | IllegalAccessException e) {
- throw new ServiceException(
- "Could not instantiate UIProvider " + uiProviderProperty,
- e);
}
}
import com.vaadin.shared.communication.PushMode;
import com.vaadin.shared.ui.ui.Transport;
import com.vaadin.ui.UI;
+import com.vaadin.util.ReflectTools;
public abstract class UIProvider implements Serializable {
public abstract Class<? extends UI> getUIClass(UIClassSelectionEvent event);
public UI createInstance(UICreateEvent event) {
- try {
- return event.getUIClass().newInstance();
- } catch (InstantiationException e) {
- throw new RuntimeException("Could not instantiate UI class", e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException("Could not access UI class", e);
- }
+ return ReflectTools.createInstance(event.getUIClass());
}
/**
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.declarative.DesignContext.ComponentCreatedEvent;
import com.vaadin.ui.declarative.DesignContext.ComponentCreationListener;
+import com.vaadin.util.ReflectTools;
/**
* Design is used for reading a component hierarchy from an html string or input
+ " which is not a Vaadin Component class";
try {
- return componentClass.newInstance();
+ return ReflectTools.createInstance(componentClass);
} catch (Exception e) {
throw new DesignException(
"Could not create component " + fullyQualifiedClassName,
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
/**
* An util class with helpers for reflection operations. Used internally by
* @since 6.2
*/
public class ReflectTools implements Serializable {
+
+ static final String CREATE_INSTANCE_FAILED = "Unable to create an instance of {0}. Make sure it has a no-arg constructor";
+ static final String CREATE_INSTANCE_FAILED_FOR_NON_STATIC_MEMBER_CLASS = "Unable to create an instance of {0}. Make sure the class is static if it is a nested class.";
+ static final String CREATE_INSTANCE_FAILED_ACCESS_EXCEPTION = "Unable to create an instance of {0}. Make sure the class is public and that is has a public no-arg constructor.";
+ static final String CREATE_INSTANCE_FAILED_NO_PUBLIC_NOARG_CONSTRUCTOR = "Unable to create an instance of {0}. Make sure the class has a public no-arg constructor.";
+ static final String CREATE_INSTANCE_FAILED_LOCAL_CLASS = "Cannot instantiate local class '%s'. Move class declaration outside the method.";
+ static final String CREATE_INSTANCE_FAILED_CONSTRUCTOR_THREW_EXCEPTION = "Unable to create an instance of {0}. The constructor threw an exception.";
+
/**
* Locates the method in the given class. Returns null if the method is not
* found. Throws an ExceptionInInitializerError if there is a problem
return currentClass;
}
+
+ /**
+ * Creates a instance of the given class with a no-arg constructor.
+ * <p>
+ * Catches all exceptions which might occur and wraps them in a
+ * {@link IllegalArgumentException} with a descriptive error message hinting
+ * of what might be wrong with the class that could not be instantiated.
+ *
+ * @param cls
+ * the class to instantiate
+ * @return an instance of the class
+ * @since 8.1.1
+ */
+ public static <T> T createInstance(Class<T> cls) {
+ checkClassAccessibility(cls);
+ try {
+ return cls.getConstructor().newInstance();
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException(String.format(
+ CREATE_INSTANCE_FAILED_NO_PUBLIC_NOARG_CONSTRUCTOR,
+ cls.getName()), e);
+ } catch (InstantiationException e) {
+ if (cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers())) {
+ throw new IllegalArgumentException(String.format(
+ CREATE_INSTANCE_FAILED_FOR_NON_STATIC_MEMBER_CLASS,
+ cls.getName()), e);
+ } else {
+ throw new IllegalArgumentException(
+ String.format(CREATE_INSTANCE_FAILED, cls.getName()),
+ e);
+ }
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(String.format(
+ CREATE_INSTANCE_FAILED_ACCESS_EXCEPTION, cls.getName()), e);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(
+ String.format(CREATE_INSTANCE_FAILED, cls.getName()), e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException(String.format(
+ CREATE_INSTANCE_FAILED_CONSTRUCTOR_THREW_EXCEPTION,
+ cls.getName()), e);
+ }
+ }
+
+ /**
+ * Makes a check whether the provided class is externally accessible for
+ * instantiation (e.g. it's not inner class (nested and not static) and is
+ * not a local class).
+ *
+ * @param cls
+ * type to check
+ */
+ private static void checkClassAccessibility(Class<?> cls) {
+ if (cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers())) {
+ throw new IllegalArgumentException(String.format(
+ CREATE_INSTANCE_FAILED_FOR_NON_STATIC_MEMBER_CLASS,
+ cls.getName()));
+ } else if (cls.isLocalClass()) {
+ throw new IllegalArgumentException(String
+ .format(CREATE_INSTANCE_FAILED_LOCAL_CLASS, cls.getName()));
+ }
+ }
+
}