classloader

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2019 License: GPL-3.0 Imports: 4 Imported by: 0

README

class_loader

class loader in Go

Example

package main

import (
	"fmt"
	"reflect"

	"github.com/go-trellis/class_loader"
)

type Test struct{}

func (p *Test) Construct() {}

func (p Test) Hello(name string) {
	fmt.Println("hello:", name)
}

func main() {
	c := class_loader.Default

	c.LoadClass("class_loader:Test", (*Test)(nil))

	c.LoadClass("", (*Test)(nil))

	// unsupported class' type: string
	c.LoadClass("class_loader:Aha", "name")

	NameTest()

	ClassLoaderTest()

	DynamicAccess()
}

func ClassLoaderTest() {

	t, e := class_loader.Default.FindClass((*Test)(nil))
	if !e {
		fmt.Println("error:", "class_loader:Test not exist")
		return
	}

	test := reflect.New(t)

	if !test.IsValid() {
		fmt.Println("error:", "class_loader:Test is invalid")
		return
	}

	testI, _ := test.Interface().(*Test)

	testI.Hello("class loaded")

}

func NameTest() {

	t, e := class_loader.Default.FindClass("class_loader:Test")
	if !e {
		fmt.Println("error:", "name class not exist")
		return
	}

	test := reflect.New(t)

	if !test.IsValid() {
		fmt.Println("error:", "name class is invalid")
		return
	}

	testI, _ := test.Interface().(*Test)

	testI.Hello("name class loaded")
}

func DynamicAccess() {
	da := class_loader.NewReflectiveDynamicAccess(class_loader.Default)

	fmt.Println(da.GetClassFor("class_loader:Test"))

	ins, e := da.CreateInstanceByName("class_loader:Test")
	if e != nil {
		fmt.Println("error:", e)
		return
	}

	testI, _ := ins.(*Test)

	testI.Hello("DynamicAccess")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedCreateInstance      = errors.New("failed create instance")
	ErrNotFoundConstructMethod   = errors.New("not found construct method")
	ErrBadActorInitFuncOutNumber = errors.New("the actor init function, result should error or nothing")
)

reflective dynamic access errors

View Source
var Default = NewClassLoader(nil)

Default default class loader

View Source
var ErrorType = reflect.TypeOf((*error)(nil)).Elem()

ErrorType define error type

Functions

This section is empty.

Types

type ClassLoader

type ClassLoader interface {
	GetParent() ClassLoader
	LoadClass(name string, v interface{})
	FindClass(name interface{}) (reflect.Type, bool)
	FindLoadedClass(name string) bool
}

ClassLoader class loader funtions

func NewClassLoader

func NewClassLoader(parent ClassLoader) ClassLoader

NewClassLoader generate class loader with giving parent class loader

type DynamicAccess

type DynamicAccess interface {
	// Convenience method which given a Class[_] object and a constructor description will create a new instance of that class.
	CreateInstanceByType(typ reflect.Type, args ...interface{}) (ins interface{}, err error)
	// Obtain an object conforming to the type T, which is expected to be instantiated from a class designated by the fully-qualified class name given, where the constructor is selected and invoked according to the args argument.
	CreateInstanceByName(name string, args ...interface{}) (ins interface{}, err error)
	// Obtain a Class[_] object loaded with the right class loader (i.e. the one returned by classLoader).
	GetClassFor(name string) (reflect.Type, bool)
}

The DynamicAccess implementation is the class which is used for loading all configurable parts of an actor system (the ReflectiveDynamicAccess is the default implementation). This is an internal facility and users are not expected to encounter it unless they are extending Akka in ways which go beyond simple Extensions.

func NewReflectiveDynamicAccess

func NewReflectiveDynamicAccess(classLoader ClassLoader) DynamicAccess

NewReflectiveDynamicAccess obtain the ReflectiveDynamicAccess

type ReflectiveDynamicAccess

type ReflectiveDynamicAccess struct {
	// contains filtered or unexported fields
}

ReflectiveDynamicAccess is the default implementation akka.DynamicAccess

func (*ReflectiveDynamicAccess) CreateInstanceByName

func (p *ReflectiveDynamicAccess) CreateInstanceByName(name string, args ...interface{}) (
	ins interface{}, err error)

CreateInstanceByName Obtain an object conforming to the type T, which is expected to be instantiated from a class designated by the fully-qualified class name given, where the constructor is selected and invoked according to the args argument.

func (*ReflectiveDynamicAccess) CreateInstanceByType

func (p *ReflectiveDynamicAccess) CreateInstanceByType(typ reflect.Type, args ...interface{}) (
	ins interface{}, err error)

CreateInstanceByType Convenience method which given a Class[_] object and a constructor description will create a new instance of that class.

func (*ReflectiveDynamicAccess) GetClassFor

func (p *ReflectiveDynamicAccess) GetClassFor(name string) (reflect.Type, bool)

GetClassFor Obtain a Class[_] object loaded with the right class loader (i.e. the one returned by classLoader).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL