下面列出了java.lang.reflect.Field#toGenericString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected List<StructuralProperty> buildStructuralProperties(Class<?> cls) {
List<StructuralProperty> properties = new ArrayList<>();
for (Field field : cls.getDeclaredFields()) {
EdmProperty propertyAnno = field.getAnnotation(EdmProperty.class);
EdmNavigationProperty navigationPropertyAnno = field.getAnnotation(EdmNavigationProperty.class);
if (propertyAnno != null) {
if (navigationPropertyAnno != null) {
throw new IllegalArgumentException("Field has both an EdmProperty and an EdmNavigationProperty " +
"annotation. Only one of the two is allowed: " + field.toGenericString());
}
properties.add(buildProperty(propertyAnno, field));
} else if (navigationPropertyAnno != null) {
properties.add(buildNavigationProperty(navigationPropertyAnno, field));
}
}
return properties;
}
public B setTypeFromJavaField(Field field, TypeNameResolver resolver) {
// Find out if the field is an array or collection; get the element type if that is the case
Class<?> cls = field.getType();
if (cls.isArray()) {
this.isCollection = true;
cls = cls.getComponentType();
} else if (Collection.class.isAssignableFrom(cls)) {
this.isCollection = true;
cls = getCollectionElementType(field);
} else {
this.isCollection = false;
}
this.typeName = resolver.resolveTypeName(cls);
if (isNullOrEmpty(this.typeName)) {
throw new IllegalArgumentException("The OData type of this field cannot be determined from " +
"the Java type: " + field.toGenericString());
}
return self;
}
/**
* Records the field of a target object is visited
*
* @param object The field holder
* @param objectField The field which holds the object in its parent
* @param field The field of the holder
*/
private void recordVisitField(Object object, Field objectField, Field field) {
Map<String, Set<String>> bag = visitedFields.get(object);
if (bag == null) {
bag = new HashMap<>();
visitedFields.put(object, bag);
}
Set<String> fields = bag.get(objectField);
String objectFiledKey = objectField == null ? "" : objectField.toGenericString();
if (fields == null) {
fields = new HashSet<>();
bag.put(objectFiledKey, fields);
}
fields.add(field.toGenericString());
}
/**
* Retrieves (from a field of map type) the key and value classes.
*
* @param field to analyze
* @param key key class
* @param value value class
*/
public static void getKeyValueClasses(Field field,Class<?> key, Class<?> value){
if(!mapIsAssignableFrom(field.getType()))
throw new IllegalArgumentException("the field is not a map");
String generic = field.toGenericString();
String[] pair = generic.substring(generic.indexOf("<")+1, generic.indexOf(">")).split(", ");
try {
key = Class.forName(pair[0].trim());
value = Class.forName(pair[1].trim());
} catch (ClassNotFoundException e) {
key = null;
value = null;
}
}
public CompilePropertyEntity(Context context, Field field) {
super(context);
this.field = field;
this.field.setAccessible(true);
setModifier(php.runtime.common.Modifier.PUBLIC);
if (Modifier.isProtected(field.getModifiers())) {
setModifier(php.runtime.common.Modifier.PROTECTED);
} else if (Modifier.isPrivate(field.getModifiers())) {
throw new CriticalException("Unsupported bind private fields: " + field.toGenericString());
}
this.operation = MemoryOperation.get(field.getType(), field.getGenericType());
if (this.operation == null) {
throw new CriticalException("Unsupported type for field " + field.toGenericString());
}
setStatic(Modifier.isStatic(field.getModifiers()));
}
/**
* Indicates whether the field of a target object is visited
*
* @param object The field holder
* @param objectField The field which holds the object in its parent
* @param field The field of the holder
*/
private boolean isFieldVisited(Object object, Field objectField, Field field) {
Map<String, Set<String>> bag = visitedFields.get(object);
if (bag == null) {
return false;
}
String objectFiledKey = objectField == null ? "" : objectField.toGenericString();
Set<String> fields = bag.get(objectFiledKey);
return fields != null && fields.contains(field);
}
protected String getFieldDescriptionFromField(Field field) {
return "Autogenerated from j.l.r.Field [" + field.toGenericString() + "]";
}
private void marshallStructuralProperty(Object object, StructuralProperty property)
throws ODataRenderException, IOException, NoSuchFieldException, IllegalAccessException {
String propertyName = property.getName();
// Get the property value through reflection
Object propertyValue;
Field field = property.getJavaField();
try {
field.setAccessible(true);
propertyValue = field.get(object);
} catch (IllegalAccessException e) {
LOG.error("Error getting field value of field: " + field.toGenericString());
throw new ODataRenderException("Error getting field value of field: " + field.toGenericString());
}
// Collection properties and non-nullable properties should not be null
if (propertyValue == null) {
if (property.isCollection()) {
throw new ODataRenderException("Collection property has null value: " + property);
} else if (!property.isNullable()) {
throw new ODataRenderException("Non-nullable property has null value: " + property);
}
}
// Check if the property is a collection
if (property.isCollection()) {
// Get an iterator for the array or collection
Iterator<?> iterator;
if (propertyValue.getClass().isArray()) {
iterator = Arrays.asList((Object[]) propertyValue).iterator();
} else if (Collection.class.isAssignableFrom(propertyValue.getClass())) {
iterator = ((Collection<?>) propertyValue).iterator();
} else {
throw new UnsupportedOperationException("Unsupported collection type: " +
propertyValue.getClass().getName() + " for property: " + propertyName);
}
// Get the OData type of the elements of the collection
Type elementType = entityDataModel.getType(property.getElementTypeName());
if (elementType == null) {
throw new ODataRenderException("OData type not found for elements of property: " + property);
}
LOG.trace("Start collection property: {}", propertyName);
if (((Collection) propertyValue).isEmpty()) {
jsonGenerator.writeArrayFieldStart(propertyName);
jsonGenerator.writeEndArray();
} else {
while (iterator.hasNext()) {
Object element = iterator.next();
if (element instanceof Number | element instanceof String | element.getClass().isEnum()) {
marshallToArray(propertyName, element, iterator);
} else {
marshallCollection(propertyName, iterator, element, elementType);
}
}
}
LOG.trace("End collection property: {}", propertyName);
} else {
// Single value (non-collection) property
LOG.trace("Start property: {}", propertyName);
// Get the OData type of the property
Type propertyType = entityDataModel.getType(property.getTypeName());
if (propertyType == null) {
throw new ODataRenderException("OData type not found for property: " + property);
}
jsonGenerator.writeFieldName(propertyName);
if (propertyType.getMetaType().equals(COMPLEX) && propertyValue != null) {
jsonGenerator.writeStartObject();
}
marshall(propertyValue, propertyType);
if (propertyType.getMetaType().equals(COMPLEX) && propertyValue != null) {
jsonGenerator.writeEndObject();
}
LOG.trace("End property: {}", propertyName);
}
}
/**
* Splits the fieldDescription to obtain his class type,generics inclusive.
* @param field field to check
* @return returns a string that specified the structure of the field, including its generic
*/
public static String getGenericString(Field field){
String fieldDescription = field.toGenericString();
List<String> splitResult = new ArrayList<String>();
char[] charResult = fieldDescription.toCharArray();
boolean isFinished = false;
int separatorIndex = fieldDescription.indexOf(" ");
int previousIndex = 0;
while(!isFinished){
// if previous character is "," don't cut the string
int position = separatorIndex-1;
char specialChar = charResult[position];
boolean isSpecialChar = true;
if(specialChar!=',' && specialChar != '?'){
if(specialChar == 's'){
String specialString = null;
try{
specialString = fieldDescription.substring(position - "extends".length(), position+1);
if(isNull(specialString) || !" extends".equals(specialString))
isSpecialChar = false;
}catch(IndexOutOfBoundsException e){
isSpecialChar = false;
}
}else
isSpecialChar = false;
}
if(!isSpecialChar){
splitResult.add(fieldDescription.substring(previousIndex, separatorIndex));
previousIndex = separatorIndex+1;
}
separatorIndex = fieldDescription.indexOf(" ",separatorIndex+1);
if(separatorIndex == -1)isFinished = true;
}
for (String description : splitResult)
if(!isAccessModifier(description)) return description;
return null;
}
/**
* Provides an {@link AbstractAnnotations} implementation based on provided field's annotations.
*
* @param field which annotations are checked
* @param injector to provide Bobcat-augmented annotations
* @return <ul>
* <li>{@link Annotations} for fields decorated with {@link org.openqa.selenium.support.FindBy}, {@link org.openqa.selenium.support.FindAll} or {@link org.openqa.selenium.support.FindBys}</li>
* <li>{@link BobcatAnnotations} for fields decorated with {@link com.cognifide.qa.bb.qualifier.FindPageObject}</li>
* </ul>
* @throws IllegalArgumentException when the field is not decorated with any of the above annotations
*/
public static AbstractAnnotations create(Field field, Injector injector) {
if (AnnotationsHelper.isFindByAnnotationPresent(field)) {
return new Annotations(field);
}
if (AnnotationsHelper.isFindPageObjectAnnotationPresent(field)) {
return new BobcatAnnotations(field, injector);
}
throw new IllegalArgumentException(
"Field is not marked by any supported annotation: " + field.toGenericString());
}