Primary object constructor This method is simpler than those that must resolve general method invocation because constructors are not inherited. This method determines whether to attempt to use non-public constructors based on Capabilities.haveAccessibility() and will set the accessibilty flag
(Class clas, Object[] args)
| 542 | * <p/> |
| 543 | */ |
| 544 | static Object constructObject(Class clas, Object[] args) throws ReflectError, InvocationTargetException { |
| 545 | if (clas.isInterface()) { |
| 546 | throw new ReflectError("Can't create instance of an interface: " + clas); |
| 547 | } |
| 548 | |
| 549 | Class[] types = Types.getTypes(args); |
| 550 | |
| 551 | // Find the constructor. |
| 552 | // (there are no inherited constructors to worry about) |
| 553 | Constructor[] constructors = Capabilities.haveAccessibility() ? clas.getDeclaredConstructors() : clas.getConstructors(); |
| 554 | |
| 555 | if (Interpreter.DEBUG) { |
| 556 | Interpreter.debug("Looking for most specific constructor: " + clas); |
| 557 | } |
| 558 | Constructor con = findMostSpecificConstructor(types, constructors); |
| 559 | if (con == null) { |
| 560 | throw cantFindConstructor(clas, types); |
| 561 | } |
| 562 | |
| 563 | if (!isPublic(con) && Capabilities.haveAccessibility()) { |
| 564 | con.setAccessible(true); |
| 565 | } |
| 566 | |
| 567 | args = Primitive.unwrap(args); |
| 568 | try { |
| 569 | return con.newInstance(args); |
| 570 | } catch (InstantiationException e) { |
| 571 | throw new ReflectError("The class " + clas + " is abstract ", e); |
| 572 | } catch (IllegalAccessException e) { |
| 573 | throw new ReflectError("We don't have permission to create an instance. Use setAccessibility(true) to enable access.", e); |
| 574 | } catch (IllegalArgumentException e) { |
| 575 | throw new ReflectError("The number of arguments was wrong", e); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | /* |
no test coverage detected