|
| 1 | +/* Copyright (c) 2018 Matthias Bläsing |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | + |
| 25 | +package com.sun.jna; |
| 26 | + |
| 27 | +import java.lang.reflect.InvocationTargetException; |
| 28 | + |
| 29 | +abstract class Klass { |
| 30 | + |
| 31 | + private Klass() { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a new instance for the given {@code klass}. Runtime exceptions |
| 36 | + * thrown from the constructor are rethrown, all other exceptions |
| 37 | + * generated from the reflective call are wrapped into a |
| 38 | + * {@link java.lang.IllegalArgumentException} and rethrown. |
| 39 | + * |
| 40 | + * @param klass desired class to instantiate |
| 41 | + * @return the new instance |
| 42 | + * @throws IllegalArgumentException if the instantiation fails |
| 43 | + * @throws RuntimeException if the constructor for {@code klass} throws |
| 44 | + * a runtime exception |
| 45 | + */ |
| 46 | + public static <T> T newInstance(Class<T> klass) { |
| 47 | + try { |
| 48 | + return klass.getDeclaredConstructor().newInstance(); |
| 49 | + } catch (IllegalAccessException e) { |
| 50 | + String msg = "Can't create an instance of " + klass |
| 51 | + + ", requires a public no-arg constructor: " + e; |
| 52 | + throw new IllegalArgumentException(msg, e); |
| 53 | + } catch (IllegalArgumentException e) { |
| 54 | + String msg = "Can't create an instance of " + klass |
| 55 | + + ", requires a public no-arg constructor: " + e; |
| 56 | + throw new IllegalArgumentException(msg, e); |
| 57 | + } catch (InstantiationException e) { |
| 58 | + String msg = "Can't create an instance of " + klass |
| 59 | + + ", requires a public no-arg constructor: " + e; |
| 60 | + throw new IllegalArgumentException(msg, e); |
| 61 | + } catch (NoSuchMethodException e) { |
| 62 | + String msg = "Can't create an instance of " + klass |
| 63 | + + ", requires a public no-arg constructor: " + e; |
| 64 | + throw new IllegalArgumentException(msg, e); |
| 65 | + } catch (SecurityException e) { |
| 66 | + String msg = "Can't create an instance of " + klass |
| 67 | + + ", requires a public no-arg constructor: " + e; |
| 68 | + throw new IllegalArgumentException(msg, e); |
| 69 | + } catch (InvocationTargetException e) { |
| 70 | + if (e.getCause() instanceof RuntimeException) { |
| 71 | + throw (RuntimeException) e.getCause(); |
| 72 | + } else { |
| 73 | + String msg = "Can't create an instance of " + klass |
| 74 | + + ", requires a public no-arg constructor: " + e; |
| 75 | + throw new IllegalArgumentException(msg, e); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments