|
| 1 | +package tools.jackson.databind.util; |
| 2 | + |
| 3 | +import java.lang.reflect.RecordComponent; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import tools.jackson.databind.PropertyName; |
| 8 | +import tools.jackson.databind.cfg.MapperConfig; |
| 9 | +import tools.jackson.databind.introspect.AnnotatedClass; |
| 10 | +import tools.jackson.databind.introspect.AnnotatedConstructor; |
| 11 | +import tools.jackson.databind.introspect.PotentialCreator; |
| 12 | + |
| 13 | +/** |
| 14 | + * Helper class for finding so-called canonical constructor |
| 15 | + * of Record types. |
| 16 | + */ |
| 17 | +public class RecordUtil |
| 18 | +{ |
| 19 | + public static String[] getRecordFieldNames(Class<?> recordType) { |
| 20 | + final RecordComponent[] components = recordType.getRecordComponents(); |
| 21 | + if (components == null) { |
| 22 | + // not a record, or no reflective access on native image |
| 23 | + return null; |
| 24 | + } |
| 25 | + return Arrays.stream(components).map(RecordComponent::getName).toArray(String[]::new); |
| 26 | + } |
| 27 | + |
| 28 | + public static PotentialCreator findCanonicalRecordConstructor(MapperConfig<?> config, |
| 29 | + AnnotatedClass recordClass, |
| 30 | + List<PotentialCreator> constructors) |
| 31 | + { |
| 32 | + final RecordComponent[] components = recordClass.getRawType().getRecordComponents(); |
| 33 | + if (components == null) { |
| 34 | + // not a record, or no reflective access on native image |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + // And then locate the canonical constructor |
| 39 | + final int argCount = components.length; |
| 40 | + // One special case: zero-arg constructor not included in candidate List |
| 41 | + if (argCount == 0) { |
| 42 | + // Bit hacky but has to do: create new PotentialCreator let caller deal |
| 43 | + AnnotatedConstructor defCtor = recordClass.getDefaultConstructor(); |
| 44 | + if (defCtor != null) { |
| 45 | + return new PotentialCreator(defCtor, null); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + main_loop: |
| 50 | + for (PotentialCreator ctor : constructors) { |
| 51 | + if (ctor.paramCount() != argCount) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + for (int i = 0; i < argCount; ++i) { |
| 55 | + if (!ctor.creator().getRawParameterType(i).equals(components[i].getType())) { |
| 56 | + continue main_loop; |
| 57 | + } |
| 58 | + } |
| 59 | + // Found it! One more thing; get implicit Record field names: |
| 60 | + final PropertyName[] implicits = new PropertyName[argCount]; |
| 61 | + for (int i = 0; i < argCount; ++i) { |
| 62 | + implicits[i] = PropertyName.construct(components[i].getName()); |
| 63 | + } |
| 64 | + return ctor.introspectParamNames(config, implicits); |
| 65 | + } |
| 66 | + |
| 67 | + throw new IllegalArgumentException("Failed to find the canonical Record constructor of type " |
| 68 | + +ClassUtil.getTypeDescription(recordClass.getType())); |
| 69 | + } |
| 70 | +} |
0 commit comments