/
javaimport.go
247 lines (207 loc) · 4.75 KB
/
javaimport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"archive/zip"
"flag"
"fmt"
"github.com/k0kubun/pp"
jireg "github.com/kamichidu/go-javaimport/regexp"
"github.com/kamichidu/go-jclass"
"log"
"os"
"path/filepath"
"regexp"
"runtime/pprof"
"strings"
"sync"
"time"
)
type stringSet struct {
set []string
}
func (self *stringSet) Set(arg string) error {
for _, pkg := range strings.Split(arg, ",") {
self.set = append(self.set, strings.Trim(pkg, " ."))
}
return nil
}
func (self *stringSet) String() string {
return "[]"
}
var _ flag.Value = (*stringSet)(nil)
var (
debug = flag.Bool("debug", false, "DEVELOPMENT OPTION")
profile = flag.Bool("profile", false, "DEVELOPMENT OPTION")
current = flag.String("p", "", "Current package that you're in.")
excludes = &stringSet{}
includes = &stringSet{}
logger *log.Logger = nil
)
func init() {
flag.Var(excludes, "e", "Packages will be excluded.")
flag.Var(includes, "i", "Packages will be included.")
pp.ColoringEnabled = false
logger = log.New(os.Stderr, "", log.Lshortfile|log.LstdFlags)
}
type PathFilter struct {
excludePattern *regexp.Regexp
includePattern *regexp.Regexp
}
func newPathFilter(excludes []string, includes []string) *PathFilter {
f := &PathFilter{}
// ignore always
ex := jireg.NewTrieOptimizer()
for _, exclude := range excludes {
prefix := strings.Replace(exclude, ".", "/", -1) + "/"
if prefix != "/" {
ex.Add(prefix)
}
}
if *debug {
fmt.Fprintf(os.Stderr, "exclude pattern: %s\n", ex.Re())
}
f.excludePattern, _ = regexp.Compile("^" + ex.Re() + "|\\$\\d+\\.class$")
in := jireg.NewTrieOptimizer()
for _, include := range includes {
prefix := strings.Replace(include, ".", "/", -1) + "/"
if prefix != "/" {
in.Add(prefix)
}
}
if *debug {
fmt.Fprintf(os.Stderr, "include pattern: %s\n", in.Re())
}
f.includePattern, _ = in.Compile()
return f
}
func (self *PathFilter) Apply(path string) bool {
if self.excludePattern != nil && self.excludePattern.MatchString(path) {
if self.includePattern != nil && self.includePattern.MatchString(path) {
return true
} else {
return false
}
}
return true
}
func ParseWithLfs(root string, filename string) (*jclass.JClass, error) {
abspath := filepath.Join(root, filename)
r, err := os.Open(abspath)
if err != nil {
return nil, err
}
defer r.Close()
return jclass.NewJClass(r)
}
func ParseWithJar(filename string, zf *zip.File) (*jclass.JClass, error) {
r, err := zf.Open()
if err != nil {
return nil, err
}
defer r.Close()
return jclass.NewJClass(r)
}
func write(jc *jclass.JClass) {
if *current == jc.GetPackageName() {
return
}
if !jc.IsPublic() {
return
}
fmt.Println(jc.GetCanonicalName())
}
func runWithLfs(path string, filter *PathFilter) {
filepath.Walk(path, func(filename string, info os.FileInfo, err error) error {
if filepath.Ext(filename) != ".class" {
return nil
}
relpath, err := filepath.Rel(path, filename)
if err != nil {
panic(err)
}
relpath = strings.Replace(relpath, "\\", "/", -1)
if !filter.Apply(relpath) {
return nil
}
typeInfo, err := ParseWithLfs(path, relpath)
if err != nil {
panic(err)
}
write(typeInfo)
return nil
})
}
func runWithJar(filename string, filter *PathFilter) {
zr, err := zip.OpenReader(filename)
if err != nil {
panic(err)
}
defer zr.Close()
for _, zf := range zr.File {
if filepath.Ext(zf.Name) != ".class" {
continue
}
if !filter.Apply(zf.Name) {
continue
}
typeInfo, err := ParseWithJar(filename, zf)
if err != nil {
panic(err)
}
write(typeInfo)
}
}
func javaimportMain() int {
if *profile {
f, err := os.Create("pprof.out")
if err != nil {
panic(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
}
flag.Parse()
filter := newPathFilter(excludes.set, includes.set)
if *debug {
fmt.Fprintf(os.Stderr, "Exclude pattern: %s\n", filter.excludePattern.String())
fmt.Fprintf(os.Stderr, "Include pattern: %s\n", filter.includePattern.String())
}
allStart := time.Now()
var wg sync.WaitGroup
for _, path := range flag.Args() {
path, err := filepath.Abs(path)
if err != nil {
panic(err)
}
wg.Add(1)
go func(path string) {
defer wg.Done()
if *debug {
fmt.Fprintf(os.Stderr, "Start %s\n", path)
}
start := time.Now()
switch filepath.Ext(path) {
case ".zip":
fallthrough
case ".jar":
runWithJar(path, filter)
default:
runWithLfs(path, filter)
}
duration := time.Now().Sub(start)
if *debug {
fmt.Fprintf(os.Stderr, "Parsing %s requires %.09f [s]\n", path, duration.Seconds())
}
}(path)
}
wg.Wait()
allDuration := time.Now().Sub(allStart)
if *debug {
fmt.Fprintf(os.Stderr, "Time requires %.09f [s]\n", allDuration.Seconds())
}
return 0
}
func main() {
os.Exit(javaimportMain())
}