Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANNER-352 Add MethodMemberAccessor + delete ReadMethodAccessor hier…
…archy
- Loading branch information
Showing
15 changed files
with
233 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...re/src/main/java/org/optaplanner/core/impl/domain/common/member/MethodMemberAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2015 JBoss Inc | ||
* | ||
* 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 org.optaplanner.core.impl.domain.common.member; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
|
||
import org.optaplanner.core.impl.domain.common.ReflectionHelper; | ||
|
||
/** | ||
* A {@link MemberAccessor} based on a single read {@link Method}. | ||
* Do not confuse with {@link BeanPropertyMemberAccessor} which is richer. | ||
*/ | ||
public final class MethodMemberAccessor implements MemberAccessor { | ||
|
||
private final Class<?> returnType; | ||
private final String methodName; | ||
private final Method readMethod; | ||
|
||
public MethodMemberAccessor(Method readMethod) { | ||
this.readMethod = readMethod; | ||
readMethod.setAccessible(true); // Performance hack by avoiding security checks | ||
returnType = readMethod.getReturnType(); | ||
methodName = readMethod.getName(); | ||
if (readMethod.getParameterTypes().length != 0) { | ||
throw new IllegalArgumentException("The readMethod (" + readMethod + ") must not have any parameters (" | ||
+ Arrays.toString(readMethod.getParameterTypes()) + ")."); | ||
} | ||
if (readMethod.getReturnType() == void.class) { | ||
throw new IllegalArgumentException("The readMethod (" + readMethod + ") must have a return type (" | ||
+ readMethod.getReturnType() + ")."); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return methodName; | ||
} | ||
|
||
@Override | ||
public Class<?> getType() { | ||
return returnType; | ||
} | ||
|
||
@Override | ||
public Type getGenericType() { | ||
return readMethod.getGenericReturnType(); | ||
} | ||
|
||
public Object executeGetter(Object bean) { | ||
try { | ||
return readMethod.invoke(bean); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException("Cannot call property (" + methodName | ||
+ ") getterMethod (" + readMethod + ") on bean of class (" + bean.getClass() + ").", e); | ||
} catch (InvocationTargetException e) { | ||
throw new IllegalStateException("The property (" + methodName | ||
+ ") getterMethod (" + readMethod + ") on bean of class (" + bean.getClass() | ||
+ ") throws an exception.", | ||
e.getCause()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportSetter() { | ||
return false; | ||
} | ||
|
||
public void executeSetter(Object bean, Object value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
// ************************************************************************ | ||
// AnnotatedElement methods | ||
// ************************************************************************ | ||
|
||
@Override | ||
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { | ||
return readMethod.isAnnotationPresent(annotationClass); | ||
} | ||
|
||
@Override | ||
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { | ||
return readMethod.getAnnotation(annotationClass); | ||
} | ||
|
||
@Override | ||
public Annotation[] getAnnotations() { | ||
return readMethod.getAnnotations(); | ||
} | ||
|
||
@Override | ||
public Annotation[] getDeclaredAnnotations() { | ||
return readMethod.getDeclaredAnnotations(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "method " + methodName + " on class " + readMethod.getDeclaringClass(); | ||
} | ||
|
||
} |
61 changes: 0 additions & 61 deletions
61
...in/java/org/optaplanner/core/impl/domain/common/readmethod/DefaultReadMethodAccessor.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
.../src/main/java/org/optaplanner/core/impl/domain/common/readmethod/ReadMethodAccessor.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.