2 # Copyright 2012-2015 Bronto Software, Inc. and contributors
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 Convert Java syntax tree nodes to string representations.
24 from .util import StringBuilder
26 # The order for displaying modifiers
27 __modifiers_order = ('public', 'protected', 'private', 'static', 'abstract', 'final',
28 'native', 'synchronized', 'transient', 'volatile', 'strictfp')
31 def _f(node, output=None, **kwargs):
33 output = StringBuilder()
35 f(node, output, **kwargs)
39 def output_list(f, items, output=None, sep=', '):
42 for item in items[1:]:
47 def output_annotation(annotation, output):
49 output.append(annotation.name)
53 def output_type(type, output, with_generics=True):
59 dim = '[]' * len(type.dimensions)
63 if isinstance(type, javalang.tree.BasicType):
64 output.append(type.name)
67 output.append(type.name)
70 output_type_args(type.arguments, output)
79 def output_exception(exception, output):
80 output.append(exception)
83 def output_type_arg(type_arg, output):
84 if type_arg.pattern_type == '?':
87 if type_arg.pattern_type:
89 output.append(type_arg.pattern_type)
92 output_type(type_arg.type, output)
95 def output_type_args(type_args, output):
98 output_list(output_type_arg, type_args, output, ', ')
102 def output_type_param(type_param, output):
103 output.append(type_param.name)
105 if type_param.extends:
106 output.append(' extends ')
107 output_list(output_type, type_param.extends, output, ' & ')
110 def output_type_params(type_params, output):
113 output_list(output_type_param, type_params, output, ', ')
117 def output_declaration(declaration, output):
118 for annotation in declaration.annotations:
119 output_annotation(annotation, output)
121 output_modifiers(declaration.modifiers, output)
124 if isinstance(declaration, javalang.tree.ClassDeclaration):
125 output.append('class ')
126 elif isinstance(declaration, javalang.tree.EnumDeclaration):
127 output.append('enum ')
128 elif isinstance(declaration, javalang.tree.InterfaceDeclaration):
129 output.append('interface ')
130 elif isinstance(declaration, javalang.tree.AnnotationDeclaration):
131 output.append('@interface ')
133 output.append(declaration.name)
135 if isinstance(declaration, (javalang.tree.ClassDeclaration, javalang.tree.InterfaceDeclaration)):
136 output_type_params(declaration.type_parameters, output)
138 if isinstance(declaration, javalang.tree.ClassDeclaration) and declaration.extends:
139 output.append(' extends ')
140 output_type(declaration.extends, output)
142 if isinstance(declaration, javalang.tree.InterfaceDeclaration) and declaration.extends:
143 output.append(' extends ')
144 output_list(output_type, declaration.extends, output, ', ')
146 if isinstance(declaration, (javalang.tree.ClassDeclaration, javalang.tree.EnumDeclaration)) and declaration.implements:
147 output.append(' implements ')
148 output_list(output_type, declaration.implements, output, ', ')
151 def output_formal_param(param, output):
152 output_type(param.type, output)
158 output.append(param.name)
161 def output_modifiers(modifiers, output):
162 ordered_modifiers = [mod for mod in __modifiers_order if mod in modifiers]
163 output_list(lambda mod, output: output.append(mod), ordered_modifiers, output, ' ')