Refactoring business logic validation - java

I'm trying to refactoring this code
private void validate(Customer customer) {
List<String> errors = new ArrayList<>();
if (customer == null) {
errors.add("Customer must not be null");
}
if (customer != null && customer.getName() == null) {
errors.add("Name must not be null");
}
if (customer != null && customer.getName().isEmpty()) {
errors.add("Name must not be empty");
}
if (customer != null) {
Customer customerFromDb = customerRepository.findByName(customer.getName());
if (customerFromDb != null) {
errors.add("Customer already present on db");
}
}
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
}
I read this post Business logic validation patterns & advices
I'd like to build a generic validator for my entities and fields of the entity, I wrote this
private void validate(Customer customer) {
List<ValidationRule> validationRules = new ArrayList<>();
validationRules.add(new NotNullValidationRule(customer));
validationRules.add(new NotNullValidationRule(customer, Customer::getName));
validationRules.add(new NotEmptyValidationRule(customer, Customer::getName));
validationRules.add(new NotExistValidationRule(customer -> customerRepository.findByName(customer.getName())));
Validator.validate(validationRules);
}
and the Validator class
public class Validator {
public static void validate(List<ValidationRule> validationRules) {
final List<String> errors = new ArrayList<>();
for (final ValidationRule rule : validationRules) {
final Optional<String> error = rule.validate();
if (error.isPresent()) {
errors.add(error.get());
}
}
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
}
}
but I don't know how to implement the interface ValidationRule and other classes (NotNullValidationRule, NotEmptyValidationRule, NotExistValidationRule)

I would write something like :
CommonValidations.notNull(errors, customer);
if (customer != null) {
CommonValidations.notEmpty(errors, customer.getName());
}
customerCustomeBeanValidations.validName(errors, customer.getName());
customerCustomeBeanValidations.notExist(errors, customer.getName());

In the link you reference, the accepted answer suggested using the Strategy design pattern, and then gave an example of both an interface and implementation. In your case, you would create a new interface ValidationRule, with at least one method validate(), and then you would create concrete classes that each implementat that interface (NotNullValidationRule, NotEmptyValidationRule, AlreadyExistValidationRule).

I found this solution:
I create an interface ValidationRule
import java.util.Optional;
public interface ValidationRule {
Optional<ValidationError> validate();
}
And some classes that implement the behaviours
public class NotNullValidationRule implements ValidationRule {
private Object object;
private String field;
public NotNullValidationRule(Object object, String field) {
this.object = object;
if (field == null || field.isEmpty()) {
throw new IllegalArgumentException("field must not be null or emtpy");
}
this.field = field;
}
#Override public Optional<ValidationError> validate() {
if (object == null) {
return Optional.empty();
}
try {
Object value = new PropertyDescriptor(field, object.getClass()).getReadMethod().invoke(object);
if (value == null) {
ValidationError validationError = new ValidationError();
validationError.setName(object.getClass().getSimpleName() + "." + field);
validationError.setError("Field " + field + " is null");
return Optional.of(validationError);
}
}
catch (Exception e) {
throw new IllegalStateException("error during retrieve of field value");
}
return Optional.empty();
}
}
Another where I pass a method to call:
package it.winetsolutions.winactitime.core.service.validation;
import java.beans.PropertyDescriptor;
import java.util.Optional;
import java.util.function.Function;
public class NotExistValidationRule implements ValidationRule {
Object object;
String field;
Function<? super String, ? super Object> function;
public NotExistValidationRule(Object object, String field, Function<? super String, ? super Object> function) {
this.object = object;
if (field == null || field.isEmpty() || function == null) {
throw new IllegalArgumentException("field and function must not be null or emtpy");
}
this.field = field;
this.function = function;
}
#Override public Optional<ValidationError> validate() {
if (object == null) {
return Optional.empty();
}
try {
Object value = new PropertyDescriptor(field, object.getClass()).getReadMethod().invoke(object);
Long id = (Long) new PropertyDescriptor("id", object.getClass()).getReadMethod().invoke(object);
Object result = function.apply(value == null ? (String) value : ((String) value).trim());
if (result != null &&
!id.equals((Long) new PropertyDescriptor("id", result.getClass()).getReadMethod().invoke(result))) {
ValidationError validationError = new ValidationError();
validationError.setName(object.getClass().getSimpleName() + "." + field);
validationError.setError("Element with " + field +": " + value + " already exists");
return Optional.of(validationError);
}
}
catch (Exception e) {
throw new IllegalStateException("error during retrieve of field value");
}
return Optional.empty();
}
}

Related

StackOverflowError in equals method of bidirectional objects

