/** Constructor. * * @param importManager the import manager. */ public ExtraLanguageAppendable(ImportManager importManager) { super(false); ImportManager im = importManager; if (im == null) { im = new ImportManager(true); } this.importManager = im; }
public List<String> getImports() { return importManager.getImports(); }
@Override protected void appendType(final Class<?> type, StringBuilder builder) { importManager.appendType(type, builder); }
public boolean addImportFor(JvmType type) { final String qualifiedName = type.getQualifiedName(getInnerTypeSeparator()); final String simpleName = type.getSimpleName(); final Map<String, String> imps = internalGetImports(); if (!allowsSimpleName(qualifiedName, simpleName) && !needsQualifiedName(qualifiedName, simpleName) && !imps.containsKey(simpleName)) { imps.put(simpleName, qualifiedName); return true; } return false; }
protected boolean needsQualifiedName(String qualifiedName, String simpleName) { return !organizeImports || (getThisTypeSimpleNames().contains(simpleName) && !getThisTypeQualifiedNames().contains(qualifiedName)) || CodeGenUtil2.isJavaLangType(simpleName); }
protected void appendType(final String qualifiedName, final String shortName, final String outerShortName, final String namespaceImport, StringBuilder builder) { if (allowsSimpleName(namespaceImport, shortName)) { builder.append(shortName); } else if (needsQualifiedName(namespaceImport, shortName)) { builder.append(qualifiedName); } else { final Map<String, String> imps = internalGetImports(); if (imps.containsKey(outerShortName)) { if (namespaceImport.equals(imps.get(outerShortName))) { builder.append(shortName); } else { builder.append(qualifiedName); } } else { imps.put(outerShortName, namespaceImport); builder.append(shortName); } } }
ImportManager concreteImports = new ImportManager(true); XImportSection importSection = getScript().getImportSection(); if (importSection != null) { for (XImportDeclaration decl : importSection.getImportDeclarations()) { concreteImports.addImportFor(decl.getImportedType()); for (String importName : getImportManager().getImports()) { JvmType type = findType(getScript(), importName).getType(); if (concreteImports.addImportFor(type) && type instanceof JvmDeclaredType) { XImportDeclaration declaration = XtypeFactory.eINSTANCE.createXImportDeclaration(); declaration.setImportedType((JvmDeclaredType) type);
public void appendType(final Class<?> type, StringBuilder builder) { if (type.isPrimitive()) { builder.append(type.getSimpleName()); } else if (type.isArray()) { appendType(type.getComponentType(), builder); builder.append("[]"); } else { final String qualifiedName = type.getCanonicalName(); String nameToImport = qualifiedName; String shortName = type.getSimpleName(); String outerShortName = shortName; if (shouldUseQualifiedNestedName(qualifiedName)) { Class<?> outerContainer = type; while (outerContainer.getDeclaringClass() != null) { outerContainer = outerContainer.getDeclaringClass(); } if (type != outerContainer) { outerShortName = outerContainer.getSimpleName(); if(!getThisTypeQualifiedNames().contains(outerContainer.getCanonicalName()) && getThisTypeSimpleNames().contains(outerShortName)) { outerShortName = qualifiedName; shortName = qualifiedName; } else { nameToImport = outerContainer.getCanonicalName(); shortName = outerShortName + qualifiedName.substring(nameToImport.length()); } } } appendType(qualifiedName, shortName, outerShortName, nameToImport, builder); } }
@Override public void appendInlineAnnotation(JvmAnnotationTarget target, XtendExecutable source) { final ImportManager imports = new ImportManager(); final InlineAnnotationTreeAppendable result = newAppendable(imports); generate(source.getExpression(), null, source, result); final String content = result.getContent(); if (!Strings.isEmpty(content)) { final List<String> importedTypes = imports.getImports(); final JvmTypeReference[] importArray = new JvmTypeReference[importedTypes.size()]; for (int i = 0; i < importArray.length; ++i) { importArray[i] = this.typeReferences.getTypeForName(importedTypes.get(i), source); } appendInlineAnnotation(target, source.eResource().getResourceSet(), content, result.isConstant(), result.isStatement(), importArray); } }
private void markCapacityFunctions(PyAppendable it) { final Map<JvmOperation, String> mapping = this.useCapacityMapping; this.useCapacityMapping = new HashMap<>(); final ImportManager imports = it.getImportManager(); for (final Entry<JvmOperation, String> entry : mapping.entrySet()) { final JvmOperation operation = entry.getKey(); final JvmDeclaredType type = operation.getDeclaringType(); imports.addImportFor(type); it.declareVariable(operation, entry.getValue()); } }
protected void registerSimpleNamesOfInnerClasses(JvmDeclaredType thisType, LinkedHashSet<JvmType> handled) { if (!handled.add(thisType)) return; List<JvmDeclaredType> nested = EcoreUtil2.typeSelect(thisType.getMembers(), JvmDeclaredType.class); for (JvmDeclaredType jvmDeclaredType : nested) { getThisTypeSimpleNames().add(jvmDeclaredType.getSimpleName()); getThisTypeQualifiedNames().add(jvmDeclaredType.getQualifiedName(getInnerTypeSeparator())); thisCollidesWithJavaLang |= CodeGenUtil2.isJavaLangType(jvmDeclaredType.getSimpleName()); } for (JvmTypeReference superType: thisType.getSuperTypes()) { if (superType.getType() instanceof JvmDeclaredType) { registerSimpleNamesOfInnerClasses((JvmDeclaredType) superType.getType(), handled); } } }
public ImportManager(boolean organizeImports, JvmDeclaredType thisType, char innerTypeSeparator) { this.organizeImports = organizeImports; this.innerTypeSeparator = innerTypeSeparator; if (thisType != null) { getThisTypeSimpleNames().add(thisType.getSimpleName()); getThisTypeQualifiedNames().add(thisType.getQualifiedName(innerTypeSeparator)); thisCollidesWithJavaLang |= CodeGenUtil2.isJavaLangType(thisType.getSimpleName()); registerSimpleNamesOfInnerClasses(thisType, new LinkedHashSet<JvmType>()); } }
public void appendType(final JvmType type, StringBuilder builder) { if (type instanceof JvmPrimitiveType || type instanceof JvmVoid || type instanceof JvmTypeParameter) { builder.append(type.getQualifiedName(getInnerTypeSeparator())); } else if (type instanceof JvmArrayType) { appendType(((JvmArrayType) type).getComponentType(), builder); builder.append("[]"); } else { final String qualifiedName = type.getQualifiedName(getInnerTypeSeparator()); String nameToImport = qualifiedName; String shortName = type.getSimpleName(); String outerShortName = shortName; if (shouldUseQualifiedNestedName(qualifiedName)) { JvmType outerContainer = type; while (outerContainer.eContainer() instanceof JvmType) { outerContainer = (JvmType) outerContainer.eContainer(); } if (type != outerContainer) { outerShortName = outerContainer.getSimpleName(); if(!getThisTypeQualifiedNames().contains(outerContainer.getQualifiedName(getInnerTypeSeparator())) && getThisTypeSimpleNames().contains(outerShortName)) { outerShortName = qualifiedName; shortName = qualifiedName; } else { nameToImport = outerContainer.getQualifiedName(getInnerTypeSeparator()); shortName = outerShortName + qualifiedName.substring(nameToImport.length()); } } } appendType(qualifiedName, shortName, outerShortName, nameToImport, builder); } }
private static String getSignatureType(JvmType type, ImportManager importManager) { if (importManager != null) { importManager.addImportFor(type); return type.getSimpleName(); } return type.getIdentifier(); }
protected boolean allowsSimpleName(String qualifiedName, String simpleName) { return getThisTypeQualifiedNames().contains(qualifiedName) || ((!thisCollidesWithJavaLang || !getThisTypeSimpleNames().contains(simpleName)) && JAVA_LANG_PACK.matcher(qualifiedName).matches()) || equal(qualifiedName, simpleName); }
public StringBuilderBasedAppendable() { this(new ImportManager(false)); }
@Deprecated @Override public List<String> getImports() { return importManager.getImports(); }
/** Generate a constructor call. * * @param expr the call expression. */ public void generate(XConstructorCall expr) { final List<Object> leftOperand = new ArrayList<>(); final List<Object> receiver = new ArrayList<>(); final JvmConstructor feature = expr.getConstructor(); final List<XExpression> args = getActualArguments(expr); final JvmType type = expr.getConstructor().getDeclaringType(); this.codeReceiver.getImportManager().addImportFor(type); internalAppendCall(feature, leftOperand, receiver, type.getSimpleName(), args, null); }
@Override protected void appendType(final JvmType type, StringBuilder builder) { importManager.appendType(type, builder); }
protected ImportManager createImportManager(final JvmDeclaredType type) { return new ImportManager(true, type); }