Search in sources :

Example 41 with Constructor

use of java.lang.reflect.Constructor in project android_frameworks_base by ResurrectionRemix.

the class MainActivityTest method createEncoder.

private Object createEncoder(ByteArrayOutputStream baos) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Class clazz = Class.forName("android.view.ViewHierarchyEncoder");
    Constructor constructor = clazz.getConstructor(ByteArrayOutputStream.class);
    return constructor.newInstance(baos);
}
Also used : Constructor(java.lang.reflect.Constructor)

Example 42 with Constructor

use of java.lang.reflect.Constructor in project android_frameworks_base by ResurrectionRemix.

the class DelegateClassAdapterTest method testDelegateInner.

@Test
public void testDelegateInner() throws Throwable {
    // We'll delegate the "get" method of both the inner and outer class.
    HashSet<String> delegateMethods = new HashSet<>();
    delegateMethods.add("get");
    delegateMethods.add("privateMethod");
    // Generate the delegate for the outer class.
    ClassWriter cwOuter = new ClassWriter(0);
    String outerClassName = OUTER_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cvOuter = new DelegateClassAdapter(mLog, cwOuter, outerClassName, delegateMethods);
    ClassReader cr = new ClassReader(OUTER_CLASS_NAME);
    cr.accept(cvOuter, 0);
    // Generate the delegate for the inner class.
    ClassWriter cwInner = new ClassWriter(0);
    String innerClassName = INNER_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cvInner = new DelegateClassAdapter(mLog, cwInner, innerClassName, delegateMethods);
    cr = new ClassReader(INNER_CLASS_NAME);
    cr.accept(cvInner, 0);
    // Load the generated classes in a different class loader and try them
    ClassLoader2 cl2 = null;
    try {
        cl2 = new ClassLoader2() {

            @Override
            public void testModifiedInstance() throws Exception {
                // Check the outer class
                Class<?> outerClazz2 = loadClass(OUTER_CLASS_NAME);
                Object o2 = outerClazz2.newInstance();
                assertNotNull(o2);
                // The original Outer.get returns 1+10+20,
                // but the delegate makes it return 4+10+20
                assertEquals(4 + 10 + 20, callGet(o2, 10, 20));
                assertEquals(1 + 10 + 20, callGet_Original(o2, 10, 20));
                // The original Outer has a private method,
                // so by default we can't access it.
                boolean gotIllegalAccessException = false;
                try {
                    callMethod(o2, "privateMethod", false);
                } catch (IllegalAccessException e) {
                    gotIllegalAccessException = true;
                }
                assertTrue(gotIllegalAccessException);
                // The private method from original Outer has been
                // delegated. The delegate generated should have the
                // same access.
                gotIllegalAccessException = false;
                try {
                    assertEquals("outerPrivateMethod", callMethod(o2, "privateMethod_Original", false));
                } catch (IllegalAccessException e) {
                    gotIllegalAccessException = true;
                }
                assertTrue(gotIllegalAccessException);
                // Check the inner class. Since it's not a static inner class, we need
                // to use the hidden constructor that takes the outer class as first parameter.
                Class<?> innerClazz2 = loadClass(INNER_CLASS_NAME);
                Constructor<?> innerCons = innerClazz2.getConstructor(outerClazz2);
                Object i2 = innerCons.newInstance(o2);
                assertNotNull(i2);
                // The original Inner.get returns 3+10+20,
                // but the delegate makes it return 6+10+20
                assertEquals(6 + 10 + 20, callGet(i2, 10, 20));
                assertEquals(3 + 10 + 20, callGet_Original(i2, 10, 20));
            }
        };
        cl2.add(OUTER_CLASS_NAME, cwOuter.toByteArray());
        cl2.add(INNER_CLASS_NAME, cwInner.toByteArray());
        cl2.testModifiedInstance();
    } catch (Throwable t) {
        throw dumpGeneratedClass(t, cl2);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassReader(org.objectweb.asm.ClassReader) OuterClass(com.android.tools.layoutlib.create.dataclass.OuterClass) StaticInnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.StaticInnerClass) InnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 43 with Constructor

use of java.lang.reflect.Constructor in project android_frameworks_base by ResurrectionRemix.

the class GenericInflater method createItem.

/**
     * Low-level function for instantiating by name. This attempts to
     * instantiate class of the given <var>name</var> found in this
     * inflater's ClassLoader.
     * 
     * <p>
     * There are two things that can happen in an error case: either the
     * exception describing the error will be thrown, or a null will be
     * returned. You must deal with both possibilities -- the former will happen
     * the first time createItem() is called for a class of a particular name,
     * the latter every time there-after for that class name.
     * 
     * @param name The full name of the class to be instantiated.
     * @param attrs The XML attributes supplied for this instance.
     * 
     * @return The newly instantied item, or null.
     */
public final T createItem(String name, String prefix, AttributeSet attrs) throws ClassNotFoundException, InflateException {
    Constructor constructor = (Constructor) sConstructorMap.get(name);
    try {
        if (null == constructor) {
            // Class not found in the cache, see if it's real,
            // and try to add it
            Class clazz = mContext.getClassLoader().loadClass(prefix != null ? (prefix + name) : name);
            constructor = clazz.getConstructor(mConstructorSignature);
            constructor.setAccessible(true);
            sConstructorMap.put(name, constructor);
        }
        Object[] args = mConstructorArgs;
        args[1] = attrs;
        return (T) constructor.newInstance(args);
    } catch (NoSuchMethodException e) {
        InflateException ie = new InflateException(attrs.getPositionDescription() + ": Error inflating class " + (prefix != null ? (prefix + name) : name));
        ie.initCause(e);
        throw ie;
    } catch (ClassNotFoundException e) {
        // If loadClass fails, we should propagate the exception.
        throw e;
    } catch (Exception e) {
        InflateException ie = new InflateException(attrs.getPositionDescription() + ": Error inflating class " + constructor.getClass().getName());
        ie.initCause(e);
        throw ie;
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InflateException(android.view.InflateException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) InflateException(android.view.InflateException)

Example 44 with Constructor

use of java.lang.reflect.Constructor in project ACS by ACS-Community.

the class Frame method loadPanel.

/**
	 * Load the panel whose class name is in the parameter
	 * 
	 * @param className The class name of the poanel to be shown
	 *                  in the main window
	 * @return A reference to the panel
	 */
private IPanel loadPanel(String className) throws PanelException {
    Thread t = Thread.currentThread();
    ClassLoader loader = t.getContextClassLoader();
    try {
        Class cl = loader.loadClass(className);
        Class[] classes = { JFrame.class };
        Constructor constructor = cl.getConstructor(classes);
        return (IPanel) constructor.newInstance((JFrame) this);
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        throw new PanelException("Error building " + className, throwable);
    }
}
Also used : JFrame(javax.swing.JFrame) Constructor(java.lang.reflect.Constructor)

Example 45 with Constructor

use of java.lang.reflect.Constructor in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ConstantsTest method testConsistentStructure.

@Test
public void testConsistentStructure() throws Exception {
    Class clazz = Constants.class;
    assertTrue("Class should be final", Modifier.isFinal(clazz.getModifiers()));
    Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
    assertEquals("Only one constructor should be present", 1, declaredConstructors.length);
    assertTrue("Constructor should be private", Modifier.isPrivate(declaredConstructors[0].getModifiers()));
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.isSynthetic()) {
            //ignore the jacoco generated field
            continue;
        }
        int modifiers = field.getModifiers();
        assertTrue("Field should be public", Modifier.isPublic(modifiers));
        assertTrue("Field should be static", Modifier.isStatic(modifiers));
        assertTrue("Field should be final", Modifier.isFinal(modifiers));
    }
    //the number of methods declared in the class source
    int originalMethodCount = 0;
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isSynthetic()) {
            //ignore the jacoco generated method
            continue;
        }
        originalMethodCount++;
    }
    assertTrue("There should be no methods declared", originalMethodCount == 0);
    Constructor constructor = declaredConstructors[0];
    constructor.setAccessible(true);
    constructor.newInstance();
}
Also used : Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

Constructor (java.lang.reflect.Constructor)1311 InvocationTargetException (java.lang.reflect.InvocationTargetException)281 Method (java.lang.reflect.Method)252 IOException (java.io.IOException)128 Field (java.lang.reflect.Field)111 ArrayList (java.util.ArrayList)106 Test (org.junit.Test)92 DOMTestDocumentBuilderFactory (org.w3c.domts.DOMTestDocumentBuilderFactory)74 JUnitTestSuiteAdapter (org.w3c.domts.JUnitTestSuiteAdapter)73 List (java.util.List)61 JAXPDOMTestDocumentBuilderFactory (org.w3c.domts.JAXPDOMTestDocumentBuilderFactory)58 Map (java.util.Map)50 Type (java.lang.reflect.Type)39 Annotation (java.lang.annotation.Annotation)38 HashMap (java.util.HashMap)38 HashSet (java.util.HashSet)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)31 ParameterizedType (java.lang.reflect.ParameterizedType)30 File (java.io.File)20 URL (java.net.URL)20