Skip to content

Commit

Permalink
Add module reading and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Rasquier committed Dec 13, 2017
1 parent 3cca9a9 commit d16802a
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 9 deletions.
File renamed without changes.
94 changes: 93 additions & 1 deletion asm/class-reader.go → asm/classreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,99 @@ func (c ClassReader) AcceptB(classVisitor ClassVisitor, attributePrototypes []At
// ----------------------------------------------------------------------------------------------

func (c ClassReader) readModule(classVisitor ClassVisitor, context *Context, moduleOffset int, modulePackagesOffset int, moduleMainClass string) {
//TODO
buffer := context.charBuffer
currentOffset := moduleOffset
moduleName := c.readModuleB(currentOffset, buffer)
moduleFlags := c.readUnsignedShort(currentOffset + 2)
moduleVersion := c.readUTF8(currentOffset+4, buffer)
currentOffset += 6
moduleVisitor := classVisitor.VisitModule(moduleName, moduleFlags, moduleVersion)
if moduleVisitor == nil {
return
}

if modulePackagesOffset != 0 {
packageCount := c.readUnsignedShort(modulePackagesOffset)
currentPackageOffset := modulePackagesOffset + 2
for packageCount > 0 {
moduleVisitor.VisitPackage(c.readPackage(currentPackageOffset, buffer))
currentPackageOffset += 2
packageCount--
}
}

requiresCount := c.readUnsignedShort(currentOffset)
currentOffset += 2
for requiresCount > 0 {
requires := c.readModuleB(currentOffset, buffer)
requiresFlags := c.readUnsignedShort(currentOffset + 2)
requiresVersion := c.readUTF8(currentOffset+4, buffer)
currentOffset += 6
moduleVisitor.VisitRequire(requires, requiresFlags, requiresVersion)
requiresCount--
}

exportsCount := c.readUnsignedShort(currentOffset)
currentOffset += 2
for exportsCount > 0 {
exports := c.readPackage(currentOffset, buffer)
exportsFlags := c.readUnsignedShort(currentOffset + 2)
exportsToCount := c.readUnsignedShort(currentOffset + 4)
currentOffset += 6
var exportsTo []string
if exportsToCount != 0 {
exportsTo = make([]string, exportsToCount)
for i := 0; i < exportsToCount; i++ {
exportsTo[i] = c.readModuleB(currentOffset, buffer)
currentOffset += 2
}
}
moduleVisitor.VisitExport(exports, exportsFlags, exportsTo...)
exportsCount--
}

opensCount := c.readUnsignedShort(currentOffset)
currentOffset += 2
for opensCount > 0 {
opens := c.readPackage(currentOffset, buffer)
opensFlags := c.readUnsignedShort(currentOffset + 2)
opensToCount := c.readUnsignedShort(currentOffset + 4)
currentOffset += 6
var opensTo []string
if opensToCount != 0 {
opensTo = make([]string, opensToCount)
for i := 0; i < opensToCount; i++ {
opensTo[i] = c.readModuleB(currentOffset, buffer)
currentOffset += 2
}
}
moduleVisitor.VisitOpen(opens, opensFlags, opensTo...)
}

usesCount := c.readUnsignedShort(currentOffset)
currentOffset += 2
for usesCount > 0 {
moduleVisitor.VisitUse(c.readClass(currentOffset, buffer))
currentOffset += 2
usesCount--
}

providesCount := c.readUnsignedShort(currentOffset)
currentOffset += 2
for providesCount > 0 {
provides := c.readClass(currentOffset, buffer)
providesWithCount := c.readUnsignedShort(currentOffset + 2)
currentOffset += 4
providesWith := make([]string, providesWithCount)
for i := 0; i < providesWithCount; i++ {
providesWith[i] = c.readClass(currentOffset, buffer)
currentOffset += 2
}
moduleVisitor.VisitProvide(provides, providesWith...)
providesCount--
}

moduleVisitor.VisitEnd()
}

func (c ClassReader) readField(classVisitor ClassVisitor, context *Context, fieldInfoOffset int) int {
Expand Down
4 changes: 2 additions & 2 deletions asm/class-visitor.go → asm/classvisitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package asm
type ClassVisitor interface {
Visit(version, access int, name, signature, superName string, interfaces []string)
VisitSource(source, debug string)
VisitModule(name string, access, version int) //should return modulevisitor
VisitModule(name string, access int, version string) ModuleVisitor
VisitOuterClass(owner, name, descriptor string)
VisitAnnotation(descriptor string, visible bool) AnnotationVisitor
VisitTypeAnnotation(typeRef, typePath int, descriptor string, visible bool) AnnotationVisitor //typePath : TypePath
VisitAttribute(attribute *Attribute)
VisitInnerClass(name, outerName, innerName string, access int)
VisitField(access int, name, descriptor, signature string, value interface{}) //should return FieldVisitor
VisitField(access int, name, descriptor, signature string, value interface{}) FieldVisitor
VisitMethod(access int, name, descriptor, signature string, exceptions []string) MethodVisitor
VisitEnd()
}
11 changes: 11 additions & 0 deletions asm/fieldvisitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asm

// FieldVisitor a visitor to visit a Java field. The methods of this class must be called in the following order:
// ( <tt>visitAnnotation</tt> | <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )*
// <tt>visitEnd</tt>.
type FieldVisitor interface {
VisitAnnotation(descriptor string, visible bool) AnnotationVisitor
VisitTypeAnnotation(typeRef int, typePath interface{}, descriptor string, visible bool) AnnotationVisitor //TypePath
VisitAttribute(attribute *Attribute)
VisitEnd()
}
File renamed without changes.
16 changes: 16 additions & 0 deletions asm/modulevisitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package asm

// ModuleVisitor a visitor to visit a Java module. The methods of this class must be called in the following
// order: <tt>visitMainClass</tt> | ( <tt>visitPackage</tt> | <tt>visitRequire</tt> |
// <tt>visitExport</tt> | <tt>visitOpen</tt> | <tt>visitUse</tt> | <tt>visitProvide</tt> )*
// <tt>visitEnd</tt>.
type ModuleVisitor interface {
VisitMainClass(mainClass string)
VisitPackage(packaze string)
VisitRequire(module string, access int, version string)
VisitExport(packaze string, access int, modules ...string)
VisitOpen(packaze string, access int, modules ...string)
VisitUse(service string)
VisitProvide(service string, providers ...string)
VisitEnd()
}
File renamed without changes.
8 changes: 4 additions & 4 deletions event-visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (e EventVisitor) VisitSource(source, debug string) {

}

func (e EventVisitor) VisitModule(name string, access, version int) {

func (e EventVisitor) VisitModule(name string, access int, version string) asm.ModuleVisitor {
return nil
}

func (e EventVisitor) VisitOuterClass(owner, name, descriptor string) {
Expand All @@ -43,8 +43,8 @@ func (e EventVisitor) VisitInnerClass(name, outerName, innerName string, access

}

func (e EventVisitor) VisitField(access int, name, descriptor, signature string, value interface{}) {

func (e EventVisitor) VisitField(access int, name, descriptor, signature string, value interface{}) asm.FieldVisitor {
return nil
}

func (e EventVisitor) VisitMethod(access int, name, descriptor, signature string, exceptions []string) asm.MethodVisitor {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func main() {
os.Exit(1)
}

r, err := asm.NewClassReader(bytes)
reader, err := asm.NewClassReader(bytes)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

r.Accept(&EventVisitor{
reader.Accept(&EventVisitor{
OnVisit: []func(version, access int, name, signature, superName string, interfaces []string){
func(version, access int, name, signature, superName string, interfaces []string) {
fmt.Println(name, signature, superName, interfaces)
Expand Down

0 comments on commit d16802a

Please sign in to comment.