MCPcopy Index your code
hub / github.com/beanshell/beanshell / initInstance

Method initInstance

src/bsh/ClassGeneratorUtil.java:903–1000  ·  view source on GitHub ↗

Initialize an instance of the class. This method is called from the generated class constructor to evaluate the instance initializer and scripted constructor in the instance namespace.

(GeneratedClass instance, String className, Object[] args)

Source from the content-addressed store, hash-verified

901 * namespace.
902 */
903 public static void initInstance(GeneratedClass instance, String className, Object[] args) {
904 Class[] sig = Types.getTypes(args);
905 CallStack callstack = new CallStack();
906 Interpreter interpreter;
907 NameSpace instanceNameSpace;
908
909 // check to see if the instance has already been initialized
910 // (the case if using a this() alternate constuctor)
911 // todo PeJoBo70 write test for this
912 This instanceThis = getClassInstanceThis(instance, className);
913
914 // XXX clean up this conditional
915 if (instanceThis == null) {
916 // Create the instance 'This' namespace, set it on the object
917 // instance and invoke the instance initializer
918
919 // Get the static This reference from the proto-instance
920 This classStaticThis = getClassStaticThis(instance.getClass(), className);
921 interpreter = CONTEXT_INTERPRETER.get();
922 if (interpreter == null) {
923 interpreter = classStaticThis.declaringInterpreter;
924 }
925
926
927 // Get the instance initializer block from the static This
928 BSHBlock instanceInitBlock;
929 try {
930 instanceInitBlock = (BSHBlock) classStaticThis.getNameSpace().getVariable(BSHINIT);
931 } catch (Exception e) {
932 throw new InterpreterError("unable to get instance initializer: " + e);
933 }
934
935 // Create the instance namespace
936 if (CONTEXT_NAMESPACE.get() != null) {
937 instanceNameSpace = classStaticThis.getNameSpace().copy();
938 instanceNameSpace.setParent(CONTEXT_NAMESPACE.get());
939 } else {
940 instanceNameSpace = new NameSpace(classStaticThis.getNameSpace(), className); // todo: old code
941 }
942 instanceNameSpace.isClass = true;
943
944 // Set the instance This reference on the instance
945 instanceThis = instanceNameSpace.getThis(interpreter);
946 try {
947 LHS lhs = Reflect.getLHSObjectField(instance, BSHTHIS + className);
948 lhs.assign(instanceThis, false/*strict*/);
949 } catch (Exception e) {
950 throw new InterpreterError("Error in class gen setup: " + e);
951 }
952
953 // Give the instance space its object import
954 instanceNameSpace.setClassInstance(instance);
955
956 // should use try/finally here to pop ns
957 callstack.push(instanceNameSpace);
958
959 // evaluate the instance portion of the block in it
960 try { // Evaluate the initializer block

Callers

nothing calls this directly

Calls 15

getTypesMethod · 0.95
getClassInstanceThisMethod · 0.95
getClassStaticThisMethod · 0.95
getNameSpaceMethod · 0.95
setParentMethod · 0.95
getThisMethod · 0.95
getLHSObjectFieldMethod · 0.95
assignMethod · 0.95
setClassInstanceMethod · 0.95
pushMethod · 0.95
evalBlockMethod · 0.95
popMethod · 0.95

Tested by

no test coverage detected