|
| 1 | +package com.fasterxml.jackson.databind.introspect; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.cfg.MapperConfig; |
| 4 | + |
| 5 | +/** |
| 6 | + * Default {@link AccessorNamingStrategy} used by Jackson: to be used either as-is, |
| 7 | + * or as base-class with overrides. |
| 8 | + */ |
| 9 | +public class DefaultAccessorNamingStrategy |
| 10 | + extends AccessorNamingStrategy |
| 11 | +{ |
| 12 | + protected final MapperConfig<?> _config; |
| 13 | + protected final AnnotatedClass _forClass; |
| 14 | + |
| 15 | + /** |
| 16 | + * Prefix used by auto-detected mutators ("setters"): usually "set", |
| 17 | + * but differs for builder objects ("with" by default). |
| 18 | + */ |
| 19 | + protected final String _mutatorPrefix; |
| 20 | + |
| 21 | + protected DefaultAccessorNamingStrategy(MapperConfig<?> config, AnnotatedClass forClass, |
| 22 | + String mutatorPrefix) { |
| 23 | + _config = config; |
| 24 | + _forClass = forClass; |
| 25 | + |
| 26 | + _mutatorPrefix = mutatorPrefix; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String findNameForIsGetter(AnnotatedMethod am, String name) |
| 31 | + { |
| 32 | + final Class<?> rt = am.getRawType(); |
| 33 | + if (rt == Boolean.class || rt == Boolean.TYPE) { |
| 34 | + if (name.startsWith("is")) { // plus, must return a boolean |
| 35 | + return stdManglePropertyName(name, 2); |
| 36 | + } |
| 37 | + } |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public String findNameForRegularGetter(AnnotatedMethod am, String name) |
| 43 | + { |
| 44 | + if (name.startsWith("get")) { |
| 45 | + // 16-Feb-2009, tatu: To handle [JACKSON-53], need to block CGLib-provided |
| 46 | + // method "getCallbacks". Not sure of exact safe criteria to get decent |
| 47 | + // coverage without false matches; but for now let's assume there is |
| 48 | + // no reason to use any such getter from CGLib. |
| 49 | + if ("getCallbacks".equals(name)) { |
| 50 | + if (isCglibGetCallbacks(am)) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + } else if ("getMetaClass".equals(name)) { |
| 54 | + // 30-Apr-2009, tatu: Need to suppress serialization of a cyclic reference |
| 55 | + if (isGroovyMetaClassGetter(am)) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + } |
| 59 | + return stdManglePropertyName(name, 3); |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String findNameForMutator(AnnotatedMethod am, String name) |
| 66 | + { |
| 67 | + if (name.startsWith(_mutatorPrefix)) { |
| 68 | + return stdManglePropertyName(name, _mutatorPrefix.length()); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + /********************************************************************** |
| 75 | + /* Name-mangling methods copied in 2.12 from "BeanUtil" |
| 76 | + /********************************************************************** |
| 77 | + */ |
| 78 | + |
| 79 | + // 24-Sep-2017, tatu: note that "std" here refers to earlier (1.x, 2.x) distinction |
| 80 | + // between "legacy" (slightly non-conforming) and "std" (fully conforming): with 3.x |
| 81 | + // only latter exists. |
| 82 | + protected static String stdManglePropertyName(final String basename, final int offset) |
| 83 | + { |
| 84 | + final int end = basename.length(); |
| 85 | + if (end == offset) { // empty name, nope |
| 86 | + return null; |
| 87 | + } |
| 88 | + // first: if it doesn't start with capital, return as-is |
| 89 | + char c0 = basename.charAt(offset); |
| 90 | + char c1 = Character.toLowerCase(c0); |
| 91 | + if (c0 == c1) { |
| 92 | + return basename.substring(offset); |
| 93 | + } |
| 94 | + // 17-Dec-2014, tatu: As per [databind#653], need to follow more |
| 95 | + // closely Java Beans spec; specifically, if two first are upper-case, |
| 96 | + // then no lower-casing should be done. |
| 97 | + if ((offset + 1) < end) { |
| 98 | + if (Character.isUpperCase(basename.charAt(offset+1))) { |
| 99 | + return basename.substring(offset); |
| 100 | + } |
| 101 | + } |
| 102 | + StringBuilder sb = new StringBuilder(end - offset); |
| 103 | + sb.append(c1); |
| 104 | + sb.append(basename, offset+1, end); |
| 105 | + return sb.toString(); |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + /********************************************************************** |
| 110 | + /* Legacy methods copied in 2.12 from "BeanUtil" -- are these still needed? |
| 111 | + /********************************************************************** |
| 112 | + */ |
| 113 | + |
| 114 | + // This method was added to address the need to weed out CGLib-injected |
| 115 | + // "getCallbacks" method. |
| 116 | + // At this point caller has detected a potential getter method with |
| 117 | + // name "getCallbacks" and we need to determine if it is indeed injected |
| 118 | + // by Cglib. We do this by verifying that the result type is "net.sf.cglib.proxy.Callback[]" |
| 119 | + private static boolean isCglibGetCallbacks(AnnotatedMethod am) |
| 120 | + { |
| 121 | + Class<?> rt = am.getRawType(); |
| 122 | + // Ok, first: must return an array type |
| 123 | + if (rt.isArray()) { |
| 124 | + // And that type needs to be "net.sf.cglib.proxy.Callback". |
| 125 | + // Theoretically could just be a type that implements it, but |
| 126 | + // for now let's keep things simple, fix if need be. |
| 127 | + |
| 128 | + Class<?> compType = rt.getComponentType(); |
| 129 | + // Actually, let's just verify it's a "net.sf.cglib.*" class/interface |
| 130 | + String className = compType.getName(); |
| 131 | + if (className.contains(".cglib")) { |
| 132 | + return className.startsWith("net.sf.cglib") |
| 133 | + // also, as per [JACKSON-177] |
| 134 | + || className.startsWith("org.hibernate.repackage.cglib") |
| 135 | + // and [core#674] |
| 136 | + || className.startsWith("org.springframework.cglib"); |
| 137 | + } |
| 138 | + } |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + // Another helper method to deal with Groovy's problematic metadata accessors |
| 143 | + private static boolean isGroovyMetaClassGetter(AnnotatedMethod am) { |
| 144 | + return am.getRawType().getName().startsWith("groovy.lang"); |
| 145 | + } |
| 146 | + |
| 147 | + /* |
| 148 | + /********************************************************************** |
| 149 | + /* Standard Provider implementation |
| 150 | + /********************************************************************** |
| 151 | + */ |
| 152 | + |
| 153 | + /** |
| 154 | + * Provider for {@link DefaultAccessorNamingStrategy}. |
| 155 | + */ |
| 156 | + protected static class Provider |
| 157 | + extends AccessorNamingStrategy.Provider |
| 158 | + implements java.io.Serializable |
| 159 | + { |
| 160 | + private static final long serialVersionUID = 1L; |
| 161 | + |
| 162 | + @Override |
| 163 | + public AccessorNamingStrategy forPOJO(MapperConfig<?> config, AnnotatedClass ac, |
| 164 | + String mutatorPrefix) { |
| 165 | + return new DefaultAccessorNamingStrategy(config, ac, mutatorPrefix); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public AccessorNamingStrategy forBuilder(MapperConfig<?> config, AnnotatedClass builderClass, |
| 170 | + AnnotatedClass targetClass, String mutatorPrefix) |
| 171 | + { |
| 172 | + return new DefaultAccessorNamingStrategy(config, builderClass, mutatorPrefix); |
| 173 | + } |
| 174 | + |
| 175 | + } |
| 176 | +} |
0 commit comments