Skip to content

Commit

Permalink
Fixed Issue #95
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed Mar 9, 2018
1 parent 6123edc commit a31e659
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import com.openpojo.random.collection.util.BaseCollectionRandomGenerator;
import com.openpojo.random.util.Helper;
import com.openpojo.reflection.construct.InstanceFactory;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.reflection.java.load.ClassUtil;

import static com.openpojo.reflection.impl.PojoClassFactory.getPojoClass;
import static com.openpojo.reflection.java.load.ClassUtil.loadClass;

/**
* @author oshoukry
*/
Expand All @@ -42,14 +44,14 @@ public static ConcurrentSkipListSetRandomGenerator getInstance() {
public Collection<Class<?>> getTypes() {
List<Class<?>> types = new ArrayList<Class<?>>();
if (ClassUtil.isClassLoaded(TYPE))
types.add(ClassUtil.loadClass(TYPE));
types.add(loadClass(TYPE));
return types;
}

@Override
protected Collection getBasicInstance(Class<?> type) {
Helper.assertIsAssignableTo(type, getTypes());
return (Collection) InstanceFactory.getInstance(PojoClassFactory.getPojoClass(ClassUtil.loadClass(TYPE)));
return (Collection) InstanceFactory.getInstance(getPojoClass(loadClass(TYPE)));
}

private ConcurrentSkipListSetRandomGenerator() {
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/openpojo/reflection/impl/PojoMethodFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.openpojo.log.LoggerFactory;
import com.openpojo.reflection.PojoMethod;
import com.openpojo.reflection.utils.AttributeHelper;

import static com.openpojo.reflection.utils.AttributeHelper.getFieldNameVariations;

/**
* This factory handles various method related operations on given Class or Field.
Expand Down Expand Up @@ -119,16 +121,24 @@ public static PojoMethod getFieldGetter(final Field field) {
*/
private static List<String> generateGetMethodNames(final Field field) {
final List<String> prefix = new LinkedList<String>();
prefix.add("get" + AttributeHelper.getAttributeName(field));
prefix.addAll(appendFieldNamesWithPrefix("get", field));
if (field.getType() == boolean.class || field.getType() == Boolean.class) {
prefix.add("is" + AttributeHelper.getAttributeName(field));
prefix.addAll(appendFieldNamesWithPrefix("is", field));
String fieldName = field.getName();
if (fieldName.length() > 2 && fieldName.startsWith("is") && Character.isUpperCase(fieldName.charAt(2)))
prefix.add(fieldName);
}
return prefix;
}

private static List<String> appendFieldNamesWithPrefix(String prefix, Field field) {
List<String> appendedList = new ArrayList<String>();
for (String entry : getFieldNameVariations(field)) {
appendedList.add(prefix + entry);
}
return appendedList;
}

/**
* Returns the Setter Method for a field.
*
Expand Down Expand Up @@ -165,7 +175,7 @@ public static PojoMethod getFieldSetter(final Field field) {
*/
private static List<String> generateSetMethodNames(final Field field) {
final List<String> prefix = new LinkedList<String>();
prefix.add("set" + AttributeHelper.getAttributeName(field));
prefix.addAll(appendFieldNamesWithPrefix("set", field));
String fieldName = field.getName();
if (fieldName.length() > 2 && fieldName.startsWith("is") && Character.isUpperCase(fieldName.charAt(2)))
prefix.add("set" + fieldName.substring(2));
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/openpojo/reflection/utils/AttributeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.openpojo.reflection.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

Expand Down Expand Up @@ -65,13 +67,26 @@ public static void clearRegistry() {
fieldPrefixes.clear();
}

public static List<String> getFieldNameVariations(final Field field) {
List<String> fieldNameVariations = new ArrayList<String>();
fieldNameVariations.add(formattedFieldName(field.getName()));
try {
String withoutPrefix = getAttributeName(field);
if (!fieldNameVariations.contains(withoutPrefix))
fieldNameVariations.add(withoutPrefix);
} catch (ReflectionException ignored) { /* ignored */ }
return fieldNameVariations;
}

/**
* This method returns the attribute name given a field name. The field name will get stripped of prefixes
* @deprecated Please rewire to utilize getFieldNameVariations instead.
*
* @param field
* The field to inspect for attribute name
* @return Normalized attribute name
*/
@Deprecated
public static String getAttributeName(final Field field) {
String normalizedFieldName = field.getName();
normalizedFieldName = stripPrefix(normalizedFieldName);
Expand Down Expand Up @@ -116,6 +131,6 @@ private static String camelCase(String fieldName) {
}

private AttributeHelper() {
throw new UnsupportedOperationException(AttributeHelper.class.getName() + " should not be constructed!");
throw new UnsupportedOperationException(AttributeHelper.class.getName() + " should not be constructed!");
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/openpojo/issues/issue95/IssueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* 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.openpojo.issues.issue95;

import com.openpojo.reflection.utils.AttributeHelper;
import org.junit.Test;

import static com.openpojo.random.RandomFactory.getRandomValue;
import static com.openpojo.reflection.java.load.ClassUtil.loadClass;

/**
* @author oshoukry
*/
public class IssueTest {
@Test
public void shouldGenerateConcurrentSkipListSet() {
String className = "java.util.concurrent.ConcurrentSkipListSet";
AttributeHelper.registerFieldPrefix("m");
getRandomValue(loadClass(className));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.openpojo.reflection.impl;

import java.lang.reflect.Field;

import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.PojoField;
import com.openpojo.reflection.cache.PojoCache;
Expand Down Expand Up @@ -62,9 +64,10 @@ public void shouldNotHaveGettersAndSetters() {
}

@Test(expected = ReflectionException.class)
public void shouldFailAttributeName() {
public void shouldFailAttributeName() throws NoSuchFieldException {
AttributeHelper.registerFieldPrefix("mName");
PojoClassFactory.getPojoClass(AClassWithFieldsPrefixed.class);
Field mNameField = AClassWithFieldsPrefixed.class.getDeclaredField("mName");
AttributeHelper.getAttributeName(mNameField);
}

@Test
Expand Down

0 comments on commit a31e659

Please sign in to comment.