|
15 | 15 | */ |
16 | 16 | package org.mybatis.generator.codegen; |
17 | 17 |
|
| 18 | +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; |
| 19 | + |
18 | 20 | /** |
19 | 21 | * This class exists to that Java client generators can specify whether |
20 | 22 | * an XML generator is required to match the methods in the |
@@ -49,4 +51,92 @@ public boolean requiresXMLGenerator() { |
49 | 51 | * XML is required by this generator |
50 | 52 | */ |
51 | 53 | public abstract AbstractXmlGenerator getMatchedXMLGenerator(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Processes the rootInterface string to replace generic type placeholders with the actual record type. |
| 57 | + * <p> |
| 58 | + * If the rootInterface contains a generic placeholder like {@code <T>}, {@code <E>}, etc., it will be |
| 59 | + * replaced with the actual record type from the introspected table. For example: |
| 60 | + * <ul> |
| 61 | + * <li>{@code "BaseMapper<T>"} becomes {@code "BaseMapper<com.example.User>"}</li> |
| 62 | + * <li>{@code "BaseMapper"} remains {@code "BaseMapper"} (no change for backward compatibility)</li> |
| 63 | + * <li>{@code "BaseMapper<com.example.User>"} remains unchanged (already fully qualified)</li> |
| 64 | + * </ul> |
| 65 | + * |
| 66 | + * @param rootInterface |
| 67 | + * the rootInterface string from configuration, may contain generic placeholders |
| 68 | + * @param recordType |
| 69 | + * the actual record type (fully qualified) to use as replacement |
| 70 | + * @return the processed rootInterface string with placeholders replaced, or the original string if no |
| 71 | + * replacement is needed |
| 72 | + */ |
| 73 | + protected String processRootInterfaceWithGenerics(String rootInterface, String recordType) { |
| 74 | + if (!stringHasValue(rootInterface) || !stringHasValue(recordType)) { |
| 75 | + return rootInterface; |
| 76 | + } |
| 77 | + |
| 78 | + // Check if rootInterface contains generic brackets |
| 79 | + int openBracketIndex = rootInterface.indexOf('<'); |
| 80 | + if (openBracketIndex == -1) { |
| 81 | + // No generic brackets, return as-is for backward compatibility |
| 82 | + return rootInterface; |
| 83 | + } |
| 84 | + |
| 85 | + int closeBracketIndex = rootInterface.lastIndexOf('>'); |
| 86 | + if (closeBracketIndex == -1 || closeBracketIndex <= openBracketIndex) { |
| 87 | + // Malformed generic brackets, return as-is |
| 88 | + return rootInterface; |
| 89 | + } |
| 90 | + |
| 91 | + String baseInterface = rootInterface.substring(0, openBracketIndex); |
| 92 | + String genericContent = rootInterface.substring(openBracketIndex + 1, closeBracketIndex).trim(); |
| 93 | + |
| 94 | + // Check if the generic content is a placeholder (single letter or single letter with extends clause) |
| 95 | + // Common Java generic type parameter names: T, E, K, V, N, U, R, S |
| 96 | + if (isGenericPlaceholder(genericContent)) { |
| 97 | + // Replace placeholder with actual record type |
| 98 | + return baseInterface + "<" + recordType + ">"; |
| 99 | + } |
| 100 | + |
| 101 | + // Already has a concrete type (fully qualified or not), return as-is |
| 102 | + return rootInterface; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Checks if a generic content string represents a placeholder that should be replaced. |
| 107 | + * Placeholders are typically single-letter type parameters like T, E, K, V, etc., |
| 108 | + * optionally with an "extends" clause like "T extends Entity". |
| 109 | + * |
| 110 | + * @param genericContent |
| 111 | + * the content between angle brackets |
| 112 | + * @return true if this looks like a placeholder that should be replaced |
| 113 | + */ |
| 114 | + private boolean isGenericPlaceholder(String genericContent) { |
| 115 | + if (genericContent.isEmpty()) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + String trimmed = genericContent.trim(); |
| 120 | + |
| 121 | + // Simple case: just a single uppercase letter (T, E, K, V, etc.) |
| 122 | + if (trimmed.length() == 1 && Character.isUpperCase(trimmed.charAt(0))) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + // Case with bounds: "T extends SomeClass" or "T super SomeClass" |
| 127 | + // Pattern: single uppercase letter followed by " extends " or " super " |
| 128 | + int extendsIndex = trimmed.indexOf(" extends "); |
| 129 | + int superIndex = trimmed.indexOf(" super "); |
| 130 | + |
| 131 | + if (extendsIndex > 0 || superIndex > 0) { |
| 132 | + int boundIndex = extendsIndex > 0 ? extendsIndex : superIndex; |
| 133 | + // First part should be a single uppercase letter |
| 134 | + String typeParam = trimmed.substring(0, boundIndex).trim(); |
| 135 | + if (typeParam.length() == 1 && Character.isUpperCase(typeParam.charAt(0))) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return false; |
| 141 | + } |
52 | 142 | } |
0 commit comments