Example #1
0
//([B)Ljava/lang/String;
//String getHostByAddr(byte[] var1) throws UnknownHostException
func i6di_getHostByAddr(frame *rtda.Frame) {
	vars := frame.LocalVars()
	t := vars.GetRef(1)
	buf := t.GoBytes()
	address := fmt.Sprintf("%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3])
	if name, err := net.LookupAddr(address); err == nil {
		frame.OperandStack().PushRef(rtda.JString(name[0]))
	} else {
		panic("not found from lookupAllHostAddr")
		frame.OperandStack().PushRef(rtda.JString(""))
	}
}
Example #2
0
// private static native String getVersion0();
// ()Ljava/lang/String;
func getVersion0(frame *rtda.Frame) {
	// todo
	version := rtda.JString("0")

	stack := frame.OperandStack()
	stack.PushRef(version)
}
Example #3
0
func mainThreadNotReady(thread *rtda.Thread) bool {
	stack := thread.CurrentFrame().OperandStack()
	if _mainThreadGroup == nil {
		undoExec(thread)
		threadGroupClass := _classLoader.LoadClass("java/lang/ThreadGroup")
		_mainThreadGroup = threadGroupClass.NewObj()
		initMethod := threadGroupClass.GetConstructor("()V")
		stack.PushRef(_mainThreadGroup) // this
		thread.InvokeMethod(initMethod)
		return true
	}
	if thread.JThread() == nil {
		undoExec(thread)
		threadClass := _classLoader.LoadClass("java/lang/Thread")
		mainThreadObj := threadClass.NewObjWithExtra(thread)
		mainThreadObj.SetFieldValue("priority", "I", int32(1))
		thread.HackSetJThread(mainThreadObj)

		initMethod := threadClass.GetConstructor("(Ljava/lang/ThreadGroup;Ljava/lang/String;)V")
		stack.PushRef(mainThreadObj)        // this
		stack.PushRef(_mainThreadGroup)     // group
		stack.PushRef(rtda.JString("main")) // name
		thread.InvokeMethod(initMethod)
		return true
	}
	return false
}
Example #4
0
// private native String getName0();
// ()Ljava/lang/String;
func getName0(frame *rtda.Frame) {
	class := _popClass(frame)
	name := class.NameJlsFormat()
	nameObj := rtda.JString(name)

	stack := frame.OperandStack()
	stack.PushRef(nameObj)
}
Example #5
0
// private static native Properties initProperties(Properties props);
// (Ljava/util/Properties;)Ljava/util/Properties;
func initProperties(frame *rtda.Frame) {
	vars := frame.LocalVars()
	props := vars.GetRef(0)

	stack := frame.OperandStack()
	stack.PushRef(props)

	// public synchronized Object setProperty(String key, String value)
	setPropMethod := props.Class().GetInstanceMethod("setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;")
	thread := frame.Thread()
	for key, val := range _sysProps() {
		jKey := rtda.JString(key)
		jVal := rtda.JString(val)
		args := []Any{props, jKey, jVal}
		thread.InvokeMethodWithShim(setPropMethod, args)
	}
}
Example #6
0
func createArgs() *rtc.Obj {
	jArgs := make([]*rtc.Obj, len(_args))
	for i, arg := range _args {
		jArgs[i] = rtda.JString(arg)
	}

	return rtc.NewRefArray2(_classLoader.JLStringClass(), jArgs)
}
Example #7
0
func _createEnclosintMethodInfo(classLoader *rtc.ClassLoader, emInfo *rtc.EnclosingMethod) *rtc.Obj {
	if emInfo == nil {
		return nil
	}

	enclosingClass := classLoader.LoadClass(emInfo.ClassName())
	enclosingClassObj := enclosingClass.JClass()
	var methodNameObj, methodDescriptorObj *rtc.Obj
	if emInfo.MethodName() != "" {
		methodNameObj = rtda.JString(emInfo.MethodName())
		methodDescriptorObj = rtda.JString(emInfo.MethodDescriptor())
	} else {
		methodNameObj, methodDescriptorObj = nil, nil
	}

	objs := []*rtc.Obj{enclosingClassObj, methodNameObj, methodDescriptorObj}
	return rtc.NewRefArray2(classLoader.JLObjectClass(), objs) // Object[]
}
Example #8
0
// private static native String getSystemPackage0(String name);
// (Ljava/lang/String;)Ljava/lang/String;
func getSystemPackage0(frame *rtda.Frame) {
	// vars := frame.LocalVars()
	// name := vars.GetRef(0)

	sysPkg := frame.ClassLoader().JLObjectClass().LoadedFrom().String()
	sysPkgObj := rtda.JString(sysPkg)

	stack := frame.OperandStack()
	stack.PushRef(sysPkgObj)
}
Example #9
0
// private native String canonicalize0(String path) throws IOException;
// (Ljava/lang/String;)Ljava/lang/String;
func canonicalize0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	pathStr := vars.GetRef(1)

	// todo
	path := rtda.GoString(pathStr)
	path2 := filepath.Clean(path)
	if path2 != path {
		pathStr = rtda.JString(path2)
	}

	stack := frame.OperandStack()
	stack.PushRef(pathStr)
}
Example #10
0
// private native String getGenericSignature0();
// ()Ljava/lang/String;
func getGenericSignature0(frame *rtda.Frame) {
	class := _popClass(frame)
	if class == nil {
		panic("illegal class")
	}

	// Return null for arrays and primatives
	if !class.IsPrimitive() {
		signature := class.Signature()
		if signature == "" {
			frame.OperandStack().PushNull()
		} else {
			frame.OperandStack().PushRef(rtda.JString(signature))
		}
		return
	}

	frame.OperandStack().PushNull()
}
Example #11
0
// private native Method[] getDeclaredMethods0(boolean publicOnly);
// (Z)[Ljava/lang/reflect/Method;
func getDeclaredMethods0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	classObj := vars.GetThis()
	publicOnly := vars.GetBoolean(1)

	class := classObj.Extra().(*rtc.Class)
	methods := class.GetMethods(publicOnly)
	methodCount := uint(len(methods))

	methodClass := rtc.BootLoader().LoadClass("java/lang/reflect/Method")
	methodArr := methodClass.NewArray(methodCount)

	stack := frame.OperandStack()
	stack.PushRef(methodArr)

	// create method objs
	if methodCount > 0 {
		thread := frame.Thread()
		methodObjs := methodArr.Refs()
		methodConstructor := methodClass.GetConstructor(_methodConstructorDescriptor)
		for i, method := range methods {
			methodObj := methodClass.NewObjWithExtra(method)
			methodObjs[i] = methodObj

			// init methodObj
			thread.InvokeMethodWithShim(methodConstructor, []Any{
				methodObj,                                              // this
				classObj,                                               // declaringClass
				rtda.JString(method.Name()),                            // name
				getParameterTypeArr(method),                            // parameterTypes
				getReturnType(method),                                  // returnType
				getExceptionTypeArr(method),                            // checkedExceptions
				int32(method.GetAccessFlags()),                         // modifiers
				int32(method.Slot()),                                   // slot
				getSignatureStr(method.Signature()),                    // signature
				getAnnotationByteArr(method.AnnotationData()),          // annotations
				getAnnotationByteArr(method.ParameterAnnotationData()), // parameterAnnotations
				getAnnotationByteArr(method.AnnotationDefaultData()),   // annotationDefault
			})
		}
	}
}
Example #12
0
File: ldc.go Project: cretz/jvm.go
func _ldc(frame *rtda.Frame, index uint) {
	stack := frame.OperandStack()
	cp := frame.ConstantPool()
	c := cp.GetConstant(index)

	switch c.(type) {
	case int32:
		stack.PushInt(c.(int32))
	case float32:
		stack.PushFloat(c.(float32))
	case string:
		internedStr := rtda.JString(c.(string))
		stack.PushRef(internedStr)
	case *rtc.ConstantClass:
		kClass := c.(*rtc.ConstantClass)
		classObj := kClass.Class().JClass()
		stack.PushRef(classObj)
	default:
		// todo
		// ref to MethodType or MethodHandle
		jutil.Panicf("todo: ldc! %v", c)
	}
}
Example #13
0
// private native Field[] getDeclaredFields0(boolean publicOnly);
// (Z)[Ljava/lang/reflect/Field;
func getDeclaredFields0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	classObj := vars.GetThis()
	publicOnly := vars.GetBoolean(1)

	class := classObj.Extra().(*rtc.Class)
	fields := class.GetFields(publicOnly)
	fieldCount := uint(len(fields))

	fieldClass := rtc.BootLoader().LoadClass("java/lang/reflect/Field")
	fieldArr := rtc.NewRefArray(fieldClass, fieldCount)

	stack := frame.OperandStack()
	stack.PushRef(fieldArr)

	if fieldCount > 0 {
		thread := frame.Thread()
		fieldObjs := fieldArr.Refs()
		fieldConstructor := fieldClass.GetConstructor(_fieldConstructorDescriptor)
		for i, goField := range fields {
			fieldObj := fieldClass.NewObjWithExtra(goField)
			fieldObjs[i] = fieldObj

			// init fieldObj
			thread.InvokeMethodWithShim(fieldConstructor, []Any{
				fieldObj,                                       // this
				classObj,                                       // declaringClass
				rtda.JString(goField.Name()),                   // name
				goField.Type().JClass(),                        // type
				int32(goField.GetAccessFlags()),                // modifiers
				int32(goField.SlotId()),                        // slot
				getSignatureStr(goField.Signature()),           // signature
				getAnnotationByteArr(goField.AnnotationData()), // annotations
			})
		}
	}
}
Example #14
0
func getSignatureStr(signature string) *rtc.Obj {
	if signature != "" {
		return rtda.JString(signature)
	}
	return nil
}