Android Open Source - ormada Default Reflector






From Project

Back to project page ormada.

License

The source code is released under:

Copyright (c) 2012 Jesse Rosalia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...

If you think the Android project ormada listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.ormada.reflect;
/*  ww w.  j av  a2s.  c  om*/
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.ormada.util.Profiler;

/**
 * A utility class
 * 
 * @author thejenix
 *
 */
public class DefaultReflector implements Reflector {

    private Map<String, Method> reflCache = new HashMap<String, Method>();
    private Profiler addProf = new Profiler("Add", 100);
    private Profiler getProf = new Profiler("Get", 100);
    private Profiler setProf = new Profiler("Set", 100);

    /* (non-Javadoc)
     * @see org.ormada.Reflector#getFieldType(java.lang.Class, java.lang.String)
     */
    @Override
    public Class<?> getFieldType(Class<?> clazz, String field) throws SecurityException, NoSuchMethodException {
        return getGetter(clazz, field).getReturnType();
    }

    private String buildMethodName(String prefix, String fieldName) {
        return prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }

    /* (non-Javadoc)
     * @see org.ormada.Reflector#getGetter(java.lang.Class, java.lang.String)
     */
    @Override
    public Method getGetter(Class<?> clazz, String field) throws SecurityException, NoSuchMethodException {
        Method m = null;
        getProf.enter();
        String cacheKey = clazz.getCanonicalName() + "#get" + field;
        m = reflCache.get(cacheKey);
        if (m == null) {
            try {
                if (m == null) {
                    String name = buildMethodName("is", field);
                    m = clazz.getMethod(name);
                    reflCache.put(cacheKey, m);
                }
            } catch (NoSuchMethodException nme) {
                if (m == null) {
                    String name = buildMethodName("get", field);
                    m = clazz.getMethod(name);
                    reflCache.put(cacheKey, m);
                }
            }
            //let this one throw
        }
        getProf.exit();
        return m;
    }

    private String toSingular(String str) {
        int lastInx = str.length() - 1;
        //strip off the trailing s
        if ((str.charAt(lastInx) | 0x60) == 's') {
            lastInx--;
            //if the "singular" ends with an s, the plural will end with es...string that e off here
            if ((str.charAt(lastInx) | 0x60) == 'e') {
                lastInx--;
            }
        }
        return str.substring(0, lastInx + 1);
    }

    /* (non-Javadoc)
     * @see org.ormada.Reflector#getAdder(java.lang.Class, java.lang.String, java.lang.Class)
     */
    @Override
    public Method getAdder(Class<?> clazz, String field, Class<?> fieldType) throws SecurityException, NoSuchMethodException {
        addProf.enter();
        String cacheKey = clazz.getCanonicalName() + "#add" + field + "(" + fieldType.getCanonicalName() + ")";
        Method m = reflCache.get(cacheKey);
        if (m == null) {
            String name = buildMethodName("add", toSingular(field));
            m = clazz.getMethod(name, fieldType);
            reflCache.put(cacheKey, m);
        }
        
        addProf.exit();
        return m;
    }
    
    /* (non-Javadoc)
     * @see org.ormada.Reflector#getSetter(java.lang.Class, java.lang.String)
     */
    @Override
    public Method getSetter(Class<?> clazz, String field) throws SecurityException, NoSuchMethodException {
        setProf.enter();
        //NOTE: optimization here to eschew the valueClass from the cacheKey...this should speed things up
        // on android, but will also mean that an Entity class cannot have 2 setters with different parameters
        //...for a persisted field...this should not be an issue.
        String cacheKey = clazz.getCanonicalName() + "#set" + field + "()";
        Method m = reflCache.get(cacheKey);
        if (m == null) {
            Class<?> valueClass = getFieldType(clazz, field);
            String name = buildMethodName("set", field);
            m = clazz.getMethod(name, valueClass);
            reflCache.put(cacheKey, m);
        }
        setProf.exit();
        return m;
    }
}




Java Source Code List

org.andrormeda.dialect.SQLiteCursor.java
org.andrormeda.dialect.SQLiteDialect.java
org.andrormeda.dialect.SQLiteValueSet.java
org.andrormeda.example.AppDataSource.java
org.andrormeda.example.ExampleActivity.java
org.andrormeda.example.model.Cat.java
org.andrormeda.example.model.Kitten.java
org.ormada.ORMDataSource.java
org.ormada.annotations.OneToMany.java
org.ormada.annotations.Owner.java
org.ormada.annotations.Reference.java
org.ormada.annotations.Text.java
org.ormada.annotations.Transient.java
org.ormada.dialect.AStandardSQLDialect.java
org.ormada.dialect.DefaultValueSet.java
org.ormada.dialect.Dialect.java
org.ormada.dialect.ForwardOnlyResultSetCursor.java
org.ormada.dialect.FullResultSetCursor.java
org.ormada.dialect.QueryCursor.java
org.ormada.dialect.ValueSet.java
org.ormada.entity.EntityBuilder.java
org.ormada.entity.EntityCache.java
org.ormada.entity.EntityMetaData.java
org.ormada.entity.Entity.java
org.ormada.exception.MixedCollectionException.java
org.ormada.exception.UnableToOpenException.java
org.ormada.exception.UnsavedReferenceException.java
org.ormada.hsql.dialect.HSQLDialect.java
org.ormada.hsql.example.AppDataSource.java
org.ormada.hsql.example.ExampleMain.java
org.ormada.hsql.example.model.Cat.java
org.ormada.hsql.example.model.Kitten.java
org.ormada.model.ORMeta.java
org.ormada.reflect.DefaultReflector.java
org.ormada.reflect.Reflector.java
org.ormada.util.Profiler.java