forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava9builder.py
More file actions
130 lines (111 loc) · 5.28 KB
/
java9builder.py
File metadata and controls
130 lines (111 loc) · 5.28 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
129
130
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from inspect import cleandoc
from java.nio.charset import StandardCharsets
from java.util import Locale
from javax.lang.model.element.Modifier import PUBLIC
from javax.lang.model.util import ElementFilter
from javax.lang.model.type import TypeKind
from javax.tools import DocumentationTool, ToolProvider
from robot.utils import normalize, printable_name, split_tags_from_doc
from .model import LibraryDoc, KeywordDoc
class JavaDocBuilder(object):
def build(self, path):
qualified_name, type_element, fields, constructors, methods, elements \
= self._get_documentation_data(path)
libdoc = LibraryDoc(name=qualified_name,
doc=self._get_doc(elements, type_element),
version=self._get_version(fields),
scope=self._get_scope(fields),
named_args=False,
doc_format=self._get_doc_format(fields))
libdoc.inits = self._initializers(elements, constructors)
libdoc.keywords = self._keywords(elements, methods)
return libdoc
def _get_doc(self, elements, element):
doc = elements.getDocComment(element)
return cleandoc(doc or '').rstrip()
def _get_version(self, fields):
return self._get_attr(fields, 'VERSION')
def _get_scope(self, fields):
scope = self._get_attr(fields, 'SCOPE', upper=True)
return {'TESTSUITE': 'test suite',
'GLOBAL': 'global'}.get(scope, 'test suite')
def _get_doc_format(self, fields):
return self._get_attr(fields, 'DOC_FORMAT', upper=True)
def _get_attr(self, fields, name, upper=False):
name = 'ROBOT_LIBRARY_' + name
for field in fields:
if field.getSimpleName().toString() == name:
value = field.getConstantValue()
if upper:
value = normalize(value, ignore='_').upper()
return value
return ''
def _initializers(self, elements, constructors):
inits = [self._keyword_doc(elements, constructor)
for constructor in constructors]
if len(inits) == 1 and not inits[0].args:
return []
return inits
def _keywords(self, elements, methods):
return [self._keyword_doc(elements, method) for method in methods]
def _keyword_doc(self, elements, method):
doc, tags = split_tags_from_doc(self._get_doc(elements, method))
return KeywordDoc(
name=printable_name(method.getSimpleName().toString(),
code_style=True),
args=self._get_keyword_arguments(method),
doc=doc,
tags=tags
)
def _get_keyword_arguments(self, method):
params = method.getParameters()
if not params:
return []
names = [param.getSimpleName().toString() for param in params]
if self._is_varargs(params[-1]):
names[-1] = '*' + names[-1]
elif self._is_kwargs(params[-1]):
names[-1] = '**' + names[-1]
if len(params) > 1 and self._is_varargs(params[-2]):
names[-2] = '*' + names[-2]
return names
def _is_varargs(self, param):
param_type = param.asType()
return (param_type.toString().startswith('java.util.List') or
(param_type.getKind() == TypeKind.ARRAY and
param_type.getComponentType().getKind() != TypeKind.ARRAY))
def _is_kwargs(self, param):
return param.asType().toString().startswith('java.util.Map')
def _get_documentation_data(self, path):
doc_tool = ToolProvider.getSystemDocumentationTool()
file_manager = DocumentationTool.getStandardFileManager(
doc_tool, None, Locale.US, StandardCharsets.UTF_8)
compiler = ToolProvider.getSystemJavaCompiler()
source = file_manager.getJavaFileObjectsFromStrings([path])
task = compiler.getTask(None, None, None, None, None, source)
type_element = task.analyze().iterator().next()
elements = task.getElements()
members = elements.getAllMembers(type_element)
qf_name = type_element.getQualifiedName().toString()
fields = [f for f in ElementFilter.fieldsIn(members)
if PUBLIC in f.getModifiers()]
constructors = [c for c in ElementFilter.constructorsIn(members)
if PUBLIC in c.getModifiers()]
methods = [m for m in ElementFilter.methodsIn(members)
if m.getEnclosingElement() is type_element and
PUBLIC in m.getModifiers()]
return qf_name, type_element, fields, constructors, methods, elements