-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathresolve.go
More file actions
78 lines (63 loc) · 2.35 KB
/
resolve.go
File metadata and controls
78 lines (63 loc) · 2.35 KB
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
package main
import (
"strconv"
"github.com/NickyBoy89/java2go/parsing"
"github.com/NickyBoy89/java2go/symbol"
)
func ResolveFile(file parsing.SourceFile) {
ResolveClass(file.Symbols.BaseClass, file)
for _, subclass := range file.Symbols.BaseClass.Subclasses {
ResolveClass(subclass, file)
}
}
func ResolveClass(class *symbol.ClassScope, file parsing.SourceFile) {
// Resolve all the fields in that respective class
for _, field := range class.Fields {
// Since a private global variable is able to be accessed in the package, it must be renamed
// to avoid conflicts with other global variables
packageScope := symbol.GlobalScope.FindPackage(file.Symbols.Package)
symbol.ResolveDefinition(field, file.Symbols)
// Rename the field if its name conflits with any keyword
for i := 0; symbol.IsReserved(field.Name) ||
len(packageScope.ExcludeFile(class.Class.Name).FindStaticField().ByName(field.Name)) > 0; i++ {
field.Rename(field.Name + strconv.Itoa(i))
}
}
// Resolve all the methods
for _, method := range class.Methods {
// Resolve the return type, as well as the body of the method
symbol.ResolveChildren(method, file.Symbols)
// Comparison compares the method against the found method
// This tests for a method of the same name, but with different
// aspects of it, so that it can be identified as a duplicate
comparison := func(d *symbol.Definition) bool {
// The names must match, but everything else must be different
if method.Name != d.Name {
return false
}
// Size of parameters do not match
if len(method.Parameters) != len(d.Parameters) {
return true
}
// Go through the types and check to see if they differ
for index, param := range method.Parameters {
if param.OriginalType != d.Parameters[index].OriginalType {
return true
}
}
// Both methods are equal, skip this method since it is likely
// the same method that we are trying to find duplicates of
return false
}
for i := 0; symbol.IsReserved(method.Name) || len(class.FindMethod().By(comparison)) > 0; i++ {
method.Rename(method.Name + strconv.Itoa(i))
}
// Resolve all the paramters of the method
for _, param := range method.Parameters {
symbol.ResolveDefinition(param, file.Symbols)
for i := 0; symbol.IsReserved(param.Name); i++ {
param.Rename(param.Name + strconv.Itoa(i))
}
}
}
}