Search in sources :

Example 46 with Field

use of java.lang.reflect.Field in project BGABanner-Android by bingoogolapple.

the class BGAViewPager method setPageChangeDuration.

/**
     * 设置调用setCurrentItem(int item, boolean smoothScroll)方法时,page切换的时间长度
     *
     * @param duration page切换的时间长度
     */
public void setPageChangeDuration(int duration) {
    try {
        Field scrollerField = ViewPager.class.getDeclaredField("mScroller");
        scrollerField.setAccessible(true);
        scrollerField.set(this, new BGABannerScroller(getContext(), duration));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Field(java.lang.reflect.Field)

Example 47 with Field

use of java.lang.reflect.Field in project BGABanner-Android by bingoogolapple.

the class BGAViewPager method setPageTransformer.

@Override
public void setPageTransformer(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer) {
    /**
         继承ViewPager,重写setPageTransformer方法,移除版本限制,通过反射设置参数和方法

         getDeclaredMethod*()获取的是【类自身】声明的所有方法,包含public、protected和private方法。
         getMethod*()获取的是类的所有共有方法,这就包括自身的所有【public方法】,和从基类继承的、从接口实现的所有【public方法】。

         getDeclaredField获取的是【类自身】声明的所有字段,包含public、protected和private字段。
         getField获取的是类的所有共有字段,这就包括自身的所有【public字段】,和从基类继承的、从接口实现的所有【public字段】。
         */
    Class viewpagerClass = ViewPager.class;
    try {
        boolean hasTransformer = transformer != null;
        Field pageTransformerField = viewpagerClass.getDeclaredField("mPageTransformer");
        pageTransformerField.setAccessible(true);
        PageTransformer mPageTransformer = (PageTransformer) pageTransformerField.get(this);
        boolean needsPopulate = hasTransformer != (mPageTransformer != null);
        pageTransformerField.set(this, transformer);
        Method setChildrenDrawingOrderEnabledCompatMethod = viewpagerClass.getDeclaredMethod("setChildrenDrawingOrderEnabledCompat", boolean.class);
        setChildrenDrawingOrderEnabledCompatMethod.setAccessible(true);
        setChildrenDrawingOrderEnabledCompatMethod.invoke(this, hasTransformer);
        Field drawingOrderField = viewpagerClass.getDeclaredField("mDrawingOrder");
        drawingOrderField.setAccessible(true);
        if (hasTransformer) {
            drawingOrderField.setInt(this, reverseDrawingOrder ? 2 : 1);
        } else {
            drawingOrderField.setInt(this, 0);
        }
        if (needsPopulate) {
            Method populateMethod = viewpagerClass.getDeclaredMethod("populate");
            populateMethod.setAccessible(true);
            populateMethod.invoke(this);
        }
    } catch (Exception e) {
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) ViewPager(android.support.v4.view.ViewPager)

Example 48 with Field

use of java.lang.reflect.Field in project bazel by bazelbuild.

the class RClassGeneratorTest method checkInnerClass.

private void checkInnerClass(Path baseDir, String expectedClassName, Class<?> outerClass, ImmutableMap<String, Integer> intFields, ImmutableMap<String, List<Integer>> intArrayFields, boolean areFieldsFinal) throws Exception {
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { baseDir.toUri().toURL() });
    Class<?> innerClass = urlClassLoader.loadClass(expectedClassName);
    assertThat(innerClass.getSuperclass()).isEqualTo(Object.class);
    assertThat(innerClass.getEnclosingClass().toString()).isEqualTo(outerClass.toString());
    ImmutableMap.Builder<String, Integer> actualIntFields = ImmutableMap.builder();
    ImmutableMap.Builder<String, List<Integer>> actualIntArrayFields = ImmutableMap.builder();
    for (Field f : innerClass.getFields()) {
        int fieldModifiers = f.getModifiers();
        assertThat(Modifier.isFinal(fieldModifiers)).isEqualTo(areFieldsFinal);
        assertThat(Modifier.isPublic(fieldModifiers)).isTrue();
        assertThat(Modifier.isStatic(fieldModifiers)).isTrue();
        Class<?> fieldType = f.getType();
        if (fieldType.isPrimitive()) {
            assertThat(fieldType).isEqualTo(Integer.TYPE);
            actualIntFields.put(f.getName(), (Integer) f.get(null));
        } else {
            assertThat(fieldType.isArray()).isTrue();
            int[] asArray = (int[]) f.get(null);
            ImmutableList.Builder<Integer> list = ImmutableList.builder();
            for (int i : asArray) {
                list.add(i);
            }
            actualIntArrayFields.put(f.getName(), list.build());
        }
    }
    assertThat(actualIntFields.build()).containsExactlyEntriesIn(intFields);
    assertThat(actualIntArrayFields.build()).containsExactlyEntriesIn(intArrayFields);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Field(java.lang.reflect.Field) URLClassLoader(java.net.URLClassLoader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 49 with Field

use of java.lang.reflect.Field in project bazel by bazelbuild.

the class OptionsParser method describeOptionsHtml.

/**
   * Returns a description of all the options this parser can digest.
   * In addition to {@link Option} annotations, this method also
   * interprets {@link OptionsUsage} annotations which give an intuitive short
   * description for the options.
   *
   * @param categoryDescriptions a mapping from category names to category
   *   descriptions.  Options of the same category (see {@link
   *   Option#category}) will be grouped together, preceded by the description
   *   of the category.
   */
public String describeOptionsHtml(Map<String, String> categoryDescriptions, Escaper escaper) {
    StringBuilder desc = new StringBuilder();
    if (!impl.getOptionsClasses().isEmpty()) {
        List<Field> allFields = Lists.newArrayList();
        for (Class<? extends OptionsBase> optionsClass : impl.getOptionsClasses()) {
            allFields.addAll(impl.getAnnotatedFieldsFor(optionsClass));
        }
        Collections.sort(allFields, OptionsUsage.BY_CATEGORY);
        String prevCategory = null;
        for (Field optionField : allFields) {
            String category = optionField.getAnnotation(Option.class).category();
            DocumentationLevel level = documentationLevel(category);
            if (!category.equals(prevCategory) && level == DocumentationLevel.DOCUMENTED) {
                String description = categoryDescriptions.get(category);
                if (description == null) {
                    description = "Options category '" + category + "'";
                }
                if (prevCategory != null) {
                    desc.append("</dl>\n\n");
                }
                desc.append(escaper.escape(description)).append(":\n");
                desc.append("<dl>");
                prevCategory = category;
            }
            if (level == DocumentationLevel.DOCUMENTED) {
                OptionsUsage.getUsageHtml(optionField, desc, escaper);
            }
        }
        desc.append("</dl>\n");
    }
    return desc.toString();
}
Also used : Field(java.lang.reflect.Field)

Example 50 with Field

use of java.lang.reflect.Field in project bazel by bazelbuild.

the class OptionsParserImpl method clearValue.

void clearValue(String optionName, Map<String, OptionValueDescription> clearedValues) throws OptionsParsingException {
    Field field = optionsData.getFieldFromName(optionName);
    if (field == null) {
        throw new IllegalArgumentException("No such option '" + optionName + "'");
    }
    Option option = field.getAnnotation(Option.class);
    clearValue(field, option, clearedValues);
}
Also used : 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