I have to objects Client and Order and these objects are living in bidirectional relation and I try to write them to file, but I get StackOverflowError. I got this error because my equals methods are looped.
My classes which I try to serialize:
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class Client implements Serializable {
private Long id;
private String name;
private List<Order> orders = new ArrayList<>();
public void addOrder(Order order) {
order.setClient(this);
orders.add(order);
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Client client = (Client) o;
if (id != null ? !id.equals(client.id) : client.id != null) return false;
if (name != null ? !name.equals(client.name) : client.name != null) return false;
return orders != null ? orders.equals(client.orders) : client.orders == null;
}
#Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (orders != null ? orders.hashCode() : 0);
return result;
}
#Override
public String toString() {
return "Client{" +
"id=" + id +
", name='" + name + '\'' +
// ", orders=" + orders.size() +
'}';
}
}
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class Order implements Serializable {
private Long id;
private String name;
private Client client;
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
if (id != null ? !id.equals(order.id) : order.id != null) return false;
if (name != null ? !name.equals(order.name) : order.name != null) return false;
return client != null ? client.equals(order.client) : order.client == null;
}
#Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (client != null ? client.hashCode() : 0);
return result;
}
#Override
public String toString() {
return "Order{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
#Data
#AllArgsConstructor
public class MapDataSource implements Serializable {
private final Map<Date, List<Client>> clients = new HashMap<>();
private final Map<Date, List<Order>> orders = new HashMap<>();
}
#Slf4j
public class ObjectWriter {
private final String fileName = "data.obj";
public void write(String fileName, MapDataSource mapDataSource) {
try (
FileOutputStream fs = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fs)
) {
oos.writeObject(mapDataSource);
log.info("Object has been written.");
} catch (IOException ioe) {}
}
}
#Slf4j
public class ObjectReader {
private static final String fileName = "data.obj";
public MapDataSource readObj(String fileName) {
MapDataSource mapDataSource = null;
try (
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis)
) {
mapDataSource = ((MapDataSource) ois.readObject());
// log.info("Read object: {}", mapDataSource);
} catch (IOException ioe) {
} catch (ClassNotFoundException classEx) {
System.out.println();
}
return mapDataSource;
}
}
And when I try to run code below I get StackOVerflowError:
String testFile = "testFile.obj";
final DateTime time = new DateTime(2017, 12, 1, 10, 0);
final Client client1 = new Client(1L, "Client1", new ArrayList<>());
final Order order1 = new Order(1L, "Order1", null);
final MapDataSource mapDataSource = new MapDataSource();
mapDataSource.getClients().put(time.toDate(), new ArrayList<>());
mapDataSource.getClients().get(time.toDate()).add(client1);
mapDataSource.getOrders().put(time.toDate(), new ArrayList<>());
mapDataSource.getOrders().get(time.toDate()).add(order1);
new ObjectWriter().write(testFile, mapDataSource);
final MapDataSource found = new ObjectReader().readObj(testFile);
System.out.println(found);
Solution:
MapDataSource needs to have implemented equals() and hashcode() methods.
It seems like you need to sit down and seriously consider what it should even mean for two clients or orders to be equal in the first place. The Long id; makes me wonder whether you should really be comparing the object graphs in the first place. If e.g. clients have unique IDs, then it could make sense to just ensure that clients are unique object instances and then you do away with the problem entirely.
If you really do need to compare object graphs, you could use something like the following. We use an IdentityHashMap to record all of the objects we've seen, then if we detect a cycle, we just compare the previously-stored counter values which tells us if the two graphs have the same cycle.
Client and Order need to share code (so the maps can be passed around), so you just override equals in both to return ClientOrderEquality.equals(this, that).
import java.util.*;
public final class ClientOrderEquality {
private ClientOrderEquality() {}
private static final class Counter { long value; }
public static boolean equals(Client lhs, Client rhs) {
return equals(lhs, new IdentityHashMap<>(),
rhs, new IdentityHashMap<>(),
new Counter());
}
public static boolean equals(Order lhs, Order rhs) {
return equals(lhs, new IdentityHashMap<>(),
rhs, new IdentityHashMap<>(),
new Counter());
}
private static boolean equals(Client lhs,
Map<Object, Long> seenL,
Client rhs,
Map<Object, Long> seenR,
Counter counter) {
if (lhs == null || rhs == null)
return lhs == rhs;
Long countL = seenL.putIfAbsent(lhs, counter.value);
Long countR = seenR.putIfAbsent(rhs, counter.value);
if (countL != null || countR != null)
return Objects.equals(countL, countR);
counter.value++;
if (lhs == rhs)
return true;
if (!Objects.equals(lhs.id, rhs.id))
return false;
if (!Objects.equals(lhs.name, rhs.name))
return false;
if (lhs.orders.size() != rhs.orders.size())
return false;
Iterator<Order> itL = lhs.orders.iterator();
Iterator<Order> itR = rhs.orders.iterator();
while (itL.hasNext() && itR.hasNext())
if (!equals(itL.next(), seenL, itR.next(), seenR, counter))
return false;
return true;
}
private static boolean equals(Order lhs,
Map<Object, Long> seenL,
Order rhs,
Map<Object, Long> seenR,
Counter counter) {
if (lhs == null || rhs == null)
return lhs == rhs;
Long countL = seenL.putIfAbsent(lhs, counter.value);
Long countR = seenR.putIfAbsent(rhs, counter.value);
if (countL != null || countR != null)
return Objects.equals(countL, countR);
counter.value++;
if (lhs == rhs)
return true;
if (!Objects.equals(lhs.id, rhs.id))
return false;
if (!Objects.equals(lhs.name, rhs.name))
return false;
return equals(lhs.client, seenL, rhs.client, seenR, counter);
}
}
I assume if you want to actually use that code, you'll need to alter it to use whatever getter naming format you're using and write a hashCode implementation. You'll also need to consider subtypes correctly if you're extending Client and Order.

