Exemple #1
0
//(Ljava/lang/String;)[Ljava/net/InetAddress;
func i6di_lookupAllHostAddr(frame *rtda.Frame) {
	vars := frame.LocalVars()
	host := rtda.GoString(vars.GetRef(1))
	address, _ := net.LookupHost(host)
	constructorCount := uint(len(address))

	inetAddress := rtc.BootLoader().LoadClass("java/net/InetAddress")
	inetAddressArr := inetAddress.NewArray(constructorCount)

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

	//TODO
	//getByName descriptor:(Ljava/lang/String;)Ljava/net/InetAddress;
	//if constructorCount > 0 {
	//	thread := frame.Thread()
	//	constructorObjs := inetAddressArr.Refs()
	//	inetAddressGetByNameMethod := inetAddress.GetStaticMethod("getByName", "(Ljava/lang/String;)Ljava/net/InetAddress;")

	//	fmt.Println(constructorObjs[0])
	//	fmt.Println(inetAddressGetByNameMethod)
	//	fmt.Println(thread)
	//	thread.InvokeMethodWithShim(inetAddressGetByNameMethod, []Any{
	//		constructorObjs[0],
	//		rtda.JString(host),
	//	})
	//}
}
func (self *Thread) throwException(className, initDesc string, initArgs ...Any) {
	class := rtc.BootLoader().LoadClass(className)
	exObj := class.NewObj()
	athrowFrame := newAthrowFrame(self, exObj, initArgs)
	self.PushFrame(athrowFrame)

	// init exObj
	constructor := class.GetConstructor(initDesc)
	self.InvokeMethod(constructor)
}
Exemple #3
0
func _callValueOf(frame *rtda.Frame, primitiveDescriptor, wrapperClassName string) {
	wrapperClass := rtc.BootLoader().LoadClass(wrapperClassName)
	valueOfDescriptor := "(" + primitiveDescriptor + ")L" + wrapperClassName + ";"
	valueOfMethod := wrapperClass.GetStaticMethod("valueOf", valueOfDescriptor)
	frame.Thread().InvokeMethod(valueOfMethod)

	// init wrapper class
	if wrapperClass.InitializationNotStarted() {
		frame.Thread().InitClass(wrapperClass)
	}
}
Exemple #4
0
// todo: is there a better way to create String?
// go string -> java.lang.String
func JString(goStr string) *rtc.Obj {
	internedStr := getInternedString(goStr)
	if internedStr != nil {
		return internedStr
	}

	chars := _stringToUtf16(goStr)
	charArr := rtc.NewCharArray(chars)
	jStr := rtc.BootLoader().JLStringClass().NewObj()
	jStr.SetFieldValue("value", "[C", charArr)
	return InternString(goStr, jStr)
}
Exemple #5
0
// native ConstantPool getConstantPool();
// ()Lsun/reflect/ConstantPool;
func getConstantPool(frame *rtda.Frame) {
	class := _popClass(frame)
	cpClass := rtc.BootLoader().LoadClass("sun/reflect/ConstantPool")
	if cpClass.InitializationNotStarted() {
		frame.RevertNextPC()
		frame.Thread().InitClass(cpClass)
		return
	}

	cp := class.ConstantPool()
	cpObj := cpClass.NewObjWithExtra(cp) // todo init cpObj
	frame.OperandStack().PushRef(cpObj)
}
Exemple #6
0
// private native Class<?>[] getInterfaces0();
// ()[Ljava/lang/Class;
func getInterfaces0(frame *rtda.Frame) {
	class := _popClass(frame)
	interfaces := class.Interfaces()
	interfaceObjs := make([]*rtc.Obj, len(interfaces))
	for i, iface := range interfaces {
		interfaceObjs[i] = iface.JClass()
	}

	jlClassClass := rtc.BootLoader().JLClassClass()
	interfaceArr := rtc.NewRefArray2(jlClassClass, interfaceObjs)

	stack := frame.OperandStack()
	stack.PushRef(interfaceArr)
}
Exemple #7
0
// native ClassLoader getClassLoader0();
// ()Ljava/lang/ClassLoader;
func getClassLoader0(frame *rtda.Frame) {
	class := _popClass(frame)
	from := class.LoadedFrom()

	stack := frame.OperandStack()
	if cp.IsBootClassPath(from) {
		stack.PushRef(nil)
		return
	}

	clClass := rtc.BootLoader().LoadClass("java/lang/ClassLoader")
	getSysCl := clClass.GetStaticMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;")
	frame.Thread().InvokeMethod(getSysCl)
}
Exemple #8
0
func getParameterTypeArr(method *rtc.Method) *rtc.Obj {
	paramTypes := method.ParameterTypes()
	paramCount := len(paramTypes)

	classClass := rtc.BootLoader().JLClassClass()
	classArr := classClass.NewArray(uint(paramCount))

	if paramCount > 0 {
		classObjs := classArr.Refs()
		for i, paramType := range paramTypes {
			classObjs[i] = paramType.JClass()
		}
	}

	return classArr
}
Exemple #9
0
func getExceptionTypeArr(method *rtc.Method) *rtc.Obj {
	exTypes := method.ExceptionTypes()
	exCount := len(exTypes)

	classClass := rtc.BootLoader().JLClassClass()
	classArr := classClass.NewArray(uint(exCount))

	if exCount > 0 {
		classObjs := classArr.Refs()
		for i, exType := range exTypes {
			classObjs[i] = exType.JClass()
		}
	}

	return classArr
}
Exemple #10
0
func (self *bootstrap) Execute(frame *rtda.Frame) {
	thread := frame.Thread()

	if _classLoader == nil {
		_classLoader = rtc.BootLoader()
		initVars(frame)
	}
	if bootClassesNotReady(thread) ||
		mainThreadNotReady(thread) ||
		jlSystemNotReady(thread) ||
		mainClassNotReady(thread) {

		return
	}

	execMain(thread)
}
Exemple #11
0
// private static native InetAddress localInetAddress(FileDescriptor fd) throws IOException;
func net_localInetAddress(frame *rtda.Frame) {
	//vars := frame.LocalVars()
	//this := vars.GetThis()
	//listen := this.Extra().(net.Listener)

	inetAddress := rtc.BootLoader().LoadClass("java/net/InetAddress")
	inetObj := inetAddress.NewObj()

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

	//fmt.Println(inetAddress)
	//fmt.Println(listen.Addr().String())
	//fmt.Println(listen.Addr().Network())

	//panic("net_localInetAddress error")
}
Exemple #12
0
// private native Class<?> findBootstrapClass(String name);
// (Ljava/lang/String;)Ljava/lang/Class;
func findBootstrapClass(frame *rtda.Frame) {
	vars := frame.LocalVars()
	//this := vars.GetThis()
	name := vars.GetRef(1)

	className := rtc.DotToSlash(rtda.GoString(name))
	class := rtc.BootLoader().LoadClass(className)

	// todo: init class?
	stack := frame.OperandStack()
	stack.PushRef(class.JClass())

	// todo
	if r := recover(); r != nil {
		frame.OperandStack().PushRef(nil)
	}
}
// 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
			})
		}
	}
}
Exemple #14
0
// private native final Class<?> findLoadedClass0(String name);
// (Ljava/lang/String;)Ljava/lang/Class;
func findLoadedClass0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	name := vars.GetRef(1)

	className := rtc.DotToSlash(rtda.GoString(name))

	if isAppClassLoader(this) {
		class := rtc.BootLoader().FindLoadedClass(className)
		if class != nil {
			frame.OperandStack().PushRef(class.JClass())
		} else {
			frame.OperandStack().PushRef(nil)
		}
		return
	}

	// todo
	frame.OperandStack().PushRef(nil)
}
Exemple #15
0
func (self *Thread) HandleUncaughtException(ex *rtc.Obj) {
	self.stack.clear()
	sysClass := rtc.BootLoader().LoadClass("java/lang/System")
	sysErr := sysClass.GetStaticValue("out", "Ljava/io/PrintStream;").(*rtc.Obj)
	printStackTrace := ex.Class().GetInstanceMethod("printStackTrace", "(Ljava/io/PrintStream;)V")

	// call ex.printStackTrace(System.err)
	newFrame := self.NewFrame(printStackTrace)
	vars := newFrame.localVars
	vars.SetRef(0, ex)
	vars.SetRef(1, sysErr)
	self.PushFrame(newFrame)

	//
	// printString := sysErr.Class().GetInstanceMethod("print", "(Ljava/lang/String;)V")
	// newFrame = self.NewFrame(printString)
	// vars = newFrame.localVars
	// vars.SetRef(0, sysErr)
	// vars.SetRef(1, JString("Exception in thread \"main\" ", newFrame))
	// self.PushFrame(newFrame)
}
// private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
// (Z)[Ljava/lang/reflect/Constructor;
func getDeclaredConstructors0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	classObj := vars.GetThis()
	publicOnly := vars.GetBoolean(1)

	class := classObj.Extra().(*rtc.Class)
	constructors := class.GetConstructors(publicOnly)
	constructorCount := uint(len(constructors))

	constructorClass := rtc.BootLoader().LoadClass("java/lang/reflect/Constructor")
	constructorArr := constructorClass.NewArray(constructorCount)

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

	if constructorCount > 0 {
		thread := frame.Thread()
		constructorObjs := constructorArr.Refs()
		constructorInitMethod := constructorClass.GetConstructor(_constructorConstructorDescriptor)
		for i, constructor := range constructors {
			constructorObj := constructorClass.NewObjWithExtra(constructor)
			constructorObjs[i] = constructorObj

			// init constructorObj
			thread.InvokeMethodWithShim(constructorInitMethod, []Any{
				constructorObj, // this
				classObj,       // declaringClass
				getParameterTypeArr(constructor),    // parameterTypes
				getExceptionTypeArr(constructor),    // checkedExceptions
				int32(constructor.GetAccessFlags()), // modifiers
				int32(0), // todo slot
				getSignatureStr(constructor.Signature()),                    // signature
				getAnnotationByteArr(constructor.AnnotationData()),          // annotations
				getAnnotationByteArr(constructor.ParameterAnnotationData()), // parameterAnnotations
			})
		}
	}
}
// 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
			})
		}
	}
}
Exemple #18
0
func _sysProps() map[string]string {
	return map[string]string{
		"java.version":         "1.8.0",
		"java.vendor":          "jvm.go",
		"java.vendor.url":      "https://github.com/zxh0/jvm.go",
		"java.home":            options.AbsJavaHome,
		"java.class.version":   "52.0",
		"java.class.path":      rtc.BootLoader().ClassPath().String(),
		"java.awt.graphicsenv": "sun.awt.CGraphicsEnvironment",
		"os.name":              runtime.GOOS,   // todo
		"os.arch":              runtime.GOARCH, // todo
		"os.version":           "",             // todo
		"file.separator":       "/",            // todo os.PathSeparator
		"path.separator":       ":",            // todo os.PathListSeparator
		"line.separator":       "\n",           // todo
		"user.name":            "",             // todo
		"user.home":            "",             // todo
		"user.dir":             ".",            // todo
		"user.country":         "CN",           // todo
		"file.encoding":        "UTF-8",
		"sun.stdout.encoding":  "UTF-8",
		"sun.stderr.encoding":  "UTF-8",
	}
}
Exemple #19
0
// todo
func (self *Frame) ClassLoader() *rtc.ClassLoader {
	return rtc.BootLoader()
}