Robot Framework Integrated Development Environment (RIDE)
javabuilder.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from inspect import cleandoc
17 
18 from robotide.lib.robot.errors import DataError
19 from robotide.lib.robot.utils import (JAVA_VERSION, normalize, split_tags_from_doc,
20  printable_name)
21 
22 from .model import LibraryDoc, KeywordDoc
23 
24 
26 
27  def build(self, path):
28  doc = ClassDoc(path)
29  libdoc = LibraryDoc(name=doc.qualifiedName(),
30  doc=self._get_doc_get_doc(doc),
31  version=self._get_version_get_version(doc),
32  scope=self._get_scope_get_scope(doc),
33  named_args=False,
34  doc_format=self._get_doc_format_get_doc_format(doc))
35  libdoc.inits = self._initializers_initializers(doc)
36  libdoc.keywords = self._keywords_keywords(doc)
37  return libdoc
38 
39  def _get_doc(self, doc):
40  text = doc.getRawCommentText()
41  return cleandoc(text).rstrip()
42 
43  def _get_version(self, doc):
44  return self._get_attr_get_attr(doc, 'VERSION')
45 
46  def _get_scope(self, doc):
47  scope = self._get_attr_get_attr(doc, 'SCOPE', upper=True)
48  return {'TESTSUITE': 'test suite',
49  'GLOBAL': 'global'}.get(scope, 'test suite')
50 
51  def _get_doc_format(self, doc):
52  return self._get_attr_get_attr(doc, 'DOC_FORMAT', upper=True)
53 
54  def _get_attr(self, doc, name, upper=False):
55  name = 'ROBOT_LIBRARY_' + name
56  for field in doc.fields():
57  if field.name() == name and field.isPublic():
58  value = field.constantValue()
59  if upper:
60  value = normalize(value, ignore='_').upper()
61  return value
62  return ''
63 
64  def _initializers(self, doc):
65  inits = [self._keyword_doc_keyword_doc(init) for init in doc.constructors()]
66  if len(inits) == 1 and not inits[0].args:
67  return []
68  return inits
69 
70  def _keywords(self, doc):
71  return [self._keyword_doc_keyword_doc(m) for m in doc.methods()]
72 
73  def _keyword_doc(self, method):
74  doc, tags = split_tags_from_doc(self._get_doc_get_doc(method))
75  return KeywordDoc(
76  name=printable_name(method.name(), code_style=True),
77  args=self._get_keyword_arguments_get_keyword_arguments(method),
78  doc=doc,
79  tags=tags
80  )
81 
82  def _get_keyword_arguments(self, method):
83  params = method.parameters()
84  if not params:
85  return []
86  names = [p.name() for p in params]
87  if self._is_varargs_is_varargs(params[-1]):
88  names[-1] = '*' + names[-1]
89  elif self._is_kwargs_is_kwargs(params[-1]):
90  names[-1] = '**' + names[-1]
91  if len(params) > 1 and self._is_varargs_is_varargs(params[-2]):
92  names[-2] = '*' + names[-2]
93  return names
94 
95  def _is_varargs(self, param):
96  return (param.typeName().startswith('java.util.List')
97  or param.type().dimension() == '[]')
98 
99  def _is_kwargs(self, param):
100  return param.typeName().startswith('java.util.Map')
101 
102 
103 
109 def ClassDoc(path):
110  try:
111  from com.sun.tools.javadoc import JavadocTool, Messager, ModifierFilter
112  from com.sun.tools.javac.util import List, Context
113  from com.sun.tools.javac.code.Flags import PUBLIC
114  except ImportError:
115  raise DataError("Creating documentation from Java source files "
116  "requires 'tools.jar' to be in CLASSPATH.")
117  context = Context()
118  Messager.preRegister(context, 'libdoc')
119  jdoctool = JavadocTool.make0(context)
120  filter = ModifierFilter(PUBLIC)
121  java_names = List.of(path)
122  if JAVA_VERSION < (1, 8): # API changed in Java 8
123  root = jdoctool.getRootDocImpl('en', 'utf-8', filter, java_names,
124  List.nil(), False, List.nil(),
125  List.nil(), False, False, True)
126  else:
127  root = jdoctool.getRootDocImpl('en', 'utf-8', filter, java_names,
128  List.nil(), List.nil(), False, List.nil(),
129  List.nil(), False, False, True)
130  return root.classes()[0]
Used when variable does not exist.
Definition: errors.py:67
def _get_attr(self, doc, name, upper=False)
Definition: javabuilder.py:54
def ClassDoc(path)
Process the given Java source file and return ClassDoc instance.
Definition: javabuilder.py:109
def printable_name(string, code_style=False)
Generates and returns printable name from the given string.
Definition: misc.py:76
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:30
def split_tags_from_doc(doc)
Definition: text.py:158