How to update the Hibernate Entity Record when only few fields of entity obj are modified and remaining fields should not be null

is there any other different solution for this code.
for every pojo class we have to check that modified data coming from browser and we will store only modified data into the database.
see below billingTax obj is coming from browser which is updated data
and billingtaxDbObject obj is retrieved from database and we will check with if condition whether updated data is changed or not
if pojo class has 20 fields, we have to write 20 if conditions
if pojo class has 5 fields, we have to write 5 if conditions
instead of writing if conditions for checking wheter data is modified or nor is there any other simplest way?
#Override
public BillingTax update(BillingTax billingTax) throws DataInsufficientException, RecordNotFoundException {
log.debug("BillingTaxServiceImpl.update()....................");
try {
if (billingTax == null)
throw new DataInsufficientException("billingTax object is null");
BillingTax billingtaxDbObject = get(billingTax.getId());
if (billingtaxDbObject == null)
throw new RecordNotFoundException("billingTax object is not found in database");
if (billingTax.getTaxApplyType() != null
&& !billingTax.getTaxApplyType().equals(billingtaxDbObject.getTaxApplyType()))
billingtaxDbObject.setTaxApplyType(billingTax.getTaxApplyType());
if (billingTax.getCode() != null && !billingTax.getCode().trim().equalsIgnoreCase("null")
&& !billingTax.getCode().equalsIgnoreCase(billingtaxDbObject.getCode()))
billingtaxDbObject.setCode(billingTax.getCode());
if (billingTax.getName() != null && !billingTax.getName().trim().equalsIgnoreCase("null")
&& !billingTax.getName().equalsIgnoreCase(billingtaxDbObject.getName()))
billingtaxDbObject.setName(billingTax.getName());
if (billingTax.getDescription() != null && !billingTax.getDescription().trim().equalsIgnoreCase("null")
&& !billingTax.getDescription().equalsIgnoreCase(billingtaxDbObject.getDescription()))
billingtaxDbObject.setDescription(billingTax.getDescription());
if (billingTax.getServiceTypeForTax() != null
&& !billingTax.getServiceTypeForTax().equals(billingtaxDbObject.getServiceTypeForTax()))
billingtaxDbObject.setServiceTypeForTax(billingTax.getServiceTypeForTax());
if (billingTax.getTaxValue() != null && !billingTax.getTaxValue().equals("null")
&& !billingTax.getTaxValue().equals(billingtaxDbObject.getTaxValue()))
billingtaxDbObject.setTaxValue(billingTax.getTaxValue());
if (billingTax.getStatus() != null && !billingTax.getStatus().equals(billingtaxDbObject.getStatus()))
billingtaxDbObject.setStatus(billingTax.getStatus());
if (billingTax.getOrderNo() != null && !billingTax.getOrderNo().equals("null")
&& !billingTax.getOrderNo().equals(billingtaxDbObject.getOrderNo()))
billingtaxDbObject.setOrderNo(billingTax.getOrderNo());
if (billingTax.getId() != null && !billingTax.getId().trim().equalsIgnoreCase(billingtaxDbObject.getId())
&& !billingTax.getId().equalsIgnoreCase(billingtaxDbObject.getId()))
billingtaxDbObject.setId(billingTax.getId());
if (billingTax.getStartDate()!= null && !billingTax.getStartDate().equals(billingtaxDbObject.getStartDate()))
billingtaxDbObject.setStartDate(billingTax.getStartDate());
if (billingTax.getEndDate()!= null && !billingTax.getEndDate().equals(billingtaxDbObject.getEndDate()))
billingtaxDbObject.setEndDate(billingTax.getEndDate());
billingtaxDbObject.setUpdatedDate(new Date());
return billingTaxDAO.update(billingtaxDbObject);
} catch (Exception e) {
log.error("BillingTaxServiceImpl.update()....................exception:" + e.getMessage());
throw e;
}
}
You can do it with Dynamic updates for hibernate if you can avoid check changes among dto and entity and update all fields that come from web. If you need check dto from web and entity you can use apache bean util to find all changed values (or use spring util if you have or reflection from java...) and update it with dynamic updates.
see : BeanUtils
BeanUtils.copyProperties() // there are 3 methods.
check how it works in source code .
Create your own util method , similar to BeanUtils.copyProperties() , but with logic that you need (not null and not equal with source-entity value ).
Also use method from BeanUtils , to get PropertyDescriptor :
public static PropertyDescriptor[] getPropertyDescriptors(Class clazz)
throws BeansException
iterate over array of PropertyDescriptor and do check that you need (set value into source with ReflectionUtils).
with this approach you populate only properties that are not null and changed( if you need it) into billingtaxDbObject and update it.
You can put your copy / merge method into some util class and reuse it for all place where you need copy from dto into entity with some checks.
this is developed by getting all the fields/properties from the pojo class and checking with wheter null data or modified data.
below is the code:
copyProperties(billingTax, billingtaxDbObject);
public void copyProperties(BillingTax source, BillingTax dest) throws Exception{
if (source == null)
throw new DataInsufficientException("billingTax object is null");
if (dest == null)
throw new RecordNotFoundException("billingtaxDbObject object is not found in database");
try{
for (Field field : source.getClass().getDeclaredFields()) {
field.setAccessible(true);
Object sourceValue = field.get(source);
Object destValue=field.get(dest);
if(sourceValue!=null && sourceValue!="" && sourceValue!="null"){
if(sourceValue instanceof String){
if(!sourceValue.toString().trim().equalsIgnoreCase("null")){
if(!sourceValue.toString().equalsIgnoreCase(destValue.toString())){
field.set(dest, sourceValue);
System.err.println(field.getName()+" is modified:"+field.get(dest));
}
}
}
else{
if(!sourceValue.equals("null") && !sourceValue.equals(destValue)){
field.set(dest, sourceValue);
System.err.println(field.getName()+" is modified:"+field.get(dest));
}
}
}
}
System.out.println();
}catch (Exception e) {
throw e;
}
}
public class CopyConverter<T> {
private List<String> errorMessages = new ArrayList<>();
private int countSuccess = 0;
public CopyConverter<T> convert(T source, T target, Set<String> ignoreFields){
copyProperties(source,target,ignoreFields);
return this;
}
public boolean hasError(){
return errorMessages.isEmpty();
}
public List<String> getErrorMessages(){
return errorMessages;
}
private boolean copyProperties(T source ,T target , Set<String> ignoreFields) {
Objects.requireNonNull(source , "..error message...");
Objects.requireNonNull(target , "..error message...");
try {
Map<String, Field> fieldNameMapping = buildFiledMap(target.getClass().
getDeclaredFields());
ignoreFields = (ignoreFields == null ? new HashSet<>() : ignoreFields);
for (Map.Entry<String, Field> fieldEntry : fieldNameMapping.entrySet()) {
if (ignoreFields.contains(fieldEntry.getKey())) {
continue;
}
Field field = fieldEntry.getValue();
field.setAccessible(true);
Object sourceValue = field.get(source);
Object targetValue = field.get(source);
if (isChangedAsString(sourceValue, targetValue)) {
field.set(target, sourceValue);
continue;
}
if (isChangedAsObject(sourceValue, targetValue)) {
field.set(target, sourceValue);
}
countSuccess++;
}
} catch (Exception e) {
errorMessages.add(".......");
log.info(....);
}
return countSuccess == 0;
}
private Map<String, Field> buildFiledMap(Field[] fields) {
Map<String, Field> fieldMap = new HashMap<>(fields.length);
//Stream.of(fields).collect(Collectors.toMap())
for (Field field : fields) {
fieldMap.put(field.getName(), field);
}
return fieldMap;
}
private boolean isChangedAsObject(Object obj1, Object obj2) {
return (obj1 == null && obj2 != null) || (obj1 != null && !obj1.equals(obj1));
}
private boolean isChangedAsString(Object obj1, Object obj2) {
if (obj1 instanceof String && obj2 instanceof String) {
String value1 = (String) obj1;
String value2 = (String) obj2;
return value1 != null &&
!value1.trim().equalsIgnoreCase("null") &&//strange check
!value1.equalsIgnoreCase(value2);
}
return false;
}
}

