forked from paulirwin/JavaToCSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeHelper.cs
More file actions
128 lines (114 loc) · 3.68 KB
/
Copy pathTypeHelper.cs
File metadata and controls
128 lines (114 loc) · 3.68 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
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
using com.github.javaparser.ast.type;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
namespace JavaToCSharp
{
public static class TypeHelper
{
public static string ConvertType(string typeName)
{
switch (typeName)
{
case "RuntimeException":
case "Error":
return "Exception";
case "Integer":
return "int";
case "boolean":
case "Boolean":
return "bool";
case "String":
return "string";
case "UnsupportedOperationException":
return "NotSupportedException";
case "IllegalArgumentException":
return "ArgumentException";
case "ICloseable":
return "IDisposable";
case "AlreadyClosedException":
return "ObjectDisposedException";
case "IllegalStateException":
return "InvalidOperationException";
default:
return typeName;
}
}
public static string Capitalize(string name)
{
if (string.IsNullOrEmpty(name))
return name;
var parts = name.Split('.');
var joined = string.Join(".", parts.Select(i =>
{
if (i.Length == 1)
return i.ToUpper();
else
return i[0].ToString().ToUpper() + i.Substring(1);
}));
return joined;
}
public static string ConvertIdentifierName(string name)
{
switch (name)
{
case "string":
case "ref":
case "object":
case "int":
case "short":
case "float":
case "long":
case "double":
case "in":
case "out":
case "byte":
case "class":
case "delegate":
case "params":
case "is":
case "as":
case "base":
case "namespace":
return "@" + name;
default:
return name;
}
}
public static string ReplaceCommonMethodNames(string name)
{
switch (name.ToLower())
{
case "hashcode":
return "GetHashCode";
case "getclass":
return "GetType";
default:
return name;
}
}
public static TypeSyntax GetSyntaxFromType(ClassOrInterfaceType type, bool addI = false)
{
string typeName = type.getName();
if (addI)
typeName = "I" + typeName;
typeName = ConvertType(typeName);
var typeArgs = type.getTypeArgs().ToList<com.github.javaparser.ast.type.Type>();
TypeSyntax typeSyntax;
if (typeArgs != null && typeArgs.Count > 0)
{
typeSyntax = SyntaxFactory.GenericName(typeName)
.AddTypeArgumentListArguments(typeArgs.Select(i => SyntaxFactory.ParseTypeName(i.toString())).ToArray());
}
else
{
typeSyntax = SyntaxFactory.ParseTypeName(typeName);
}
return typeSyntax;
}
}
}