Changeset 7513


Ignore:
Timestamp:
Nov 2, 2018, 8:45:02 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #2129: Preparations for Java 11 support

Get rid of warnings related to Class.newInstance(). Basically we need to replace it with Class.getDeclaredConstructor().newInstance() but this can throw some extra exceptions that also need to be handled or declared.

Location:
trunk/src/core/net/sf/basedb
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/Application.java

    r7470 r7513  
    822822    try
    823823    {
    824       ssc = (SecondaryStorageController)Class.forName(secondaryStorageDriver).newInstance();
     824      ssc = (SecondaryStorageController)Class.forName(secondaryStorageDriver).getDeclaredConstructor().newInstance();
    825825      ssc.init(Config.getString("secondary.storage.init"));
    826826    }
  • trunk/src/core/net/sf/basedb/core/DbControl.java

    r7381 r7513  
    5757
    5858import java.lang.reflect.Constructor;
     59import java.lang.reflect.InvocationTargetException;
    5960
    6061/**
     
    796797    try
    797798    {
    798       BasicData data = dataClass.newInstance();
     799      BasicData data = dataClass.getDeclaredConstructor().newInstance();
    799800      if (extraParameters != null && extraParameters.length > 0)
    800801      {
     
    815816      item.setDbControl(this); 
    816817      itemCache.put(data, item);
    817       return item;     
    818     }
    819     catch (Exception ex)
     818      return item;
     819    }
     820    catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex)
    820821    {
    821822      throw new BaseException(ex);
  • trunk/src/core/net/sf/basedb/core/InternalJobQueue.java

    r7381 r7513  
    2626import net.sf.basedb.core.signal.LocalSignalReceiver;
    2727import net.sf.basedb.core.signal.SignalReceiver;
     28import net.sf.basedb.util.ClassUtil;
    2829import net.sf.basedb.core.data.JobData;
    2930import net.sf.basedb.core.data.PluginDefinitionData;
     
    277278        if (signalReceiver == null)
    278279        {
    279           signalReceiver = (SignalReceiver)Class.forName(signalReceiverClass).newInstance();
     280          signalReceiver = (SignalReceiver)ClassUtil.newInstance(Class.forName(signalReceiverClass));
    280281          signalReceiver.init(signalReceiverInit);
    281282        }
    282283      }
    283284    }
    284     catch (Exception ex)
     285    catch (ClassNotFoundException ex)
    285286    {
    286287      throw new BaseException(ex);
  • trunk/src/core/net/sf/basedb/core/Job.java

    r7461 r7513  
    4444import net.sf.basedb.core.signal.Signal;
    4545import net.sf.basedb.core.signal.SignalTransporter;
     46import net.sf.basedb.util.ClassUtil;
    4647import net.sf.basedb.util.StaticCache;
    4748import net.sf.basedb.util.error.ThrowableUtil;
     
    940941          String transporterClass = tmp2[0];
    941942          String initParams = tmp2.length > 1 ? tmp2[1] : null;
    942           SignalTransporter transporter = (SignalTransporter)Class.forName(transporterClass).newInstance();
     943          SignalTransporter transporter = (SignalTransporter)ClassUtil.newInstance(Class.forName(transporterClass));
    943944          transporter.init(initParams);
    944945         
     
    955956          signalTransporter = new JobSignalTransporter(transporter);
    956957        }
    957         catch (Exception ex)
     958        catch (ClassNotFoundException ex)
    958959        {
    959960          throw new BaseException(ex);
     
    17751776    Map<String, ParameterValueData<?>> from = copyFrom.getData().getParameters();
    17761777 
    1777     try
    1778     {
    1779       for (Map.Entry<String, ParameterValueData<?>> fromEntry : from.entrySet())
    1780       {
    1781         String toName = fromEntry.getKey();
    1782         ParameterValueData<?> fromParameter = fromEntry.getValue();
    1783         Class<? extends ParameterValueData> dataClass = fromParameter.getClass();
    1784         if (org.hibernate.proxy.HibernateProxy.class.isAssignableFrom(dataClass))
    1785         {
    1786           // The 'fromParameter' is a proxy, we need the real data class
    1787           dataClass =
    1788             (Class<? extends ParameterValueData>)org.hibernate.Hibernate.getClass(fromParameter);
    1789         }
    1790         ParameterValueData<?> toParameter = dataClass.newInstance();
    1791         toParameter.setLabel(fromParameter.getLabel());
    1792         toParameter.setDescription(fromParameter.getDescription());
    1793         toParameter.replaceValuesIfDifferent(fromParameter.getValues(), false);
    1794         to.put(toName, toParameter);
    1795       }
    1796     }
    1797     catch (IllegalAccessException ex)
    1798     {
    1799       throw new BaseException(ex);
    1800     }
    1801     catch (InstantiationException ex)
    1802     {
    1803       throw new BaseException(ex);
     1778    for (Map.Entry<String, ParameterValueData<?>> fromEntry : from.entrySet())
     1779    {
     1780      String toName = fromEntry.getKey();
     1781      ParameterValueData<?> fromParameter = fromEntry.getValue();
     1782      Class<? extends ParameterValueData> dataClass = fromParameter.getClass();
     1783      if (org.hibernate.proxy.HibernateProxy.class.isAssignableFrom(dataClass))
     1784      {
     1785        // The 'fromParameter' is a proxy, we need the real data class
     1786        dataClass =
     1787          (Class<? extends ParameterValueData>)org.hibernate.Hibernate.getClass(fromParameter);
     1788      }
     1789      ParameterValueData<?> toParameter = ClassUtil.newInstance(dataClass);
     1790      toParameter.setLabel(fromParameter.getLabel());
     1791      toParameter.setDescription(fromParameter.getDescription());
     1792      toParameter.replaceValuesIfDifferent(fromParameter.getValues(), false);
     1793      to.put(toName, toParameter);
    18041794    }
    18051795  }
  • trunk/src/core/net/sf/basedb/core/PluginConfiguration.java

    r7381 r7513  
    3636import net.sf.basedb.core.query.Hql;
    3737import net.sf.basedb.core.query.Expressions;
     38import net.sf.basedb.util.ClassUtil;
    3839import net.sf.basedb.util.XmlUtil2;
    3940
     
    602603    int newVersion = getNewParameterVersion();
    603604    int copyFromVersion = copyFrom.getParameterVersion();
    604     try
    605     {
    606       for (Map.Entry<VersionedParameter, ParameterValueData<?>> fromEntry : from.entrySet())
     605
     606    for (Map.Entry<VersionedParameter, ParameterValueData<?>> fromEntry : from.entrySet())
     607    {
     608      VersionedParameter fromName = fromEntry.getKey();
     609      if (copyFromVersion == fromName.getVersion())
    607610      {
    608         VersionedParameter fromName = fromEntry.getKey();
    609         if (copyFromVersion == fromName.getVersion())
     611        VersionedParameter toName = new VersionedParameter(fromName.getName(), newVersion);
     612        ParameterValueData<?> fromParameter = fromEntry.getValue();
     613        Class<? extends ParameterValueData> dataClass = fromParameter.getClass();
     614        if (org.hibernate.proxy.HibernateProxy.class.isAssignableFrom(dataClass))
    610615        {
    611           VersionedParameter toName = new VersionedParameter(fromName.getName(), newVersion);
    612           ParameterValueData<?> fromParameter = fromEntry.getValue();
    613           Class<? extends ParameterValueData> dataClass = fromParameter.getClass();
    614           if (org.hibernate.proxy.HibernateProxy.class.isAssignableFrom(dataClass))
    615           {
    616             // The 'fromParameter' is a proxy, we need the real data class
    617             dataClass =
    618               (Class<? extends ParameterValueData<?>>)org.hibernate.Hibernate.getClass(fromParameter);
    619           }
    620           ParameterValueData<?> toParameter = dataClass.newInstance();
    621           toParameter.setLabel(fromParameter.getLabel());
    622           toParameter.setDescription(fromParameter.getDescription());
    623           toParameter.replaceValuesIfDifferent(fromParameter.getValues(), false);
    624           to.put(toName, toParameter);
     616          // The 'fromParameter' is a proxy, we need the real data class
     617          dataClass =
     618            (Class<? extends ParameterValueData<?>>)org.hibernate.Hibernate.getClass(fromParameter);
    625619        }
     620        ParameterValueData<?> toParameter = ClassUtil.newInstance(dataClass);
     621        toParameter.setLabel(fromParameter.getLabel());
     622        toParameter.setDescription(fromParameter.getDescription());
     623        toParameter.replaceValuesIfDifferent(fromParameter.getValues(), false);
     624        to.put(toName, toParameter);
    626625      }
    627     }
    628     catch (IllegalAccessException ex)
    629     {
    630       throw new BaseException(ex);
    631     }
    632     catch (InstantiationException ex)
    633     {
    634       throw new BaseException(ex);
    635626    }
    636627   
  • trunk/src/core/net/sf/basedb/core/PluginDefinition.java

    r7381 r7513  
    4444import net.sf.basedb.util.extensions.xml.PluginInfo;
    4545
     46import java.io.IOException;
     47import java.lang.reflect.InvocationTargetException;
    4648import java.util.Collection;
    4749import java.util.Collections;
     
    12771279        c = ClassUtil.checkAndLoadClass(cl, className, true, Plugin.class);
    12781280      }
    1279       return (Plugin)c.newInstance();
    1280     }
    1281     catch (BaseException bex)
    1282     {
    1283       throw bex;
    1284     }
    1285     catch (Exception ex)
     1281      return (Plugin)c.getDeclaredConstructor().newInstance();
     1282    }
     1283    catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException | ClassNotFoundException | IOException ex)
    12861284    {
    12871285      throw new BaseException("Could not instantiate plugin class: "+className+"", ex);
  • trunk/src/core/net/sf/basedb/core/SessionControl.java

    r7496 r7513  
    4343import net.sf.basedb.core.data.ContextData;
    4444import net.sf.basedb.core.data.ContextIndex;
     45import net.sf.basedb.util.ClassUtil;
    4546import net.sf.basedb.util.EmailUtil;
    4647import net.sf.basedb.util.Enumeration;
     
    26222623    }
    26232624    // The settings map now contain only those setting not found in the database
    2624     try
    2625     {
    2626       for (Map.Entry<String, SettingInfo> entry : settings.entrySet())
    2627       {
    2628         SettingInfo si = entry.getValue();
    2629         // If the setting has been modified, it should be created
    2630         if (si != null && si.value != null && si.modified)
     2625    for (Map.Entry<String, SettingInfo> entry : settings.entrySet())
     2626    {
     2627      SettingInfo si = entry.getValue();
     2628      // If the setting has been modified, it should be created
     2629      if (si != null && si.value != null && si.modified)
     2630      {
     2631        I s = ClassUtil.newInstance(dataClass);
     2632        s.setName(entry.getKey());
     2633        s.setValue(si.value);
     2634        if (s instanceof UserDefaultSettingData)
    26312635        {
    2632           I s = dataClass.newInstance();
    2633           s.setName(entry.getKey());
    2634           s.setValue(si.value);
    2635           if (s instanceof UserDefaultSettingData)
    2636           {
    2637             ((UserDefaultSettingData)s).setUser(userData);
    2638           }
    2639           if (s instanceof UserClientSettingData)
    2640           {
    2641             ((UserClientSettingData)s).setUser(userData);
    2642             ((UserClientSettingData)s).setClient(clientData);
    2643           }
    2644           HibernateUtil.saveData(session, s);
     2636          ((UserDefaultSettingData)s).setUser(userData);
    26452637        }
    2646       }
    2647     }
    2648     catch (InstantiationException ex)
    2649     {
    2650       throw new BaseException(ex);
    2651     }
    2652     catch (IllegalAccessException ex)
    2653     {
    2654       throw new BaseException(ex);
     2638        if (s instanceof UserClientSettingData)
     2639        {
     2640          ((UserClientSettingData)s).setUser(userData);
     2641          ((UserClientSettingData)s).setClient(clientData);
     2642        }
     2643        HibernateUtil.saveData(session, s);
     2644      }
    26552645    }
    26562646  }
  • trunk/src/core/net/sf/basedb/util/ClassUtil.java

    r7401 r7513  
    2525
    2626import java.lang.reflect.Constructor;
     27import java.lang.reflect.InvocationTargetException;
    2728import java.lang.reflect.Method;
    2829import java.util.Set;
     30
     31import net.sf.basedb.core.BaseException;
     32
    2933import java.util.HashSet;
    3034import java.util.Collections;
     
    4145{
    4246
     47  /**
     48    Utility method for creating a new instance from a class with a default
     49    no-argument constructor. Exceptions are wrapped in BaseExceptions.
     50    @since 3.14
     51  */
     52  public static <I> I newInstance(Class<I> clazz)
     53  {
     54    try
     55    {
     56      return clazz.getDeclaredConstructor().newInstance();
     57    }
     58    catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex)
     59    {
     60      throw new BaseException(ex);
     61    }
     62  }
     63 
    4364  /**
    4465    Find a constructor for the specified class which takes parameters of
  • trunk/src/core/net/sf/basedb/util/extensions/debug/BeanActionFactory.java

    r6898 r7513  
    211211      ClassLoader loader = actionClass.getClassLoader();   
    212212      Class<?> beanClass = ClassUtil.checkAndLoadClass(loader, beanClassName, true, actionClass);
    213       Action bean = actionClass.cast(beanClass.newInstance());
     213      Action bean = actionClass.cast(beanClass.getDeclaredConstructor().newInstance());
    214214      return bean;
    215215    }
  • trunk/src/core/net/sf/basedb/util/extensions/xml/XmlLoader.java

    r7224 r7513  
    260260      ClassLoader classLoader, boolean clear)
    261261    throws IOException, ClassNotFoundException, NoSuchMethodException,
    262       IllegalAccessException, InstantiationException
     262      IllegalAccessException, InstantiationException, InvocationTargetException
    263263  {
    264264    validateXmlFile(xmlFile, filename);
     
    313313  public About loadLastValidatedFile(ClassLoader classLoader, boolean clear)
    314314    throws ClassNotFoundException, IllegalAccessException,
    315     InstantiationException, NoSuchMethodException
     315    InstantiationException, NoSuchMethodException, InvocationTargetException
    316316  {
    317317    if (validatedDom == null)
     
    543543  protected int loadExtensionPoints(Document dom, ClassLoader classLoader, About globalAbout)
    544544    throws ClassNotFoundException, NoSuchMethodException,
    545       IllegalAccessException, InstantiationException
     545      IllegalAccessException, InstantiationException, InvocationTargetException
    546546  {
    547547    log.debug("Loading extension points from file: " + lastName);
     
    634634  protected int loadExtensions(Document dom, ClassLoader classLoader, About globalAbout)
    635635    throws ClassNotFoundException, NoSuchMethodException,
    636       IllegalAccessException, InstantiationException
     636      IllegalAccessException, InstantiationException, InvocationTargetException
    637637  {
    638638    log.debug("Loading extensions from file: " + lastName);
     
    901901  protected <F> F createFactory(Element factoryTag, ClassLoader classLoader, Class<F> factoryType, Preset config)
    902902    throws ClassNotFoundException, NoSuchMethodException,
    903       IllegalAccessException, InstantiationException
     903      IllegalAccessException, InstantiationException, InvocationTargetException
    904904  {
    905905    Namespace ns = factoryTag.getNamespace();
     
    907907    Class<?> factoryClass =
    908908      ClassUtil.checkAndLoadClass(classLoader, factoryClassName, true, factoryType);
    909     F factory = factoryType.cast(factoryClass.newInstance());
     909    F factory = factoryType.cast(factoryClass.getDeclaredConstructor().newInstance());
    910910    initBeanWithReflection(factory, factoryTag.getChild("parameters", ns), config);
    911911    return factory;
  • trunk/src/core/net/sf/basedb/util/overview/loader/BasicItemNodeLoaderFactory.java

    r7461 r7513  
    2222package net.sf.basedb.util.overview.loader;
    2323
     24import java.lang.reflect.InvocationTargetException;
    2425import java.util.HashMap;
    2526import java.util.Map;
     
    103104      try
    104105      {
    105         loader = loaderClass.newInstance();
    106       }
    107       catch (Exception ex)
     106        loader = loaderClass.getDeclaredConstructor().newInstance();
     107      }
     108      catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex)
    108109      {
    109110        throw new BaseException("Can't create node loader: " + loaderClass, ex);
  • trunk/src/core/net/sf/basedb/util/overview/validator/BasicItemNodeValidatorFactory.java

    r7461 r7513  
    2222package net.sf.basedb.util.overview.validator;
    2323
     24import java.lang.reflect.InvocationTargetException;
    2425import java.util.HashMap;
    2526import java.util.Map;
     
    112113      try
    113114      {
    114         validator = (NodeValidator<BasicItem>)validatorClass.newInstance();
    115       }
    116       catch (Exception ex)
     115        validator = (NodeValidator<BasicItem>)validatorClass.getDeclaredConstructor().newInstance();
     116      }
     117      catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex)
    117118      {
    118119        throw new BaseException("Can't create node validator: " + validatorClass, ex);
Note: See TracChangeset for help on using the changeset viewer.