Could not find java.beans.propertydescriptor on android

my app uses another projects which contains references to classes as java.beans.propertydescriptor which is not contained by android libraries.
The situation is the next: my project contains the .jar files with this another projects as libraries. I read that the solution is use an opensource class. I found it, but i dont know how can i add this class to the android .jar file. So, how can i add this class to the android java.beans library?
This is the class i have found:
package java.beans;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Vector;
import org.apache.harmony.beans.internal.nls.Messages;
public class PropertyDescriptor extends FeatureDescriptor {
private Method getter;
private Method setter;
private Class<?> propertyEditorClass;
private boolean constrained;
private boolean bound;
public PropertyDescriptor(String propertyName, Class<?> beanClass,
String getterName, String setterName) throws IntrospectionException {
super();
if (beanClass == null) {
throw new IntrospectionException(Messages.getString("beans.03")); //$NON-NLS-1$
}
if (propertyName == null || propertyName.length() == 0) {
throw new IntrospectionException(Messages.getString("beans.04")); //$NON-NLS-1$
}
this.setName(propertyName);
this.setDisplayName(propertyName);
if (setterName != null) {
if (hasMethod(beanClass, setterName)) {
setWriteMethod(beanClass, setterName);
} else {
throw new IntrospectionException(Messages.getString("beans.20")); //$NON-NLS-1$
}
}
if (getterName != null) {
if (hasMethod(beanClass, getterName)) {
setReadMethod(beanClass, getterName);
} else {
throw new IntrospectionException(Messages.getString("beans.1F")); //$NON-NLS-1$
}
}
}
public PropertyDescriptor(String propertyName, Method getter, Method setter)
throws IntrospectionException {
super();
if (propertyName == null || propertyName.length() == 0) {
throw new IntrospectionException(Messages.getString("beans.04")); //$NON-NLS-1$
}
this.setName(propertyName);
this.setDisplayName(propertyName);
setWriteMethod(setter);
setReadMethod(getter);
}
public PropertyDescriptor(String propertyName, Class<?> beanClass)
throws IntrospectionException {
String getterName;
String setterName;
if (beanClass == null) {
throw new IntrospectionException(Messages.getString("beans.03")); //$NON-NLS-1$
}
if (propertyName == null || propertyName.length() == 0) {
throw new IntrospectionException(Messages.getString("beans.04")); //$NON-NLS-1$
}
this.setName(propertyName);
this.setDisplayName(propertyName);
getterName = createDefaultMethodName(propertyName, "is"); //$NON-NLS-1$
if (hasMethod(beanClass, getterName)) {
setReadMethod(beanClass, getterName);
} else {
getterName = createDefaultMethodName(propertyName, "get"); //$NON-NLS-1$
if (hasMethod(beanClass, getterName)) {
setReadMethod(beanClass, getterName);
}
}
setterName = createDefaultMethodName(propertyName, "set"); //$NON-NLS-1$
if (hasMethod(beanClass, setterName)) {
setWriteMethod(beanClass, setterName);
}
if (getter == null && setter == null) {
throw new IntrospectionException(Messages.getString(
"beans.01", propertyName)); //$NON-NLS-1$
}
}
public void setWriteMethod(Method setter) throws IntrospectionException {
if (setter != null) {
int modifiers = setter.getModifiers();
if (!Modifier.isPublic(modifiers)) {
throw new IntrospectionException(Messages.getString("beans.05")); //$NON-NLS-1$
}
Class<?>[] parameterTypes = setter.getParameterTypes();
if (parameterTypes.length != 1) {
throw new IntrospectionException(Messages.getString("beans.06")); //$NON-NLS-1$
}
Class<?> parameterType = parameterTypes[0];
Class<?> propertyType = getPropertyType();
if (propertyType != null && !propertyType.equals(parameterType)) {
throw new IntrospectionException(Messages.getString("beans.07")); //$NON-NLS-1$
}
}
this.setter = setter;
}
public void setReadMethod(Method getter) throws IntrospectionException {
if (getter != null) {
int modifiers = getter.getModifiers();
if (!Modifier.isPublic(modifiers)) {
throw new IntrospectionException(Messages.getString("beans.0A")); //$NON-NLS-1$
}
Class<?>[] parameterTypes = getter.getParameterTypes();
if (parameterTypes.length != 0) {
throw new IntrospectionException(Messages.getString("beans.08")); //$NON-NLS-1$
}
Class<?> returnType = getter.getReturnType();
if (returnType.equals(Void.TYPE)) {
throw new IntrospectionException(Messages.getString("beans.33")); //$NON-NLS-1$
}
Class<?> propertyType = getPropertyType();
if ((propertyType != null) && !returnType.equals(propertyType)) {
throw new IntrospectionException(Messages.getString("beans.09")); //$NON-NLS-1$
}
}
this.getter = getter;
}
public Method getWriteMethod() {
return setter;
}
public Method getReadMethod() {
return getter;
}
#Override
public boolean equals(Object object) {
boolean result = (object != null && object instanceof PropertyDescriptor);
if (result) {
PropertyDescriptor pd = (PropertyDescriptor) object;
boolean gettersAreEqual = (this.getter == null)
&& (pd.getReadMethod() == null) || (this.getter != null)
&& (this.getter.equals(pd.getReadMethod()));
boolean settersAreEqual = (this.setter == null)
&& (pd.getWriteMethod() == null) || (this.setter != null)
&& (this.setter.equals(pd.getWriteMethod()));
boolean propertyTypesAreEqual = this.getPropertyType() == pd
.getPropertyType();
boolean propertyEditorClassesAreEqual = this
.getPropertyEditorClass() == pd.getPropertyEditorClass();
boolean boundPropertyAreEqual = this.isBound() == pd.isBound();
boolean constrainedPropertyAreEqual = this.isConstrained() == pd
.isConstrained();
result = gettersAreEqual && settersAreEqual
&& propertyTypesAreEqual && propertyEditorClassesAreEqual
&& boundPropertyAreEqual && constrainedPropertyAreEqual;
}
return result;
}
public void setPropertyEditorClass(Class<?> propertyEditorClass) {
this.propertyEditorClass = propertyEditorClass;
}
public Class<?> getPropertyType() {
Class<?> result = null;
if (getter != null) {
result = getter.getReturnType();
} else if (setter != null) {
Class<?>[] parameterTypes = setter.getParameterTypes();
result = parameterTypes[0];
}
return result;
}
public Class<?> getPropertyEditorClass() {
return propertyEditorClass;
}
public void setConstrained(boolean constrained) {
this.constrained = constrained;
}
public void setBound(boolean bound) {
this.bound = bound;
}
public boolean isConstrained() {
return constrained;
}
public boolean isBound() {
return bound;
}
boolean hasMethod(Class<?> beanClass, String methodName) {
Method[] methods = findMethods(beanClass, methodName);
return (methods.length > 0);
}
String createDefaultMethodName(String propertyName, String prefix) {
String result = null;
if (propertyName != null) {
String bos = propertyName.substring(0, 1).toUpperCase();
String eos = propertyName.substring(1, propertyName.length());
result = prefix + bos + eos;
}
return result;
}
Method[] findMethods(Class<?> aClass, String methodName) {
Method[] allMethods = aClass.getMethods();
Vector<Method> matchedMethods = new Vector<Method>();
Method[] result;
for (Method method : allMethods) {
if (method.getName().equals(methodName)) {
matchedMethods.add(method);
}
}
result = new Method[matchedMethods.size()];
for (int j = 0; j < matchedMethods.size(); ++j) {
result[j] = matchedMethods.elementAt(j);
}
return result;
}
void setReadMethod(Class<?> beanClass, String getterName) {
boolean result = false;
Method[] getters = findMethods(beanClass, getterName);
for (Method element : getters) {
try {
setReadMethod(element);
result = true;
} catch (IntrospectionException ie) {
}
if (result) {
break;
}
}
}
void setWriteMethod(Class<?> beanClass, String setterName)
throws IntrospectionException {
boolean result = false;
Method[] setters = findMethods(beanClass, setterName);
for (Method element : setters) {
try {
setWriteMethod(element);
result = true;
} catch (IntrospectionException ie) {
}
if (result) {
break;
}
}
}
public PropertyEditor createPropertyEditor(Object bean) {
PropertyEditor editor;
if (propertyEditorClass == null) {
return null;
}
if (!PropertyEditor.class.isAssignableFrom(propertyEditorClass)) {
// beans.48=Property editor is not assignable from the
// PropertyEditor interface
throw new ClassCastException(Messages.getString("beans.48")); //$NON-NLS-1$
}
try {
Constructor<?> constr;
try {
// try to look for the constructor with single Object argument
constr = propertyEditorClass.getConstructor(Object.class);
editor = (PropertyEditor) constr.newInstance(bean);
} catch (NoSuchMethodException e) {
// try no-argument constructor
constr = propertyEditorClass.getConstructor();
editor = (PropertyEditor) constr.newInstance();
}
} catch (Exception e) {
// beans.47=Unable to instantiate property editor
RuntimeException re = new RuntimeException(
Messages.getString("beans.47"), e); //$NON-NLS-1$
throw re;
}
return editor;
}
}
Thanks.
I downloaded from this page https://code.google.com/p/openbeans/downloads/detail?name=openbeans-1.0.jar the openbean.jar. Then, i imported this jar to the project and changed the references at imported library.
Then export again the project as .jar library and import in the android project.

