- private def imports() '''
- «IF !importMap.empty»
- «FOR entry : importMap.entrySet»
- «IF entry.value != getRootPackageName(module.QNameModule)»
- import «entry.value».«entry.key»;
- «ENDIF»
- «ENDFOR»
- «ENDIF»
- '''
-
- final protected def importedName(Class<?> cls) {
- val Type intype = Types.typeForClass(cls)
- putTypeIntoImports(intype);
- getExplicitType(intype)
- }
-
- final def void putTypeIntoImports(Type type) {
- val String typeName = type.getName();
- val String typePackageName = type.getPackageName();
- if (typePackageName.startsWith("java.lang") || typePackageName.isEmpty()) {
- return;
- }
- if (!importMap.containsKey(typeName)) {
- importMap.put(typeName, typePackageName);
- }
- if (type instanceof ParameterizedType) {
- val Type[] params = type.getActualTypeArguments()
- if (params !== null) {
- for (Type param : params) {
- putTypeIntoImports(param);
- }
- }
- }
- }
-
- final def String getExplicitType(Type type) {
- val String typePackageName = type.getPackageName();
- val String typeName = type.getName();
- val String importedPackageName = importMap.get(typeName);
- var StringBuilder builder;
- if (typePackageName.equals(importedPackageName)) {
- builder = new StringBuilder(type.getName());
- if (builder.toString().equals("Void")) {
- return "void";
- }
- addActualTypeParameters(builder, type);
- } else {
- if (type.equals(Types.voidType())) {
- return "void";
- }
- builder = new StringBuilder();
- if (!typePackageName.isEmpty()) {
- builder.append(typePackageName + Constants.DOT + type.getName());
- } else {
- builder.append(type.getName());
- }
- addActualTypeParameters(builder, type);
- }
- return builder.toString();
- }
-
- final def StringBuilder addActualTypeParameters(StringBuilder builder, Type type) {
- if (type instanceof ParameterizedType) {
- val Type[] pTypes = type.getActualTypeArguments();
- builder.append('<');
- builder.append(getParameters(pTypes));
- builder.append('>');
- }
- return builder;
- }
-
- final def String getParameters(Type[] pTypes) {
- if (pTypes === null || pTypes.length == 0) {
- return "?";
- }
- val StringBuilder builder = new StringBuilder();
-
- var int i = 0;
- for (pType : pTypes) {
- val Type t = pTypes.get(i)