diff --git a/cpg-language-go/src/main/golang/declarations.go b/cpg-language-go/src/main/golang/declarations.go index e2b039fe51..611476027a 100644 --- a/cpg-language-go/src/main/golang/declarations.go +++ b/cpg-language-go/src/main/golang/declarations.go @@ -66,11 +66,11 @@ func (f *FunctionDeclaration) SetType(t *Type) { } func (f *FunctionDeclaration) AddParameter(p *ParamVariableDeclaration) { - (*jnigi.ObjectRef)(f).CallMethod(env, "addParameter", jnigi.Void, (*jnigi.ObjectRef)(p)) + (*jnigi.ObjectRef)(f).CallMethod(env, "addParameter", nil, (*jnigi.ObjectRef)(p)) } func (f *FunctionDeclaration) SetBody(s *Statement) (err error) { - _, err = (*jnigi.ObjectRef)(f).CallMethod(env, "setBody", jnigi.Void, (*jnigi.ObjectRef)(s).Cast("de/fraunhofer/aisec/cpg/graph/statements/Statement")) + err = (*jnigi.ObjectRef)(f).CallMethod(env, "setBody", nil, (*jnigi.ObjectRef)(s).Cast("de/fraunhofer/aisec/cpg/graph/statements/Statement")) return } @@ -88,14 +88,15 @@ func (m *MethodDeclaration) SetReceiver(v *VariableDeclaration) error { } func (m *MethodDeclaration) GetReceiver() *VariableDeclaration { - o, err := (*jnigi.ObjectRef)(m).GetField(env, "receiver", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/VariableDeclaration")) + o := jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/VariableDeclaration") + err := (*jnigi.ObjectRef)(m).GetField(env, "receiver", o) if err != nil { log.Fatal(err) debug.PrintStack() } - return (*VariableDeclaration)(o.(*jnigi.ObjectRef)) + return (*VariableDeclaration)(o) } func (p *ParamVariableDeclaration) SetType(t *Type) { @@ -127,7 +128,7 @@ func (v *VariableDeclaration) IsNil() bool { } func (v *VariableDeclaration) SetInitializer(e *Expression) (err error) { - _, err = (*jnigi.ObjectRef)(v).CallMethod(env, "setInitializer", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + err = (*jnigi.ObjectRef)(v).CallMethod(env, "setInitializer", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) return } @@ -137,13 +138,14 @@ func (v *VariableDeclaration) Declaration() *Declaration { } func (t *TranslationUnitDeclaration) GetIncludeByName(s string) *IncludeDeclaration { - i, err := (*jnigi.ObjectRef)(t).CallMethod(env, "getIncludeByName", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/IncludeDeclaration"), NewString(s)) + var i = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/IncludeDeclaration") + err := (*jnigi.ObjectRef)(t).CallMethod(env, "getIncludeByName", i, NewString(s)) if err != nil { log.Fatal(err) debug.PrintStack() } - return (*IncludeDeclaration)(i.(*jnigi.ObjectRef)) + return (*IncludeDeclaration)(i) } func (r *RecordDeclaration) SetName(s string) error { @@ -155,7 +157,7 @@ func (r *RecordDeclaration) SetKind(s string) error { } func (r *RecordDeclaration) AddMethod(m *MethodDeclaration) (err error) { - _, err = (*jnigi.ObjectRef)(r).CallMethod(env, "addMethod", jnigi.Void, (*jnigi.ObjectRef)(m)) + err = (*jnigi.ObjectRef)(r).CallMethod(env, "addMethod", nil, (*jnigi.ObjectRef)(m)) return } @@ -177,23 +179,26 @@ func (c *CaseStatement) SetCaseExpression(e *Expression) error { } func NewTranslationUnitDeclaration(fset *token.FileSet, astNode ast.Node, name string, code string) *TranslationUnitDeclaration { - o, err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/NodeBuilder", "newTranslationUnitDeclaration", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/TranslationUnitDeclaration"), NewString(name), NewString(code)) + tu := jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/TranslationUnitDeclaration") + + err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/NodeBuilder", "newTranslationUnitDeclaration", tu, NewString(name), NewString(code)) if err != nil { log.Fatal(err) debug.PrintStack() } - return (*TranslationUnitDeclaration)(o.(*jnigi.ObjectRef)) + return (*TranslationUnitDeclaration)(tu) } func NewNamespaceDeclaration(fset *token.FileSet, astNode ast.Node, name string, code string) *NamespaceDeclaration { - o, err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/NodeBuilder", "newNamespaceDeclaration", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/NamespaceDeclaration"), NewString(name), NewString(code)) + var o = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/NamespaceDeclaration") + err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/NodeBuilder", "newNamespaceDeclaration", o, NewString(name), NewString(code)) if err != nil { log.Fatal(err) debug.PrintStack() } - return (*NamespaceDeclaration)(o.(*jnigi.ObjectRef)) + return (*NamespaceDeclaration)(o) } func NewIncludeDeclaration(fset *token.FileSet, astNode ast.Node) *IncludeDeclaration { diff --git a/cpg-language-go/src/main/golang/expressions.go b/cpg-language-go/src/main/golang/expressions.go index 43189f0d24..4d2e2da80f 100644 --- a/cpg-language-go/src/main/golang/expressions.go +++ b/cpg-language-go/src/main/golang/expressions.go @@ -34,6 +34,20 @@ import ( ) type Expression Statement + +func (e *Expression) ConvertToGo(o *jnigi.ObjectRef) error { + *e = (Expression)(*o) + return nil +} + +func (e *Expression) GetClassName() string { + return "de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression" +} + +func (e *Expression) IsArray() bool { + return false +} + type CallExpression Expression type NewExpression Expression type ArrayCreationExpression Expression @@ -254,12 +268,13 @@ func (m *MemberExpression) SetBase(e *Expression) { } func (m *MemberExpression) GetBase() *Expression { - i, err := (*jnigi.ObjectRef)(m).GetField(env, "base", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + var expr Expression + err := (*jnigi.ObjectRef)(m).GetField(env, "base", &expr) if err != nil { log.Fatal(err) } - return (*Expression)(i.(*jnigi.ObjectRef)) + return &expr } func (e *Expression) GetName() string { @@ -275,15 +290,15 @@ func (r *DeclaredReferenceExpression) Node() *Node { } func (c *CallExpression) AddArgument(e *Expression) { - (*jnigi.ObjectRef)(c).CallMethod(env, "addArgument", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(c).CallMethod(env, "addArgument", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (b *BinaryOperator) SetLHS(e *Expression) { - (*jnigi.ObjectRef)(b).CallMethod(env, "setLhs", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(b).CallMethod(env, "setLhs", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (b *BinaryOperator) SetRHS(e *Expression) { - (*jnigi.ObjectRef)(b).CallMethod(env, "setRhs", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(b).CallMethod(env, "setRhs", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (b *BinaryOperator) SetOperatorCode(s string) (err error) { @@ -291,7 +306,7 @@ func (b *BinaryOperator) SetOperatorCode(s string) (err error) { } func (u *UnaryOperator) SetInput(e *Expression) { - (*jnigi.ObjectRef)(u).CallMethod(env, "setInput", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(u).CallMethod(env, "setInput", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (u *UnaryOperator) SetOperatorCode(s string) (err error) { @@ -320,43 +335,43 @@ func (r *DeclaredReferenceExpression) SetName(s string) { } func (r *DeclaredReferenceExpression) SetRefersTo(d *Declaration) { - (*jnigi.ObjectRef)(r).CallMethod(env, "setRefersTo", jnigi.Void, (*jnigi.ObjectRef)(d).Cast("de/fraunhofer/aisec/cpg/graph/declarations/Declaration")) + (*jnigi.ObjectRef)(r).CallMethod(env, "setRefersTo", nil, (*jnigi.ObjectRef)(d).Cast("de/fraunhofer/aisec/cpg/graph/declarations/Declaration")) } func (r *ArrayCreationExpression) AddDimension(e *Expression) { - (*jnigi.ObjectRef)(r).CallMethod(env, "addDimension", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(r).CallMethod(env, "addDimension", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (r *ArraySubscriptionExpression) SetArrayExpression(e *Expression) { - (*jnigi.ObjectRef)(r).CallMethod(env, "setArrayExpression", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(r).CallMethod(env, "setArrayExpression", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (r *ArraySubscriptionExpression) SetSubscriptExpression(e *Expression) { - (*jnigi.ObjectRef)(r).CallMethod(env, "setSubscriptExpression", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(r).CallMethod(env, "setSubscriptExpression", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (c *ConstructExpression) AddArgument(e *Expression) { - (*jnigi.ObjectRef)(c).CallMethod(env, "addArgument", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(c).CallMethod(env, "addArgument", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (c *ConstructExpression) AddPrevDFG(n *Node) { - (*jnigi.ObjectRef)(c).CallMethod(env, "addPrevDFG", jnigi.Void, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/Node")) + (*jnigi.ObjectRef)(c).CallMethod(env, "addPrevDFG", nil, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/Node")) } func (n *NewExpression) SetInitializer(e *Expression) (err error) { - _, err = (*jnigi.ObjectRef)(n).CallMethod(env, "setInitializer", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + err = (*jnigi.ObjectRef)(n).CallMethod(env, "setInitializer", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) return } func (c *InitializerListExpression) AddInitializer(e *Expression) { - (*jnigi.ObjectRef)(c).CallMethod(env, "addInitializer", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(c).CallMethod(env, "addInitializer", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (k *KeyValueExpression) SetKey(e *Expression) { - (*jnigi.ObjectRef)(k).CallMethod(env, "setKey", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(k).CallMethod(env, "setKey", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } func (k *KeyValueExpression) SetValue(e *Expression) { - (*jnigi.ObjectRef)(k).CallMethod(env, "setValue", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(k).CallMethod(env, "setValue", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } diff --git a/cpg-language-go/src/main/golang/frontend/frontend.go b/cpg-language-go/src/main/golang/frontend/frontend.go index dec4f919a7..e853c47aec 100644 --- a/cpg-language-go/src/main/golang/frontend/frontend.go +++ b/cpg-language-go/src/main/golang/frontend/frontend.go @@ -60,12 +60,13 @@ func (g *GoLanguageFrontend) SetCurrentTU(tu *cpg.TranslationUnitDeclaration) { } func (g *GoLanguageFrontend) GetCurrentTU() *cpg.TranslationUnitDeclaration { - i, err := g.GetField(env, "currentTU", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/TranslationUnitDeclaration")) + var tu = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/TranslationUnitDeclaration") + err := g.GetField(env, "currentTU", tu) if err != nil { log.Fatal(err) } - return (*cpg.TranslationUnitDeclaration)(i.(*jnigi.ObjectRef)) + return (*cpg.TranslationUnitDeclaration)(tu) } func (g *GoLanguageFrontend) GetCodeFromRawNode(fset *token.FileSet, astNode ast.Node) string { @@ -76,22 +77,18 @@ func (g *GoLanguageFrontend) GetCodeFromRawNode(fset *token.FileSet, astNode ast } func (g *GoLanguageFrontend) GetScopeManager() *cpg.ScopeManager { - scope, err := g.GetField(env, "scopeManager", jnigi.ObjectType("de/fraunhofer/aisec/cpg/passes/scopes/ScopeManager")) - + var scope = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/passes/scopes/ScopeManager") + err := g.GetField(env, "scopeManager", scope) if err != nil { log.Fatal(err) - } - return (*cpg.ScopeManager)(scope.(*jnigi.ObjectRef)) + return (*cpg.ScopeManager)(scope) } func (g *GoLanguageFrontend) getLog() (logger *jnigi.ObjectRef, err error) { - var ref interface{} - - ref, err = env.GetStaticField("de/fraunhofer/aisec/cpg/frontends/LanguageFrontend", "log", jnigi.ObjectType("org/slf4j/Logger")) - - logger = ref.(*jnigi.ObjectRef) + logger = jnigi.NewObjectRef("org/slf4j/Logger") + err = env.GetStaticField("de/fraunhofer/aisec/cpg/frontends/LanguageFrontend", "log", logger) return } @@ -103,7 +100,7 @@ func (g *GoLanguageFrontend) LogInfo(format string, args ...interface{}) (err er return } - _, err = logger.CallMethod(env, "info", jnigi.Void, cpg.NewString(fmt.Sprintf(format, args...))) + err = logger.CallMethod(env, "info", nil, cpg.NewString(fmt.Sprintf(format, args...))) return } @@ -115,7 +112,7 @@ func (g *GoLanguageFrontend) LogDebug(format string, args ...interface{}) (err e return } - _, err = logger.CallMethod(env, "debug", jnigi.Void, cpg.NewString(fmt.Sprintf(format, args...))) + err = logger.CallMethod(env, "debug", nil, cpg.NewString(fmt.Sprintf(format, args...))) return } @@ -127,7 +124,7 @@ func (g *GoLanguageFrontend) LogError(format string, args ...interface{}) (err e return } - _, err = logger.CallMethod(env, "error", jnigi.Void, cpg.NewString(fmt.Sprintf(format, args...))) + err = logger.CallMethod(env, "error", nil, cpg.NewString(fmt.Sprintf(format, args...))) return } diff --git a/cpg-language-go/src/main/golang/frontend/handler.go b/cpg-language-go/src/main/golang/frontend/handler.go index 5058e115fb..9210cd1471 100644 --- a/cpg-language-go/src/main/golang/frontend/handler.go +++ b/cpg-language-go/src/main/golang/frontend/handler.go @@ -890,12 +890,13 @@ func (this *GoLanguageFrontend) handleNewExpr(fset *token.FileSet, callExpr *ast t := this.handleType(callExpr.Args[0]) // new is a pointer, so need to reference the type with a pointer - pointer, err := env.GetStaticField("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin", "POINTER", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin")) + var pointer = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin") + err := env.GetStaticField("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin", "POINTER", pointer) if err != nil { log.Fatal(err) } - (*cpg.HasType)(n).SetType(t.Reference(pointer.(*jnigi.ObjectRef))) + (*cpg.HasType)(n).SetType(t.Reference(pointer)) // a new expression also needs an initializer, which is usually a constructexpression c := cpg.NewConstructExpression(fset, callExpr) @@ -1161,25 +1162,27 @@ func (this *GoLanguageFrontend) handleType(typeExpr ast.Expr) *cpg.Type { case *ast.StarExpr: t := this.handleType(v.X) - i, err := env.GetStaticField("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin", "POINTER", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin")) + var i = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin") + err := env.GetStaticField("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin", "POINTER", i) if err != nil { log.Fatal(err) } this.LogDebug("Pointer to %s", (*cpg.Node)(t).GetName()) - return t.Reference(i.(*jnigi.ObjectRef)) + return t.Reference(i) case *ast.ArrayType: t := this.handleType(v.Elt) - i, err := env.GetStaticField("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin", "ARRAY", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin")) + var i = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin") + err := env.GetStaticField("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin", "ARRAY", i) if err != nil { log.Fatal(err) } this.LogDebug("Array of %s", (*cpg.Node)(t).GetName()) - return t.Reference(i.(*jnigi.ObjectRef)) + return t.Reference(i) case *ast.MapType: // we cannot properly represent Golangs built-in map types, yet so we have // to make a shortcut here and represent it as a Java-like map type. diff --git a/cpg-language-go/src/main/golang/go.mod b/cpg-language-go/src/main/golang/go.mod index ea3a01aa3c..fee9c73dc5 100644 --- a/cpg-language-go/src/main/golang/go.mod +++ b/cpg-language-go/src/main/golang/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( golang.org/x/mod v0.5.0 - tekao.net/jnigi v0.0.0-20201212091834-f7b899046676 + tekao.net/jnigi v0.0.0-20220804033536-1481db6c0949 ) require golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect diff --git a/cpg-language-go/src/main/golang/go.sum b/cpg-language-go/src/main/golang/go.sum index cfbc6bfc8b..9cc90e6f66 100644 --- a/cpg-language-go/src/main/golang/go.sum +++ b/cpg-language-go/src/main/golang/go.sum @@ -4,3 +4,5 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbO golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= tekao.net/jnigi v0.0.0-20201212091834-f7b899046676 h1:ZEkhDs0eKEynaUD1XX/WnQCD5/zoEUyMkDx5DbvVq7g= tekao.net/jnigi v0.0.0-20201212091834-f7b899046676/go.mod h1:SmVvXetJ8N0ov5c2eOC+IxmkdYGEyuXghTuBq5HWZ/Y= +tekao.net/jnigi v0.0.0-20220804033536-1481db6c0949 h1:dm+mZf0sVfEN1CqfVf4li64lPBczL5VxdVlz2Z7r31M= +tekao.net/jnigi v0.0.0-20220804033536-1481db6c0949/go.mod h1:SmVvXetJ8N0ov5c2eOC+IxmkdYGEyuXghTuBq5HWZ/Y= diff --git a/cpg-language-go/src/main/golang/lib/cpg/main.go b/cpg-language-go/src/main/golang/lib/cpg/main.go index 9a3431f138..c1bbec99c2 100644 --- a/cpg-language-go/src/main/golang/lib/cpg/main.go +++ b/cpg-language-go/src/main/golang/lib/cpg/main.go @@ -67,37 +67,40 @@ func Java_de_fraunhofer_aisec_cpg_frontends_golang_GoLanguageFrontend_parseInter frontend.InitEnv(env) cpg.InitEnv(env) - src, err := srcObject.CallMethod(env, "getBytes", jnigi.Byte|jnigi.Array) + var src []byte + err := srcObject.CallMethod(env, "getBytes", &src) if err != nil { log.Fatal(err) } - path, err := pathObject.CallMethod(env, "getBytes", jnigi.Byte|jnigi.Array) + var path []byte + err = pathObject.CallMethod(env, "getBytes", &path) if err != nil { log.Fatal(err) } - topLevel, err := topLevelObject.CallMethod(env, "getBytes", jnigi.Byte|jnigi.Array) + var topLevel []byte + err = topLevelObject.CallMethod(env, "getBytes", &topLevel) if err != nil { log.Fatal(err) } fset := token.NewFileSet() - file, err := parser.ParseFile(fset, string(path.([]byte)), string(src.([]byte)), parser.ParseComments) + file, err := parser.ParseFile(fset, string(path), string(src), parser.ParseComments) if err != nil { log.Fatal(err) } goFrontend.CommentMap = ast.NewCommentMap(fset, file, file.Comments) - _, err = goFrontend.ParseModule(string(topLevel.([]byte))) + _, err = goFrontend.ParseModule(string(topLevel)) if err != nil { goFrontend.LogError("Error occurred while looking for Go modules file: %v", err) } goFrontend.File = file - tu, err := goFrontend.HandleFile(fset, file, string(path.([]byte))) + tu, err := goFrontend.HandleFile(fset, file, string(path)) if err != nil { log.Fatal(err) } diff --git a/cpg-language-go/src/main/golang/node.go b/cpg-language-go/src/main/golang/node.go index 71be5827db..7de28213ed 100644 --- a/cpg-language-go/src/main/golang/node.go +++ b/cpg-language-go/src/main/golang/node.go @@ -54,18 +54,20 @@ func (n *Node) SetLocation(location *PhysicalLocation) error { } func (n *Node) GetName() string { - o, _ := (*jnigi.ObjectRef)(n).CallMethod(env, "getName", jnigi.ObjectType("java/lang/String")) + var o = jnigi.NewObjectRef("java/lang/String") + _ = (*jnigi.ObjectRef)(n).CallMethod(env, "getName", o) if o == nil { return "" } - b, err := o.(*jnigi.ObjectRef).CallMethod(env, "getBytes", jnigi.Byte|jnigi.Array) + var b []byte + err := o.CallMethod(env, "getBytes", &b) if err != nil { log.Fatal(err) } - return string(b.([]byte)) + return string(b) } func updateCode(fset *token.FileSet, node *Node, astNode ast.Node) { diff --git a/cpg-language-go/src/main/golang/scope.go b/cpg-language-go/src/main/golang/scope.go index 9a5c683f33..24c6f353d2 100644 --- a/cpg-language-go/src/main/golang/scope.go +++ b/cpg-language-go/src/main/golang/scope.go @@ -31,53 +31,57 @@ type ScopeManager jnigi.ObjectRef type Scope jnigi.ObjectRef func (s *ScopeManager) EnterScope(n *Node) { - (*jnigi.ObjectRef)(s).CallMethod(env, "enterScope", jnigi.Void, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/Node")) + (*jnigi.ObjectRef)(s).CallMethod(env, "enterScope", nil, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/Node")) } func (s *ScopeManager) LeaveScope(n *Node) (err error) { - _, err = (*jnigi.ObjectRef)(s).CallMethod(env, "leaveScope", jnigi.ObjectType("de/fraunhofer/aisec/cpg/passes/scopes/Scope"), (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/Node")) + var scope = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/passes/scopes/Scope") + err = (*jnigi.ObjectRef)(s).CallMethod(env, "leaveScope", scope, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/Node")) return err } func (s *ScopeManager) ResetToGlobal(n *Node) { - (*jnigi.ObjectRef)(s).CallMethod(env, "resetToGlobal", jnigi.Void, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/declarations/TranslationUnitDeclaration")) + (*jnigi.ObjectRef)(s).CallMethod(env, "resetToGlobal", nil, (*jnigi.ObjectRef)(n).Cast("de/fraunhofer/aisec/cpg/graph/declarations/TranslationUnitDeclaration")) } func (s *ScopeManager) GetCurrentScope() *Scope { - o, _ := (*jnigi.ObjectRef)(s).CallMethod(env, "getCurrentScope", jnigi.ObjectType("de/fraunhofer/aisec/cpg/passes/scopes/Scope")) + var o = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/passes/scopes/Scope") + (*jnigi.ObjectRef)(s).CallMethod(env, "getCurrentScope", o) - return (*Scope)(o.(*jnigi.ObjectRef)) + return (*Scope)(o) } func (s *ScopeManager) GetCurrentFunction() *FunctionDeclaration { - o, _ := (*jnigi.ObjectRef)(s).CallMethod(env, "getCurrentFunction", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/FunctionDeclaration")) + var o = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/FunctionDeclaration") + (*jnigi.ObjectRef)(s).CallMethod(env, "getCurrentFunction", o) - return (*FunctionDeclaration)(o.(*jnigi.ObjectRef)) + return (*FunctionDeclaration)(o) } func (s *ScopeManager) GetCurrentBlock() *CompoundStatement { - o, _ := (*jnigi.ObjectRef)(s).CallMethod(env, "getCurrentBlock", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/statements/CompoundStatement")) + var o = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/statements/CompoundStatement") + (*jnigi.ObjectRef)(s).CallMethod(env, "getCurrentBlock", o) - return (*CompoundStatement)(o.(*jnigi.ObjectRef)) + return (*CompoundStatement)(o) } func (s *ScopeManager) GetRecordForName(scope *Scope, recordName string) (record *RecordDeclaration, err error) { - var o interface{} + var o = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/RecordDeclaration") - o, err = (*jnigi.ObjectRef)(s).CallMethod(env, + err = (*jnigi.ObjectRef)(s).CallMethod(env, "getRecordForName", - jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/declarations/RecordDeclaration"), + o, (*jnigi.ObjectRef)(scope).Cast("de/fraunhofer/aisec/cpg/passes/scopes/Scope"), NewString(recordName)) - record = (*RecordDeclaration)(o.(*jnigi.ObjectRef)) + record = (*RecordDeclaration)(o) return } func (s *ScopeManager) AddDeclaration(d *Declaration) (err error) { - _, err = (*jnigi.ObjectRef)(s).CallMethod(env, "addDeclaration", jnigi.Void, (*jnigi.ObjectRef)(d).Cast("de/fraunhofer/aisec/cpg/graph/declarations/Declaration")) + err = (*jnigi.ObjectRef)(s).CallMethod(env, "addDeclaration", nil, (*jnigi.ObjectRef)(d).Cast("de/fraunhofer/aisec/cpg/graph/declarations/Declaration")) return } diff --git a/cpg-language-go/src/main/golang/statements.go b/cpg-language-go/src/main/golang/statements.go index 10f97ea074..f928fe7d50 100644 --- a/cpg-language-go/src/main/golang/statements.go +++ b/cpg-language-go/src/main/golang/statements.go @@ -148,11 +148,11 @@ func NewDefaultStatement(fset *token.FileSet, astNode ast.Node) *DefaultStatemen } func (f *CompoundStatement) AddStatement(s *Statement) { - (*jnigi.ObjectRef)(f).CallMethod(env, "addStatement", jnigi.Void, (*jnigi.ObjectRef)(s).Cast("de/fraunhofer/aisec/cpg/graph/statements/Statement")) + (*jnigi.ObjectRef)(f).CallMethod(env, "addStatement", nil, (*jnigi.ObjectRef)(s).Cast("de/fraunhofer/aisec/cpg/graph/statements/Statement")) } func (f *DeclarationStatement) SetSingleDeclaration(d *Declaration) { - (*jnigi.ObjectRef)(f).CallMethod(env, "setSingleDeclaration", jnigi.Void, (*jnigi.ObjectRef)(d).Cast("de/fraunhofer/aisec/cpg/graph/declarations/Declaration")) + (*jnigi.ObjectRef)(f).CallMethod(env, "setSingleDeclaration", nil, (*jnigi.ObjectRef)(d).Cast("de/fraunhofer/aisec/cpg/graph/declarations/Declaration")) } func (m *IfStatement) SetThenStatement(s *Statement) { @@ -200,5 +200,5 @@ func (fw *ForStatement) SetIterationStatement(s *Statement) { } func (r *ReturnStatement) SetReturnValue(e *Expression) { - (*jnigi.ObjectRef)(r).CallMethod(env, "setReturnValue", jnigi.Void, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) + (*jnigi.ObjectRef)(r).CallMethod(env, "setReturnValue", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression")) } diff --git a/cpg-language-go/src/main/golang/types.go b/cpg-language-go/src/main/golang/types.go index c15fa9b39d..44d0af77ab 100644 --- a/cpg-language-go/src/main/golang/types.go +++ b/cpg-language-go/src/main/golang/types.go @@ -37,7 +37,35 @@ import ( var env *jnigi.Env type Type jnigi.ObjectRef + +func (t *Type) ConvertToGo(o *jnigi.ObjectRef) error { + *t = (Type)(*o) + return nil +} + +func (*Type) GetClassName() string { + return "de/fraunhofer/aisec/cpg/graph/types/Type" +} + +func (*Type) IsArray() bool { + return false +} + type ObjectType jnigi.ObjectRef + +func (t *ObjectType) ConvertToGo(o *jnigi.ObjectRef) error { + *t = (ObjectType)(*o) + return nil +} + +func (*ObjectType) GetClassName() string { + return "de/fraunhofer/aisec/cpg/graph/types/ObjectType" +} + +func (*ObjectType) IsArray() bool { + return false +} + type HasType jnigi.ObjectRef func InitEnv(e *jnigi.Env) { @@ -45,62 +73,69 @@ func InitEnv(e *jnigi.Env) { } func TypeParser_createFrom(s string, resolveAlias bool) *Type { - t, err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/types/TypeParser", "createFrom", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/Type"), NewString(s), resolveAlias) + var t Type + err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/types/TypeParser", "createFrom", &t, NewString(s), resolveAlias) if err != nil { log.Fatal(err) } - return (*Type)(t.(*jnigi.ObjectRef)) + return &t } func UnknownType_getUnknown() *Type { - t, err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/types/UnknownType", "getUnknownType", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/UnknownType")) + var t Type + err := env.CallStaticMethod("de/fraunhofer/aisec/cpg/graph/types/UnknownType", "getUnknownType", &t) if err != nil { log.Fatal(err) } - return (*Type)(t.(*jnigi.ObjectRef)) + return &t } func (h *Type) GetRoot() *Type { - o, err := (*jnigi.ObjectRef)(h).CallMethod(env, "getRoot", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/Type")) + var t Type + err := (*jnigi.ObjectRef)(h).CallMethod(env, "getRoot", &t) if err != nil { log.Fatal(err) - } - return (*Type)(o.(*jnigi.ObjectRef)) + return &t } func (t *Type) Reference(o *jnigi.ObjectRef) *Type { - i, err := (*jnigi.ObjectRef)(t).CallMethod(env, "reference", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/Type"), (*jnigi.ObjectRef)(o).Cast("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin")) + var refType Type + err := (*jnigi.ObjectRef)(t).CallMethod(env, "reference", &refType, (*jnigi.ObjectRef)(o).Cast("de/fraunhofer/aisec/cpg/graph/types/PointerType$PointerOrigin")) if err != nil { log.Fatal(err) } - return (*Type)(i.(*jnigi.ObjectRef)) + return &refType } func (h *HasType) SetType(t *Type) { if t != nil { - (*jnigi.ObjectRef)(h).CallMethod(env, "setType", jnigi.Void, (*jnigi.ObjectRef)(t).Cast("de/fraunhofer/aisec/cpg/graph/types/Type")) + (*jnigi.ObjectRef)(h).CallMethod(env, "setType", nil, (*jnigi.ObjectRef)(t).Cast("de/fraunhofer/aisec/cpg/graph/types/Type")) } } func (h *HasType) GetType() *Type { - i, err := (*jnigi.ObjectRef)(h).CallMethod(env, "getType", jnigi.ObjectType("de/fraunhofer/aisec/cpg/graph/types/Type")) + var t Type + err := (*jnigi.ObjectRef)(h).CallMethod(env, "getType", &t) if err != nil { log.Fatal(err) } - return (*Type)(i.(*jnigi.ObjectRef)) + return &t } func (t *ObjectType) AddGeneric(g *Type) { - _, err := (*jnigi.ObjectRef)(t).Cast("de/fraunhofer/aisec/cpg/graph/types/ObjectType").CallMethod(env, "addGeneric", jnigi.Void, (*jnigi.ObjectRef)(g).Cast("de/fraunhofer/aisec/cpg/graph/types/Type")) + // Stupid workaround, since casting does not work. See + // https://github.com/timob/jnigi/issues/60 + var objType = jnigi.WrapJObject(uintptr((*jnigi.ObjectRef)(t).JObject()), "de/fraunhofer/aisec/cpg/graph/types/ObjectType", false) + err := objType.CallMethod(env, "addGeneric", nil, (*jnigi.ObjectRef)(g).Cast("de/fraunhofer/aisec/cpg/graph/types/Type")) if err != nil { log.Fatal(err) }