Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.cxf.jaxrs.ext.search;
 
 import java.util.Map;
 import java.util.Set;
 
Bean introspection utility.
 
 public class Beanspector<T> {
     private final MapClass< ? >, Class< ? > > primitiveWrappers = getPrimitiveWrappers();        
 
     private Class<T> tclass;
     private T tobj;
     private Map<StringMethodgetters = new LinkedHashMap<StringMethod>();
     private Map<StringMethodsetters = new LinkedHashMap<StringMethod>();
 
     public Beanspector(Class<T> tclass) {
         if (tclass == null) {
             throw new IllegalArgumentException("tclass is null");
         }
         this. = tclass;
         init();
     }
 
     public Beanspector(T tobj) {
         if (tobj == null) {
             throw new IllegalArgumentException("tobj is null");
         }
         this. = tobj;
         init();
     }
 
     @SuppressWarnings("unchecked")
     private void init() {
         if ( == null) {
              = (Class<T>).getClass();
         }
         for (Method m : .getMethods()) {
             if (isGetter(m)) {
                 .put(getPropertyName(m), m);
             } else if (isSetter(m)) {
                 .put(getPropertyName(m), m);
             }
         }
         // check type equality for getter-setter pairs
         Set<Stringpairs = new HashSet<String>(.keySet());
         pairs.retainAll(.keySet());
         for (String accessor : pairs) {
             Class<?> getterClass = .get(accessor).getReturnType();
             Class<?> setterClass = .get(accessor).getParameterTypes()[0];
             if (!getterClass.equals(setterClass)) {
                 throw new IllegalArgumentException(String
                     .format("Accessor '%s' type mismatch, getter type is %s while setter type is %s",
                             accessorgetterClass.getName(), setterClass.getName()));
             }
         }
     }
 
     public T getBean() {
         return ;
     }
 
     public Set<StringgetGettersNames() {
         return Collections.unmodifiableSet(.keySet());
     }
 
     public Set<StringgetSettersNames() {
         return Collections.unmodifiableSet(.keySet());
     }
 
     public TypeInfo getAccessorTypeInfo(String getterOrSetterNamethrows Exception {
        Method m = .get(getterOrSetterName.toLowerCase());
        if (m == null) {
            m = .get(getterOrSetterName);
        }
        if (m == null) {
            String msg = String.format("Accessor '%s' not found, "
                                       + "known setters are: %s, known getters are: %s"getterOrSetterName,
                                       .keySet(), .keySet());
            throw new IntrospectionException(msg);
        }
        return new TypeInfo(m.getReturnType(), m.getGenericReturnType(), 
            primitiveToWrapper(m.getReturnType()));
    }
    public Beanspector<T> swap(T newobjectthrows Exception {
        if (newobject == null) {
            throw new IllegalArgumentException("newobject is null");
        }
         = newobject;
        return this;
    }
    public Beanspector<T> instantiate() throws Exception {
         = .newInstance();
        return this;
    }
    public Beanspector<T> setValue(String setterNameObject valuethrows Throwable {
        Method m = .get(setterName.toLowerCase());
        if (m == null) {
            String msg = String.format("Setter '%s' not found, " + "known setters are: %s"setterName,
                                       .keySet());
            throw new IntrospectionException(msg);
        }
        setValue(mvalue);
        return this;
    }
    public Beanspector<T> setValue(Map<StringObjectsettersWithValuesthrows Throwable {
        for (Map.Entry<StringObjectentry : settersWithValues.entrySet()) {
            setValue(entry.getKey(), entry.getValue());
        }
        return this;
    }
    public Beanspector<T> setValue(Method setterObject valuethrows Throwable {
        Class<?> paramType = setter.getParameterTypes()[0];
        try {
            setter.invoke(value);
            return this;
        } catch (InvocationTargetException e) {
            throw e.getCause();
        } catch (IllegalArgumentException e) {
            String msg = String.format("; setter parameter type: %s, set value type: %s",
                                       paramType.getName(), value.getClass().getName());
            throw new IllegalArgumentException(e.getMessage() + msg);
        } catch (Exception e) {
            throw e;
        }
    }
    public Object getValue(String getterNamethrows Throwable {
        return getValue(.get(getterName));
    }
    public Object getValue(Method getterthrows Throwable {
        try {
            return getter.invoke();
        } catch (InvocationTargetException e) {
            throw e.getCause();
        } catch (Exception e) {
            throw e;
        }
    }
    private MapClass< ? >, Class< ? > > getPrimitiveWrappers() {
        final MapClass< ? >, Class< ? > > wrappers = new HashMapClass< ? >, Class< ? > >();
        
        wrappers.put(boolean.classBoolean.class);
        wrappers.put(byte.classByte.class);
        wrappers.put(char.classCharacter.class);
        wrappers.put(short.classShort.class);
        wrappers.put(int.classInteger.class);
        wrappers.put(long.classLong.class);
        wrappers.put(double.classDouble.class);
        wrappers.put(float.classFloat.class);
        
        return wrappers;
    }
    
    private Class< ? > primitiveToWrapper(final Class< ? > cls) {
        return cls.isPrimitive() ?  .get(cls) : cls;
    }
    
    private boolean isGetter(Method m) {
        return m.getParameterTypes().length == 0
               && (m.getName().startsWith("get") || m.getName().startsWith("is"));
    }
    private String getPropertyName(Method m) {
        // at this point the method is either getter or setter
        String result = m.getName().toLowerCase();
        if (result.startsWith("is")) {
            result = result.substring(2, result.length());
        } else {
            result = result.substring(3, result.length());
        }
        return result;
    }
    private boolean isSetter(Method m) {
        return m.getReturnType().equals(void.class) && m.getParameterTypes().length == 1
               && (m.getName().startsWith("set") || m.getName().startsWith("is"));
    }
    
    public static class TypeInfo {
        private Class<?> cls;
        // The wrapper class in case cls is a primitive class (byte, long, ...)
        private Class<?> wrapper
        private Type genericType;
        private CollectionCheckInfo checkInfo;
        
        public TypeInfo(Class<?> clsType genericType) {
            this(clsgenericTypecls);
        }
        
        public TypeInfo(Class<?> clsType genericTypeClass<?> wrapper) {
            this. = cls;
            this. = genericType;
            this. = wrapper;
        }
        
        public Class<?> getTypeClass() {
            return ;
        }
        
        public Class<?> getWrappedTypeClass() {
            return ;
        }
        
        public Type getGenericType() {
            return ;
        }
        public CollectionCheckInfo getCollectionCheckInfo() {
            return ;
        }
        public void setCollectionCheckInfo(CollectionCheckInfo info) {
            this. = info;
        }
    }
New to GrepCode? Check out our FAQ X