001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.convert;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Modifier;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.directwebremoting.extend.Converter;
024: import org.directwebremoting.extend.InboundContext;
025: import org.directwebremoting.extend.MarshallException;
026: import org.directwebremoting.extend.Property;
027: import org.directwebremoting.extend.TypeHintContext;
028: import org.directwebremoting.impl.FieldProperty;
029:
030: /**
031: * Convert a Javascript associative array into a JavaBean
032: * @author Joe Walker [joe at getahead dot ltd dot uk]
033: */
034: public class ObjectConverter extends BasicObjectConverter implements
035: Converter {
036: /**
037: * Do we force accessibility for private fields
038: * @param force "true|false" to set the force accessibility flag
039: */
040: public void setForce(String force) {
041: this .force = Boolean.valueOf(force);
042: }
043:
044: /* (non-Javadoc)
045: * @see org.directwebremoting.extend.NamedConverter#getPropertyMapFromObject(java.lang.Object, boolean, boolean)
046: */
047: public Map<String, Property> getPropertyMapFromObject(
048: Object example, boolean readRequired, boolean writeRequired)
049: throws MarshallException {
050: Class<?> clazz = example.getClass();
051: return getPropertyMapFromClass(clazz, readRequired,
052: writeRequired);
053: }
054:
055: /* (non-Javadoc)
056: * @see org.directwebremoting.extend.NamedConverter#getPropertyMap(java.lang.Class, boolean, boolean)
057: */
058: public Map<String, Property> getPropertyMapFromClass(Class<?> type,
059: boolean readRequired, boolean writeRequired) {
060: Map<String, Property> allFields = new HashMap<String, Property>();
061:
062: while (type != Object.class) {
063: Field[] fields = type.getDeclaredFields();
064:
065: fieldLoop: for (Field field : fields) {
066: String name = field.getName();
067:
068: // We don't marshall getClass()
069: if ("class".equals(name)) {
070: continue fieldLoop;
071: }
072:
073: // Access rules mean we might not want to do this one
074: if (!isAllowedByIncludeExcludeRules(name)) {
075: continue fieldLoop;
076: }
077:
078: if (!Modifier.isPublic(field.getModifiers())) {
079: if (force) {
080: field.setAccessible(true);
081: } else {
082: continue fieldLoop;
083: }
084: }
085:
086: allFields.put(name, new FieldProperty(field));
087: }
088:
089: type = type.getSuperclass();
090: }
091:
092: return allFields;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.directwebremoting.convert.BasicObjectConverter#createTypeHintContext(org.directwebremoting.extend.InboundContext, org.directwebremoting.extend.Property)
097: */
098: @Override
099: protected TypeHintContext createTypeHintContext(
100: InboundContext inctx, Property property) {
101: return inctx.getCurrentTypeHintContext();
102: }
103:
104: /**
105: * Do we force accessibility for hidden fields
106: */
107: private boolean force = false;
108: }
|