Search in sources :

Example 86 with Field

use of java.lang.reflect.Field in project dex2jar by pxb1988.

the class BaseCmd method initOptionFromClass.

protected void initOptionFromClass(Class<?> clz) {
    if (clz == null) {
        return;
    } else {
        initOptionFromClass(clz.getSuperclass());
    }
    Syntax syntax = clz.getAnnotation(Syntax.class);
    if (syntax != null) {
        this.cmdLineSyntax = syntax.syntax();
        this.cmdName = syntax.cmd();
        this.desc = syntax.desc();
        this.onlineHelp = syntax.onlineHelp();
    }
    Field[] fs = clz.getDeclaredFields();
    for (Field f : fs) {
        Opt opt = f.getAnnotation(Opt.class);
        if (opt != null) {
            f.setAccessible(true);
            Option option = new Option();
            option.field = f;
            option.description = opt.description();
            option.hasArg = opt.hasArg();
            option.required = opt.required();
            if ("".equals(opt.longOpt()) && "".equals(opt.opt())) {
                // into automode
                option.longOpt = fromCamel(f.getName());
                if (f.getType().equals(boolean.class)) {
                    option.hasArg = false;
                    try {
                        if (f.getBoolean(this)) {
                            throw new RuntimeException("the value of " + f + " must be false, as it is declared as no args");
                        }
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
                checkConflict(option, "--" + option.longOpt);
                continue;
            }
            if (!opt.hasArg()) {
                if (!f.getType().equals(boolean.class)) {
                    throw new RuntimeException("the type of " + f + " must be boolean, as it is declared as no args");
                }
                try {
                    if (f.getBoolean(this)) {
                        throw new RuntimeException("the value of " + f + " must be false, as it is declared as no args");
                    }
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
            boolean haveLongOpt = false;
            if (!"".equals(opt.longOpt())) {
                option.longOpt = opt.longOpt();
                checkConflict(option, "--" + option.longOpt);
                haveLongOpt = true;
            }
            if (!"".equals(opt.argName())) {
                option.argName = opt.argName();
            }
            if (!"".equals(opt.opt())) {
                option.opt = opt.opt();
                checkConflict(option, "-" + option.opt);
            } else {
                if (!haveLongOpt) {
                    throw new RuntimeException("opt or longOpt is not set in @Opt(...) " + f);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field)

Example 87 with Field

use of java.lang.reflect.Field in project spring-loaded by spring-projects.

the class ClassReflectionTests method assertField.

private void assertField(String expectedSignature, Result r) {
    Assert.assertEquals(Field.class, r.returnValue.getClass());
    Field f = (Field) r.returnValue;
    Assert.assertEquals(expectedSignature, f.toString());
}
Also used : Field(java.lang.reflect.Field)

Example 88 with Field

use of java.lang.reflect.Field in project spring-loaded by spring-projects.

the class SpringLoadedTests method getFieldAccessor.

protected ISMgr getFieldAccessor(Object o) {
    Class<?> clazz = o.getClass();
    try {
        Field f = clazz.getDeclaredField(Constants.fInstanceFieldsName);
        f.setAccessible(true);
        return (ISMgr) f.get(o);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) ISMgr(org.springsource.loaded.ISMgr) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ResultException(org.springsource.loaded.test.infra.ResultException)

Example 89 with Field

use of java.lang.reflect.Field in project spring-loaded by spring-projects.

the class GenerativeSpringLoadedTest method targetFieldFrom.

/**
	 * Select a field from given class's declared field as a test target.
	 * 
	 * @throws RejectedChoice If class has no fields to choose from.
	 */
protected Field targetFieldFrom(Class<?> clazz, FieldGetMethod howToGet) throws RejectedChoice {
    Field[] fields = null;
    switch(howToGet) {
        case GET_DECLARED_FIELD:
        case GET_DECLARED_FIELDS:
            fields = ReflectiveInterceptor.jlClassGetDeclaredFields(clazz);
            break;
        case GET_FIELD:
        case GET_FIELDS:
            fields = ReflectiveInterceptor.jlClassGetFields(clazz);
            break;
    }
    //To be deterministic we must sort these in a predictable fashion! 
    //		Arrays.sort(fields, new ToStringComparator());
    sort(fields);
    Field f = choice(fields);
    toStringValue.append(f.getName());
    try {
        switch(howToGet) {
            case GET_DECLARED_FIELDS:
            case GET_FIELDS:
                return f;
            case GET_DECLARED_FIELD:
                return ReflectiveInterceptor.jlClassGetDeclaredField(clazz, f.getName());
            case GET_FIELD:
                return ReflectiveInterceptor.jlClassGetField(clazz, f.getName());
        }
    } catch (Exception e) {
        throw new Error(e);
    }
    return f;
}
Also used : Field(java.lang.reflect.Field)

Example 90 with Field

use of java.lang.reflect.Field in project platform_frameworks_base by android.

the class ContentProviderOperationTest method operationGetValues.

private ContentValues operationGetValues(ContentProviderOperation operation) throws NoSuchFieldException, IllegalAccessException {
    final Field field = CLASS_OPERATION.getDeclaredField("mValues");
    field.setAccessible(true);
    return (ContentValues) field.get(operation);
}
Also used : ContentValues(android.content.ContentValues) Field(java.lang.reflect.Field)

Aggregations

Field (java.lang.reflect.Field)5144 Method (java.lang.reflect.Method)613 Test (org.junit.Test)538 ArrayList (java.util.ArrayList)490 IOException (java.io.IOException)318 HashMap (java.util.HashMap)318 Map (java.util.Map)296 InvocationTargetException (java.lang.reflect.InvocationTargetException)176 List (java.util.List)171 Pattern (java.util.regex.Pattern)122 Matcher (java.util.regex.Matcher)117 HashSet (java.util.HashSet)109 File (java.io.File)107 Annotation (java.lang.annotation.Annotation)98 Support_Field (tests.support.Support_Field)82 Collection (java.util.Collection)81 Test (org.testng.annotations.Test)68 Type (java.lang.reflect.Type)67 SienaException (siena.SienaException)65 Before (org.junit.Before)62