List of usage examples for java.lang Class getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:de.alpharogroup.lang.PropertiesUtils.java
/** * Load properties.//ww w. j a v a 2 s .c o m * * @param clazz * the clazz * @param name * the package path with the file name * @return the properties * @throws IOException * Signals that an I/O exception has occurred. */ public static Properties loadProperties(final Class<?> clazz, final String name) throws IOException { Properties properties = loadProperties(name); if (properties == null) { final InputStream is = ClassExtensions.getResourceAsStream(clazz.getClass(), name); if (is != null) { properties = new Properties(); properties.load(is); } } return properties; }
From source file:com.espertech.esper.util.MethodResolver.java
private static void logWarnBoxedToPrimitiveType(Class declaringClass, String methodName, Method bestMatch, Class[] paramTypes) {//from www. j a va2 s .co m Class[] parametersMethod = bestMatch.getParameterTypes(); for (int i = 0; i < parametersMethod.length; i++) { if (!parametersMethod[i].isPrimitive()) { continue; } // if null-type parameter, or non-JDK class and boxed type matches if (paramTypes[i] == null || (!declaringClass.getClass().getName().startsWith("java") && (JavaClassHelper.getBoxedType(parametersMethod[i])) == paramTypes[i])) { String paramTypeStr = paramTypes[i] == null ? "null" : paramTypes[i].getSimpleName(); log.info("Method '" + methodName + "' in class '" + declaringClass.getName() + "' expects primitive type '" + parametersMethod[i] + "' as parameter " + i + ", but receives a nullable (boxed) type " + paramTypeStr + ". This may cause null pointer exception at runtime if the actual value is null, please consider using boxed types for method parameters."); return; } } }
From source file:org.openmrs.module.openhmis.cashier.api.ReceiptNumberGeneratorFactory.java
private static IReceiptNumberGenerator createGeneratorInstance() { Class<? super IReceiptNumberGenerator> cls = null; try {// w ww. j a va 2s.co m cls = FactoryImpl.INSTANCE.getGeneratorClass(); if (cls == null) { return null; } generator = (IReceiptNumberGenerator) cls.newInstance(); return generator; } catch (ClassNotFoundException classEx) { LOG.warn("Attempt to load unknown receipt number generator type", classEx); throw new APIException("Could not locate receipt number generator class.", classEx); } catch (InstantiationException instantiationEx) { throw new APIException("Could not instantiate the '" + cls.getClass().getName() + "' class.", instantiationEx); } catch (IllegalAccessException accessEx) { throw new APIException("Could not access the '" + cls.getClass().getName() + "' class.", accessEx); } }
From source file:Main.java
private static Object getRandomValue(Class<?> type) { if (type.isAssignableFrom(Double.class)) { return new Random().nextDouble(); }/*from w w w .j a v a 2s . co m*/ if (type.isAssignableFrom(Short.class)) { return (short) new Random().nextInt(); } if (type.isAssignableFrom(Long.class)) { return new Random().nextLong(); } if (type.isAssignableFrom(Integer.class)) { return new Random().nextInt(); } if (type.isAssignableFrom(String.class)) { return "" + new Random().nextInt(); } if (type.isAssignableFrom(Boolean.class)) { return new Random().nextBoolean(); } if (type.isAssignableFrom(Date.class)) { return new Date(new Random().nextLong()); } if (type.isAssignableFrom(List.class)) { Class<?> listType = (Class<?>) ((ParameterizedType) type.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; return getRandomCollection(listType); } return null; }
From source file:org.apache.axis2.metadata.registry.MetadataFactoryRegistry.java
/** * This method will load a file, if it exists, that contains a list * of interfaces and custom implementations. This allows for non- * programmatic registration of custom interface implementations * with the MDQ layer./*from ww w. j ava2 s. co m*/ */ private static void loadConfigFromFile() { String pairSeparator = "|"; try { ClassLoader classLoader = getContextClassLoader(null); URL url = null; url = classLoader.getResource(configurationFileLoc); if (url == null) { File file = new File(configurationFileLoc); url = file.toURL(); } // the presence of this file is optional if (url != null) { if (log.isDebugEnabled()) { log.debug("Found URL to MetadataFactoryRegistry configuration file: " + configurationFileLoc); } BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); String line = reader.readLine(); // the separator of the file is the '|' character // to the left of the separator will be the interface and // to the right will be the custom implementation if (line != null && line.indexOf("|") != -1) { String interfaceName = line.substring(0, line.indexOf(pairSeparator)); String implName = line.substring(line.indexOf(pairSeparator) + 1, line.length()); if (log.isDebugEnabled()) { log.debug("For registered class: " + interfaceName + " the " + "following implementation was found: " + implName); } Class intf = classLoader.loadClass(interfaceName); Class impl = classLoader.loadClass(implName); // if we could load both we need to register them with our // internal registry if (intf != null && impl != null) { if (log.isDebugEnabled()) { log.debug("Loaded both interface and implementation class: " + interfaceName + ":" + implName); } if (impl.getEnclosingClass() == null) { table.put(intf, impl.newInstance()); } else { if (log.isWarnEnabled()) { log.warn("The implementation class: " + impl.getClass().getName() + " could not be lregistered because it is an inner class. " + "In order to register file-based overrides, implementations " + "must be public outer classes."); } } } else { if (log.isDebugEnabled()) { log.debug("Could not load both interface and implementation class: " + interfaceName + ":" + implName); } } } else { if (log.isDebugEnabled()) { log.debug("Did not find File for MetadataFactoryRegistry configuration " + "file: " + configurationFileLoc); } } } else { if (log.isDebugEnabled()) { log.debug("Did not find URL for MetadataFactoryRegistry configuration " + "file: " + configurationFileLoc); } } } catch (Throwable t) { if (log.isDebugEnabled()) { log.debug("The MetadataFactoryRegistry could not process the configuration file: " + configurationFileLoc + " because of the following error: " + t.toString()); } } }
From source file:verdandi.event.EventBoard.java
public void addListener(VerdandiEventListener newListener, Class eventType) { eventType.getClass(); Set<VerdandiEventListener> specificListeners = listeners.get(eventType); if (specificListeners == null) { specificListeners = new HashSet<VerdandiEventListener>(); listeners.put(eventType, specificListeners); }/*from w w w . j a va 2 s . c om*/ specificListeners.add(newListener); }
From source file:com.qmetry.qaf.automation.ui.webdriver.QAFWebComponent.java
@SuppressWarnings("unchecked") public <T extends QAFWebComponent> T findElement(String loc, Class<T> t) { T obj = (T) ComponentFactory.getObject(t.getClass(), loc, this, this); obj.getId();// w w w .j av a 2 s.com return obj; }
From source file:com.xiovr.unibot.plugin.impl.PluginLoaderImpl.java
@Override public void unloadPlugin(Class<?> _class) { try {//from w w w .ja v a 2 s . co m URLClassLoader cl = (URLClassLoader) _class.getClass().getClassLoader(); cl.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.sprintapi.hyperdata.gson.HyperDataAdapterFactory.java
@SuppressWarnings("unchecked") @Override/*from www.j a va 2s .c om*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { Class<? super T> raw = type.getRawType(); if (!Object.class.isAssignableFrom(raw) || HyperMap.class.isAssignableFrom(raw)) { return null; // it's a primitive or HyperMap } if (!raw.isAnnotationPresent(HyperdataContainer.class)) { return null; // it's not hyperdata } MetadataAccess metadataAccess = new MetadataAccess(); Set<String> profiles = new LinkedHashSet<String>(); Class<?> c = raw; while (c != null) { if (c.getClass().equals(Object.class)) { break; } HyperdataContainer hdc = c.getAnnotation(HyperdataContainer.class); if ((hdc != null) && (hdc.profile().length > 0)) { profiles.addAll(Arrays.asList(hdc.profile())); } Class<?>[] interfaces = c.getInterfaces(); if (interfaces != null) { for (Class<?> i : interfaces) { hdc = i.getAnnotation(HyperdataContainer.class); if ((hdc != null) && (hdc.profile().length > 0)) { profiles.addAll(Arrays.asList(hdc.profile())); } } } c = c.getSuperclass(); } metadataAccess.profile = profiles.toArray(new String[0]); for (Method method : raw.getMethods()) { if (method.isAnnotationPresent(MetadataContainer.class)) { if (method.getName().startsWith("get")) { metadataAccess.getter = method; metadataAccess.fieldName = method.getName().substring(3); } else if (method.getName().startsWith("set")) { metadataAccess.setter = method; metadataAccess.fieldName = method.getName().substring(3); } } } if (metadataAccess.fieldName != null) { if (metadataAccess.getter == null) { for (Method method : raw.getMethods()) { if (method.getName().equals("get" + metadataAccess.fieldName)) { metadataAccess.getter = method; break; } } } else if (metadataAccess.setter == null) { for (Method method : raw.getMethods()) { if (method.getName().equals("set" + metadataAccess.fieldName)) { metadataAccess.setter = method; break; } } } metadataAccess.fieldName = WordUtils.uncapitalize(metadataAccess.fieldName); } ObjectConstructor<T> constructor = constructorConstructor.get(type); return (TypeAdapter<T>) new HyperDataTypeAdapter(metadataAccess, constructorConstructor, (ObjectConstructor<Object>) constructor, getBoundFields(gson, type, raw), gson, this); }
From source file:org.gbif.registry.metasync.util.converter.UriConverter.java
@Override public Object convert(Class type, Object value) { checkNotNull(type, "type cannot be null"); checkArgument(type.equals(URI.class), "Conversion target should be org.gbif.api.vocabulary.Language, but is %s", type.getClass()); checkArgument(String.class.isAssignableFrom(value.getClass()), "Value should be a string, but is a %s", value.getClass());/* w w w .j a va 2 s .c om*/ try { return new URI((String) value); } catch (URISyntaxException e) { LOG.warn("Invalid URL: [{}]", value, e); } return null; }