Search in sources :

Example 96 with Constructor

use of java.lang.reflect.Constructor in project asterixdb by apache.

the class SimpleSerializerDeserializerTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() {
    Reflections reflections = new Reflections("org.apache.asterix.dataflow.data.nontagged.serde");
    Set<Class<? extends ISerializerDeserializer>> allClasses = reflections.getSubTypesOf(ISerializerDeserializer.class);
    for (Class<? extends ISerializerDeserializer> cl : allClasses) {
        String className = cl.getName();
        if (className.endsWith("ARecordSerializerDeserializer") || className.endsWith("AUnorderedListSerializerDeserializer") || className.endsWith("AOrderedListSerializerDeserializer") || className.endsWith("AStringSerializerDeserializer")) {
            // Serializer/Deserializer for complex types can have (immutable) states.
            continue;
        }
        // Verifies the class does not have non-static fields.
        for (Field field : cl.getDeclaredFields()) {
            if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
                throw new IllegalStateException("The serializer/deserializer " + cl.getName() + " is not stateless!");
            }
        }
        // Verifies the class follows the singleton pattern.
        for (Constructor constructor : cl.getDeclaredConstructors()) {
            if (!java.lang.reflect.Modifier.isPrivate(constructor.getModifiers())) {
                throw new IllegalStateException("The serializer/deserializer " + cl.getName() + " is not implemented as a singleton class!");
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 97 with Constructor

use of java.lang.reflect.Constructor in project lucene-solr by apache.

the class LookupBenchmarkTest method buildLookup.

/**
   * Create {@link Lookup} instance and populate it. 
   */
private Lookup buildLookup(Class<? extends Lookup> cls, Input[] input) throws Exception {
    Lookup lookup = null;
    try {
        lookup = cls.newInstance();
    } catch (InstantiationException e) {
        Analyzer a = new MockAnalyzer(random, MockTokenizer.KEYWORD, false);
        if (cls == AnalyzingInfixSuggester.class || cls == BlendedInfixSuggester.class) {
            Constructor<? extends Lookup> ctor = cls.getConstructor(Directory.class, Analyzer.class);
            lookup = ctor.newInstance(FSDirectory.open(createTempDir("LookupBenchmarkTest")), a);
        } else {
            Constructor<? extends Lookup> ctor = cls.getConstructor(Analyzer.class);
            lookup = ctor.newInstance(a);
        }
    }
    lookup.build(new InputArrayIterator(input));
    return lookup;
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Constructor(java.lang.reflect.Constructor) JaspellLookup(org.apache.lucene.search.suggest.jaspell.JaspellLookup) WFSTCompletionLookup(org.apache.lucene.search.suggest.fst.WFSTCompletionLookup) FSTCompletionLookup(org.apache.lucene.search.suggest.fst.FSTCompletionLookup) TSTLookup(org.apache.lucene.search.suggest.tst.TSTLookup) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 98 with Constructor

use of java.lang.reflect.Constructor in project geode by apache.

the class BlobHelperWithThreadContextClassLoaderTest method handlesObjectWithStateFromOtherClassLoader.

/**
   * Tests that the deserialized object has the correct state
   */
@Test
public void handlesObjectWithStateFromOtherClassLoader() throws Exception {
    Class loadedClass = Class.forName(CLASS_NAME_SERIALIZABLE_IMPL_WITH_VALUE, true, Thread.currentThread().getContextClassLoader());
    Constructor ctor = loadedClass.getConstructor(new Class[] { Object.class });
    Valuable instance = (Valuable) ctor.newInstance(new Object[] { 123 });
    assertThat(instance.getValue()).isEqualTo(123);
    byte[] bytes = BlobHelper.serializeToBlob(instance);
    Valuable object = (Valuable) BlobHelper.deserializeBlob(bytes);
    assertThat(object.getValue()).isEqualTo(instance.getValue());
}
Also used : Constructor(java.lang.reflect.Constructor) JavaClass(org.apache.bcel.classfile.JavaClass) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 99 with Constructor

use of java.lang.reflect.Constructor in project geode by apache.

the class BackwardCompatibilitySerializationDUnitTest method testAllMessages.

/**
   * Test if all messages implement toDataPreXXX and fromDataPreXXX if the message has been upgraded
   * in any of the versions
   * 
   * @throws Exception
   */
@Test
public void testAllMessages() throws Exception {
    // list of msgs not created using reflection
    // taken from DSFIDFactory.create()
    ArrayList<Integer> constdsfids = new ArrayList<Integer>();
    constdsfids.add(new Byte(DataSerializableFixedID.REGION).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.END_OF_STREAM_TOKEN).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.DLOCK_REMOTE_TOKEN).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.TRANSACTION_ID).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.INTEREST_RESULT_POLICY).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.UNDEFINED).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.RESULTS_BAG).intValue());
    constdsfids.add(new Byte(DataSerializableFixedID.GATEWAY_EVENT_IMPL_66).intValue());
    constdsfids.add(new Short(DataSerializableFixedID.TOKEN_INVALID).intValue());
    constdsfids.add(new Short(DataSerializableFixedID.TOKEN_LOCAL_INVALID).intValue());
    constdsfids.add(new Short(DataSerializableFixedID.TOKEN_DESTROYED).intValue());
    constdsfids.add(new Short(DataSerializableFixedID.TOKEN_REMOVED).intValue());
    constdsfids.add(new Short(DataSerializableFixedID.TOKEN_REMOVED2).intValue());
    constdsfids.add(new Short(DataSerializableFixedID.TOKEN_TOMBSTONE).intValue());
    for (int i = 0; i < 256; i++) {
        Constructor<?> cons = DSFIDFactory.getDsfidmap()[i];
        if (!constdsfids.contains(i - Byte.MAX_VALUE - 1) && cons != null) {
            Object ds = cons.newInstance((Object[]) null);
            checkSupportForRollingUpgrade(ds);
        }
    }
    // some msgs require distributed system
    Cache c = getCache();
    for (Object o : DSFIDFactory.getDsfidmap2().values()) {
        Constructor<?> cons = (Constructor<?>) o;
        if (cons != null) {
            DataSerializableFixedID ds = (DataSerializableFixedID) cons.newInstance((Object[]) null);
            checkSupportForRollingUpgrade(ds);
        }
    }
    c.close();
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 100 with Constructor