Mass Java code file modifying - Remove methods, annotations, etc

I got a bundle of code from an very old project, which they generated many redundancy methods and annotations.
Is there anyway that fast, to remove -method "doOldThing()"- from all classes in this package; remove all #AnnotationObsoluted in all class?
I know that we can use search and replace, but writing regex to delete those take a long time. I guess we could have someway to parse a Java file, then remove "doOldThings()" method, check if #AnnotationObsoluted there then remove. Any idea? Tks.
In case anyone interest, here is the code for small util that I wrote to clean up methods (by name) and import statement as well.
import com.googlecode.java2objc.main.Config;
import com.googlecode.java2objc.main.Main;
import japa.parser.ASTHelper;
import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.ImportDeclaration;
import japa.parser.ast.body.*;
import japa.parser.ast.expr.AnnotationExpr;
import japa.parser.ast.type.ClassOrInterfaceType;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* User: lent
* Date: 10/01/2014
*/
public class CleanUpAndModelRegeneration
{
public static void main(String... args) throws IOException, ParseException
{
List<File> results =
listFilesForFolder(new File("\\LocationOfYourCode"), new ArrayList<File>());
List<String> javaFiles = new ArrayList<String>();
for (File codeFile : results)
{
// creates an input stream for the file to be parsed
FileInputStream in = new FileInputStream(codeFile);
CompilationUnit cu = null;
try
{
// parse the file
cu = JavaParser.parse(in);
removeMethod(cu, "toString");
removeMethod(cu, "append");
removeMethod(cu, "appendFields");
removeMethod(cu, "fromValue");
optimizeImport(cu);
cleanUpInterfaces("ToString", cu.getTypes());
cleanUpAnnotationForEnum(cu);
// prints the resulting compilation unit to default system output
System.out.println(cu.toString());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
in.close();
}
FileOutputStream fileOutputStream = new FileOutputStream(codeFile, false);
fileOutputStream.write(cu.toString().getBytes());
fileOutputStream.flush();
javaFiles.add(codeFile.toString());
}
try
{
Config config = new Config();
Main main = new Main(config, javaFiles);
main.execute();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void cleanUpAnnotationForEnum(CompilationUnit cu)
{
for (TypeDeclaration type : cu.getTypes())
{
if (type instanceof EnumDeclaration)
{
EnumDeclaration enumDeclaration = (EnumDeclaration) type;
if (enumDeclaration.getAnnotations() != null)
{
enumDeclaration.getAnnotations().clear();
}
for (BodyDeclaration member : enumDeclaration.getMembers())
{
List<AnnotationExpr> annotations = member.getAnnotations();
if (annotations != null)
{
annotations.clear();
}
}
for (EnumConstantDeclaration member : enumDeclaration.getEntries())
{
List<AnnotationExpr> annotations = member.getAnnotations();
if (annotations != null)
{
annotations.clear();
}
}
}
}
}
private static void cleanUpInterfaces(String interfaceName, List<? extends BodyDeclaration> types)
{
for (BodyDeclaration type : types)
{
cleanUpInterface(type, interfaceName);
if (type instanceof TypeDeclaration)
{
List<BodyDeclaration> members = ((TypeDeclaration) type).getMembers();
if (members == null)
{
continue;
}
for (BodyDeclaration body : members)
{
if (body instanceof ClassOrInterfaceDeclaration)
{
cleanUpInterface(body, interfaceName);
cleanUpInterfaces(interfaceName, ((ClassOrInterfaceDeclaration) body).getMembers());
}
}
}
}
}
private static void cleanUpInterface(BodyDeclaration typeDeclaration, String interfaceName)
{
if (typeDeclaration instanceof ClassOrInterfaceDeclaration)
{
List<ClassOrInterfaceType> interfaceTypes = ((ClassOrInterfaceDeclaration) typeDeclaration).getImplements();
if (interfaceTypes == null)
{
return;
}
List<ClassOrInterfaceType> toBeRemove = new ArrayList<ClassOrInterfaceType>();
for (ClassOrInterfaceType interfaceType : interfaceTypes)
{
if (interfaceType.toString().equals(interfaceName))
{
toBeRemove.add(interfaceType);
}
}
interfaceTypes.removeAll(toBeRemove);
}
}
private static void optimizeImport(CompilationUnit cu)
{
List<ImportDeclaration> toBeRemove = new ArrayList<ImportDeclaration>();
if (cu.getImports() == null)
{
return;
}
for (ImportDeclaration importDeclaration : cu.getImports())
{
String importPackageName = importDeclaration.getName().toString();
if (importPackageName.startsWith("java.io")
|| importPackageName.startsWith("javax.xml.datatype")
|| importPackageName.startsWith("java.util")
|| importPackageName.startsWith("java.math")
|| importPackageName.startsWith("java.text"))
{
continue;
}
toBeRemove.add(importDeclaration);
}
cu.getImports().removeAll(toBeRemove);
}
public static List<File> listFilesForFolder(final File folder, List<File> result)
{
for (final File fileEntry : folder.listFiles())
{
if (fileEntry.isDirectory())
{
listFilesForFolder(fileEntry, result);
}
else
{
if (result == null)
{
result = new ArrayList<File>();
}
result.add(fileEntry);
}
}
return result;
}
private static void removeMethod(CompilationUnit cu, String methodName)
{
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types)
{
List<BodyDeclaration> members = type.getMembers();
cleanUp(methodName, type, members);
if (type.getAnnotations() != null)
{
type.getAnnotations().clear();
}
type.setJavaDoc(null);
type.setComment(null);
}
}
private static void cleanUp(String methodName, BodyDeclaration type, List<BodyDeclaration> members)
{
List<BodyDeclaration> membersToRemove = new ArrayList<BodyDeclaration>();
for (BodyDeclaration member : members)
{
try
{
member.setJavaDoc(null);
member.setComment(null);
if (member.getAnnotations() != null)
{
member.getAnnotations().clear();
}
if (member instanceof MethodDeclaration && methodName.equals(((MethodDeclaration) member).getName()))
{
membersToRemove.add(member);
}
if (member instanceof ClassOrInterfaceDeclaration)
{
cleanUp(methodName, member, ((ClassOrInterfaceDeclaration) member).getMembers());
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
if (type instanceof TypeDeclaration)
{
((TypeDeclaration) type).getMembers().removeAll(membersToRemove);
}
else if (type instanceof ClassOrInterfaceDeclaration)
{
((ClassOrInterfaceDeclaration) type).getMembers().removeAll(membersToRemove);
}
else if (type instanceof EnumDeclaration)
{
((EnumDeclaration) type).getMembers().removeAll(membersToRemove);
}
}
private static void changeMethods(CompilationUnit cu)
{
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types)
{
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members)
{
if (member instanceof MethodDeclaration)
{
MethodDeclaration method = (MethodDeclaration) member;
changeMethod(method);
}
}
}
}
private static void changeMethod(MethodDeclaration n)
{
// change the name of the method to upper case
n.setName(n.getName().toUpperCase());
// create the new parameter
Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");
// add the parameter to the method
ASTHelper.addParameter(n, newArg);
}
}
If you erase the body of doOldThing():
doOldThing() {
}
And then inline the method, it should disappear wherever it is used. If instead you wanted to replace it with a call to doNewThing():
doOldThing() {
doNewThing();
}
and then inline.

How to programmatically read a Java interface?

I'd like to implement a method that returns the field(s) from an interface that define a specified (int) value. I don't have source to the interface.
So, the signature could be something like this:
public ArrayList<String> getFieldnames(Object src, int targetValue);
And I'm assuming internally it could find the declared fields and test each against the value, returning the list.
ArrayList<String> s = new ArrayList<String>();
if( src!= null )
{
Field[] flist = src.getClass().getDeclaredFields();
for (Field f : flist )
if( f.getType() == int.class )
try {
if( f.getInt(null) == targetValue) {
s.add(f.getName());
break;
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
return s;
Unfortunately, this implementation is incorrect - it's as if there are no fields at all when called with the interface itself. If I pass an object that implements the interface, the list of possible fields will be too wide to be of use.
Thanks for any help!
public ArrayList<String> getFieldnames(Object src, int targetValue) {
final Class<?> myInterfaceClass = MyInterface.class;
ArrayList<String> fieldNames = new ArrayList<>();
if (src != null) {
for (Class<?> currentClass = src.getClass(); currentClass != null; currentClass = currentClass.getSuperclass()) {
Class<?> [] interfaces = currentClass.getInterfaces();
if (Arrays.asList(interfaces).contains(myInterfaceClass)) {
for (Field field : currentClass.getDeclaredFields()) {
if (field.getType().equals(int.class)) {
try {
int value = field.getInt(null);
if (value == targetValue) {
fieldNames.add(field.getName());
}
} catch (IllegalAccessException ex) {
// Do nothing. Always comment empty blocks.
}
}
}
}
}
}
return fieldNames;
}
This
src.getClass()
returns src class not interface. Consider this
interface I {
}
class A implements I {
}
new A().getClass() -- returns A.class
Although I would rather have passed in an object, I suppose changing the signature to a string value and passing in the FQIN gets the job done just as well.
Thanks to <this question> for the idea (and Google for directing me there).
Solution:
public ArrayList<String> getFieldnamesByValue(Class<?>x, int targetValue)
{
ArrayList<String> s = new ArrayList<String>();
if( x != null )
{
Field[] flist = x.getDeclaredFields();
for (Field f : flist )
if( f.getType() == int.class )
try {
if( f.getInt(null) == targetValue) {
s.add(f.getName());
break;
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
return s;
}

Categories