From 0bf1f0a0edb373abe1cb9bc3bf6a6c7ce179036a Mon Sep 17 00:00:00 2001 From: Clisprail Date: Tue, 5 Aug 2014 01:31:30 +0200 Subject: [PATCH] RefConstructor class added --- .../parabot/core/reflect/RefConstructor.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 parabotv2/src/org/parabot/core/reflect/RefConstructor.java diff --git a/parabotv2/src/org/parabot/core/reflect/RefConstructor.java b/parabotv2/src/org/parabot/core/reflect/RefConstructor.java new file mode 100644 index 0000000..0473788 --- /dev/null +++ b/parabotv2/src/org/parabot/core/reflect/RefConstructor.java @@ -0,0 +1,100 @@ +package org.parabot.core.reflect; + +import java.lang.reflect.Constructor; + +/** + * + * A RefConstructor class represent a constructor method of a RefClass. + * + * @author Everel + * + */ +public class RefConstructor extends RefModifiers { + private Constructor constructor; + + public RefConstructor(Constructor constructor) { + super(constructor.getModifiers()); + this.constructor = constructor; + } + + /** + * Creates a new instance of this class by invoking this constructor + * @return the instance of the class + */ + public RefClass newInstance() { + return newInstance(new Object[] { }); + } + + /** + * Creates a new instance of this class by invoking this constructor + * @param args the arguments for the constructor + * @return the instance of the class + */ + public RefClass newInstance(Object... args) { + if(!constructor.isAccessible()) { + constructor.setAccessible(true); + } + try { + Object instance = constructor.newInstance(args); + return new RefClass(instance); + } catch(Throwable t) { + t.printStackTrace(); + } + return null; + } + + /** + * Get the value of the accessible flag for this object. + * + * @return the value of the object's accessible flag + */ + public boolean isAccessible() { + return constructor.isAccessible(); + } + + /** + * Returns true if this constructor is a synthetic constructor; returns + * false otherwise. + * + * @return true if this constructor is a synthetic constructor; returns + * false otherwise + */ + public boolean isSynthetic() { + return constructor.isSynthetic(); + } + + /** + * Returns the name of the constructor. + * + * @return name of the constructor + */ + public String getName() { + return constructor.getName(); + } + + /** + * Returns an array of the parameter types of this constructor + * @return an array of the parameter types of this constructor + */ + public Class[] getParameterTypes() { + return constructor.getParameterTypes(); + } + + /** + * Gets the java reflection API constructor representation + * + * @return constructor + */ + public Constructor getConstructor() { + return this.constructor; + } + + public String toGenericString() { + return constructor.toGenericString(); + } + + public String toString() { + return constructor.toString(); + } + +}