use of java.lang.reflect.Constructor in project geode by apache.

the class RollingUpgradeDUnitTest method createCache.

public static Object createCache(Properties systemProperties) throws Exception {
    // systemProperties.put(DistributionConfig.LOG_FILE_NAME,
    // "rollingUpgradeCacheVM" + VM.getCurrentVMNum() + ".log");
    Class distConfigClass = Thread.currentThread().getContextClassLoader().loadClass("org.apache.geode.distributed.internal.DistributionConfigImpl");
    boolean disableConfig = true;
    try {
        distConfigClass.getDeclaredField("useSharedConfiguration");
    } catch (NoSuchFieldException e) {
        disableConfig = false;
    }
    if (disableConfig) {
        systemProperties.put(DistributionConfig.USE_CLUSTER_CONFIGURATION_NAME, "false");
    }
    Class cacheFactoryClass = Thread.currentThread().getContextClassLoader().loadClass("org.apache.geode.cache.CacheFactory");
    Constructor constructor = cacheFactoryClass.getConstructor(Properties.class);
    constructor.setAccessible(true);
    Object cacheFactory = constructor.newInstance(systemProperties);
    Method createMethod = cacheFactoryClass.getMethod("create");
    createMethod.setAccessible(true);
    Object cache = null;
    cache = createMethod.invoke(cacheFactory);
    return cache;
}
Also used : Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Aggregations

Constructor (java.lang.reflect.Constructor)1314 InvocationTargetException (java.lang.reflect.InvocationTargetException)283 Method (java.lang.reflect.Method)253 IOException (java.io.IOException)128 Field (java.lang.reflect.Field)112 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