-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support javaGetter for is prefix boolean property (#1072)
- Loading branch information
Showing
5 changed files
with
172 additions
and
32 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...ava/com/navercorp/fixturemonkey/api/experimental/JavaGetterPropertyFieldNameResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.experimental; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.navercorp.fixturemonkey.api.type.TypeCache; | ||
|
||
public final class JavaGetterPropertyFieldNameResolver { | ||
private static final String GET_PREFIX = "get"; | ||
private static final String IS_PREFIX = "is"; | ||
|
||
@Nullable | ||
public String resolveFieldName(Class<?> targetClass, String methodName) { | ||
if (hasPrefix(GET_PREFIX, methodName)) { | ||
return stripPrefixPropertyName(targetClass, methodName, GET_PREFIX.length()); | ||
} else if (hasPrefix(IS_PREFIX, methodName)) { | ||
return stripPrefixPropertyName(targetClass, methodName, IS_PREFIX.length()); | ||
} else if (isValidField(targetClass, methodName)) { | ||
// class could be using property-style getters (e.g. java record) | ||
return methodName; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static String stripPrefixPropertyName(Class<?> targetClass, String methodName, int prefixLength) { | ||
char[] ch = methodName.toCharArray(); | ||
ch[prefixLength] = Character.toLowerCase(ch[prefixLength]); | ||
String fieldName = new String(ch, prefixLength, ch.length - prefixLength); | ||
return isValidField(targetClass, fieldName) ? fieldName : null; | ||
} | ||
|
||
private static boolean hasPrefix(String prefix, String methodName) { | ||
return methodName.startsWith(prefix) && methodName.length() > prefix.length(); | ||
} | ||
|
||
private static boolean isValidField(Class<?> type, String fieldName) { | ||
return TypeCache.getFieldsByName(type).containsKey(fieldName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...a17/com/navercorp/fixturemonkey/api/experimental/JavaGetterPropertyFieldNameResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.experimental; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.navercorp.fixturemonkey.api.type.TypeCache; | ||
|
||
public final class JavaGetterPropertyFieldNameResolver { | ||
private static final String GET_PREFIX = "get"; | ||
private static final String IS_PREFIX = "is"; | ||
|
||
@Nullable | ||
public String resolveFieldName(Class<?> targetClass, String methodName) { | ||
if (targetClass.isRecord()) { | ||
if (isValidField(targetClass, methodName)) { | ||
return methodName; | ||
} | ||
} | ||
|
||
if (hasPrefix(GET_PREFIX, methodName)) { | ||
return stripPrefixPropertyName(targetClass, methodName, GET_PREFIX.length()); | ||
} else if (hasPrefix(IS_PREFIX, methodName)) { | ||
return stripPrefixPropertyName(targetClass, methodName, IS_PREFIX.length()); | ||
} else if (isValidField(targetClass, methodName)) { | ||
// class could be using property-style getters (e.g. java record) | ||
return methodName; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static String stripPrefixPropertyName(Class<?> targetClass, String methodName, int prefixLength) { | ||
char[] ch = methodName.toCharArray(); | ||
ch[prefixLength] = Character.toLowerCase(ch[prefixLength]); | ||
String fieldName = new String(ch, prefixLength, ch.length - prefixLength); | ||
return isValidField(targetClass, fieldName) ? fieldName : null; | ||
} | ||
|
||
private static boolean hasPrefix(String prefix, String methodName) { | ||
return methodName.startsWith(prefix) && methodName.length() > prefix.length(); | ||
} | ||
|
||
private static boolean isValidField(Class<?> type, String fieldName) { | ||
return TypeCache.getFieldsByName(type).containsKey(fieldName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters