Skip to content

Commit

Permalink
DUBBO-627 扩展泛化调用和实现
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi committed Dec 25, 2012
1 parent efcb0f3 commit 570a062
Show file tree
Hide file tree
Showing 5 changed files with 1,005 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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 com.alibaba.dubbo.common.beanutil;

/**
* @author <a href="mailto:[email protected]">kimi</a>
*/
public enum JavaBeanAccessor {

/** Field accessor. */
FIELD,
/** Method accessor.*/
METHOD,
/** Method prefer to field. */
ALL;

public static boolean isAccessByMethod(JavaBeanAccessor accessor) {
return METHOD.equals(accessor) || ALL.equals(accessor);
}

public static boolean isAccessByField(JavaBeanAccessor accessor) {
return FIELD.equals(accessor) || ALL.equals(accessor);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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 com.alibaba.dubbo.common.beanutil;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* @author <a href="mailto:[email protected]">kimi</a>
*/
public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entry<Object, Object>> {

private static final long serialVersionUID = -8505586483570518029L;

public static final int TYPE_CLASS = 1;

public static final int TYPE_ENUM = 2;

public static final int TYPE_COLLECTION = 3;

public static final int TYPE_MAP = 4;

public static final int TYPE_ARRAY = 5;

/** @see com.alibaba.dubbo.common.utils.ReflectUtils#isPrimitive(Class) */
public static final int TYPE_PRIMITIVE = 6;

public static final int TYPE_BEAN = 7;

public static final String ENUM_PROPERTY_NAME = "name";

public static final String CLASS_PROPERTY_NAME = "name";

public static final String PRIMITIVE_PROPERTY_VALUE = "value";

/**
* Used to define a type is valid.
* @see #isValidType(int)
*/
private static final int TYPE_MAX = TYPE_BEAN;

/**
* Used to define a type is valid.
* @see #isValidType(int)
*/
private static final int TYPE_MIN = TYPE_CLASS;

private final String className;

private final int type;

private Map<Object, Object> properties = new LinkedHashMap<Object, Object>();

public JavaBeanDescriptor(String className, int type) {
notEmpty(className, "class name is empty");
if (!isValidType(type)) {
throw new IllegalArgumentException(
new StringBuilder(16).append("type [ ")
.append(type).append(" ] is unsupported").toString());
}

this.className = className;
this.type = type;
}

public boolean isClassType() {
return TYPE_CLASS == type;
}

public boolean isEnumType() {
return TYPE_ENUM == type;
}

public boolean isCollectionType() {
return TYPE_COLLECTION == type;
}

public boolean isMapType() {
return TYPE_MAP == type;
}

public boolean isArrayType() {
return TYPE_ARRAY == type;
}

public boolean isPrimitiveType() {
return TYPE_PRIMITIVE == type;
}

public boolean isBeanType() {
return TYPE_BEAN == type;
}

public int getType() {
return type;
}

public String getClassName() {
return className;
}

public Object setProperty(Object propertyName, Object propertyValue) {
notNull(propertyName, "Property name is null");

Object oldValue = properties.put(propertyName, propertyValue);
return oldValue;
}

public Object setPrimitive(Object primitiveValue) {
if (isPrimitiveType()) {
return setProperty(PRIMITIVE_PROPERTY_VALUE, primitiveValue);
}
throw new IllegalStateException("The instance is not a primitive type wrapper");
}

public Object getPrimitive() {
if (isPrimitiveType()) {
return getProperty(PRIMITIVE_PROPERTY_VALUE);
}
throw new IllegalStateException("The instance is not a primitive type wrapper");
}

public Object getProperty(Object propertyName) {
notNull(propertyName, "Property name is null");
Object propertyValue = properties.get(propertyName);
return propertyValue;
}

public boolean containsProperty(Object propertyName) {
notNull(propertyName, "Property name is null");
return properties.containsKey(propertyName);
}

public Iterator<Map.Entry<Object, Object>> iterator() {
return properties.entrySet().iterator();
}

public int propertySize() {
return properties.size();
}

private boolean isValidType(int type) {
return TYPE_MIN <= type && type <= TYPE_MAX;
}

private void notNull(Object obj, String message) {
if (obj == null) {
throw new IllegalArgumentException(message);
}
}

private void notEmpty(String string, String message) {
if (isEmpty(string)) {
throw new IllegalArgumentException(message);
}
}

private boolean isEmpty(String string) {
return string == null || "".equals(string.trim());
}
}
Loading

0 comments on commit 570a062

Please sign in to comment.