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!");
}
}
}
}
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;
}
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());
}
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();
}
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;
}
Aggregations