Skip to content

Instantly share code, notes, and snippets.

@hugithordarson
Created March 8, 2019 09:04
Show Gist options
  • Save hugithordarson/794c8da269fb5f5918e16bd3f7b99f17 to your computer and use it in GitHub Desktop.
Save hugithordarson/794c8da269fb5f5918e16bd3f7b99f17 to your computer and use it in GitHub Desktop.
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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 org.apache.cayenne.reflect;
import java.lang.reflect.Method;
/**
* A property accessor that uses set/get methods following JavaBean naming conventions.
*
* @since 1.2
*/
public class NonPrefixedBeanAccessor implements Accessor {
public static class Factory implements AccessorFactory {
@Override
public Accessor createAccessor( Class<?> objectClass, String propertyName, Class<?> propertyType ) {
return new NonPrefixedBeanAccessor( objectClass, propertyName, propertyType );
}
}
private static final long serialVersionUID = 606253801447018099L;
protected String propertyName;
protected Method readMethod;
protected Method writeMethod;
protected Object nullValue;
public NonPrefixedBeanAccessor( Class<?> objectClass, String propertyName, Class<?> propertyType ) {
this( objectClass, propertyName, propertyType, defaultIsGetterName( propertyName ), defaultGetGetterName( propertyName ), defaultSetterName( propertyName ) );
}
public NonPrefixedBeanAccessor( Class<?> objectClass, String propertyName, Class<?> propertyType, String isGetterName, String getGetterName, String setterName ) {
if( objectClass == null ) {
throw new IllegalArgumentException( "Null objectClass" );
}
if( propertyName == null ) {
throw new IllegalArgumentException( "Null propertyName" );
}
if( propertyName.length() == 0 ) {
throw new IllegalArgumentException( "Empty propertyName" );
}
this.propertyName = propertyName;
this.nullValue = PropertyUtils.defaultNullValueForType( propertyType );
Method[] publicMethods = objectClass.getMethods();
Method getter = null;
for( Method method : publicMethods ) {
Class<?> returnType = method.getReturnType();
// following Java Bean naming conventions, "is" methods are preferred over "get" methods
if( method.getName().equals( isGetterName ) && returnType.equals( Boolean.TYPE ) && method.getParameterTypes().length == 0 ) {
getter = method;
break;
}
// Find the method with the most specific return type.
// This is the same behavior as Class.getMethod(String, Class...) except that
// Class.getMethod prefers synthetic methods generated for interfaces
// over methods with more specific return types in a super class.
if( method.getName().equals( getGetterName ) && method.getParameterTypes().length == 0 ) {
if( returnType.isPrimitive() ) {
getter = returnType.equals( Void.TYPE ) ? null : method;
if( returnType.equals( Boolean.TYPE ) ) {
// keep looking for the "is" method
continue;
}
else {
// nothing more specific than a primitive, so stop here
break;
}
}
if( getter == null || getter.getReturnType().isAssignableFrom( returnType ) ) {
getter = method;
}
}
}
if( getter == null ) {
throw new IllegalArgumentException( "Property '" + propertyName + "' is not readable" );
}
this.readMethod = getter;
// TODO: compare 'propertyType' arg with readMethod.getReturnType()
for( Method method : publicMethods ) {
if( !method.getName().equals( setterName ) || !method.getReturnType().equals( Void.TYPE ) ) {
continue;
}
Class<?>[] parameterTypes = method.getParameterTypes();
if( parameterTypes.length != 1 ) {
continue;
}
if( getter.getReturnType().isAssignableFrom( parameterTypes[0] ) ) {
this.writeMethod = method;
break;
}
}
}
private static String defaultGetGetterName( String propertyName ) {
return propertyName;
}
private static String defaultIsGetterName( String propertyName ) {
return "is" + Character.toUpperCase( propertyName.charAt( 0 ) ) + propertyName.substring( 1 );
}
private static String defaultSetterName( String propertyName ) {
return "set" + Character.toUpperCase( propertyName.charAt( 0 ) ) + propertyName.substring( 1 );
}
@Override
public String getName() {
return propertyName;
}
/**
* @since 3.0
*/
@Override
public Object getValue( Object object ) throws PropertyException {
try {
return readMethod.invoke( object, (Object[])null );
}
catch( Throwable th ) {
throw new PropertyException( "Error reading property: " + propertyName, this, object, th );
}
}
/**
* @since 3.0
*/
@Override
@SuppressWarnings( { "rawtypes", "unchecked" } )
public void setValue( Object object, Object newValue ) throws PropertyException {
if( writeMethod == null ) {
throw new PropertyException( "Property '" + propertyName + "' is not writable", this, object );
}
Class type = writeMethod.getParameterTypes()[0];
Converter<?> converter = ConverterFactory.factory.getConverter( type );
try {
newValue = (converter != null) ? converter.convert( newValue, type ) : newValue;
// this will take care of primitives.
if( newValue == null ) {
newValue = this.nullValue;
}
writeMethod.invoke( object, newValue );
}
catch( Throwable th ) {
throw new PropertyException( "Error writing property: " + propertyName, this, object, th );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment