View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package com.tapas.evidence.utility;
20  
21  import java.lang.reflect.Field;
22  
23  /**
24   * @author Michal Bocek
25   * @since 1.0.0
26   */
27  public final class FieldUtils {
28  	
29  	private FieldUtils() {
30  	}
31  	
32  	/**
33  	 * Get all fields (private, protected, public) included inherited fields.
34  	 * @param objectClass
35  	 * @param fields
36  	 * @return
37  	 * @throws ClassNotFoundException
38  	 */
39  	@SuppressWarnings("rawtypes")
40  	public static Field[] getAllFields(final Class objectClass, final Field[] fields) throws ClassNotFoundException {
41  		final Field[] totalFields = getFields(objectClass, fields);
42  		final Class superClass = objectClass.getSuperclass();
43  		
44  		Field[] finalFieldsArray;
45  		Class clazz = null;
46  		
47  		clazz = Class.forName("java.lang.Object");
48  		
49  		if (superClass != null && !superClass.equals(clazz)) { // NOPMD
50  			finalFieldsArray = getAllFields(superClass, totalFields);
51  		} else {
52  			finalFieldsArray = totalFields;
53  		}
54  
55  		return finalFieldsArray;
56  	}
57  
58  	/**
59  	 * Get all fields (private, protected, public) for object except inherited fields.
60  	 * @param objectClass
61  	 * @param fields
62  	 * @return
63  	 */
64  	@SuppressWarnings("rawtypes")
65  	public static Field[] getFields(final Class objectClass, final Field[] fields) {
66  		final Field[] newFields = objectClass.getDeclaredFields();
67  
68  		int fieldsSize = 0;
69  		int newFieldsSize = 0;
70  
71  		if (fields != null) {
72  			fieldsSize = fields.length;
73  		}
74  
75  		if (newFields != null) {
76  			newFieldsSize = newFields.length;
77  		}
78  
79  		final Field[] totalFields = new Field[fieldsSize + newFieldsSize];
80  
81  		if (fieldsSize > 0) {
82  			System.arraycopy(fields, 0, totalFields, 0, fieldsSize);
83  		}
84  
85  		if (newFieldsSize > 0) {
86  			System.arraycopy(newFields, 0, totalFields, fieldsSize, newFieldsSize);
87  		}
88  		return totalFields;
89  	}
90  	
91  	public static void setField(final Object object, final String fieldName, final Object value) 
92  			throws NoSuchFieldException, IllegalAccessException {
93  		final Field field = object.getClass().getDeclaredField(fieldName);
94  		field.setAccessible(true); // NOPMD
95  		field.set(object, value);
96  	}
97  }