List of usage examples for java.lang Class newInstance
@CallerSensitive @Deprecated(since = "9") public T newInstance() throws InstantiationException, IllegalAccessException
From source file:de.sub.goobi.metadaten.copier.DataCopyrule.java
/** * Factory method to create a class implementing the metadata copy rule * referenced by a given command string/*from ww w . j a v a 2 s . c om*/ * * @param command * A space-separated string consisting of subject (aka. patiens), * operator (aka. agens) and (optional) objects (depending on * what objects the operator requires). * @return a class implementing the metadata copy rule referenced * @throws ConfigurationException * if the operator cannot be resolved or the number of arguments * doesnt match */ public static DataCopyrule createFor(String command) throws ConfigurationException { List<String> arguments = Arrays.asList(command.split("\\s+")); String operator; try { operator = arguments.get(1); } catch (IndexOutOfBoundsException e) { throw new ConfigurationException("Missing operator (second argument) in line: " + command); } Class<? extends DataCopyrule> ruleClass = AVAILABLE_RULES.get(operator); if (ruleClass == null) { throw new ConfigurationException("Unknown operator: " + operator); } DataCopyrule ruleImplementation; try { ruleImplementation = ruleClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } ruleImplementation.setSubject(arguments.get(0)); if (ruleImplementation.getMaxObjects() > 0) { List<String> objects = arguments.subList(2, arguments.size()); if (objects.size() < ruleImplementation.getMinObjects()) { throw new ConfigurationException("Too few arguments in line: " + command); } if (objects.size() > ruleImplementation.getMaxObjects()) { throw new ConfigurationException("Too many arguments in line: " + command); } ruleImplementation.setObjects(objects); } return ruleImplementation; }
From source file:eu.edisonproject.training.execute.Main.java
private static void termExtraction(String docs, String out) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, InterruptedException { if (!new File(docs).exists()) { throw new IOException(new File(docs).getAbsolutePath() + " don't exist"); }/*from w ww . java 2 s . c om*/ // String[] extractors = prop.getProperty("term.extractors", "eu.edisonproject.training.term.extraction.LuceneExtractor," + "eu.edisonproject.training.term.extraction.JtopiaExtractor," + "eu.edisonproject.training.term.extraction.AprioriExtraction") .split(","); // String[] extractors = prop.getProperty("term.extractors", // "eu.edisonproject.training.term.extraction.JtopiaExtractor," // + "eu.edisonproject.training.term.extraction.AprioriExtraction").split(","); // String[] extractors = "eu.edisonproject.training.term.extraction.JtopiaExtractor".split(","); Map<String, Double> termDictionaray = new HashMap(); for (String className : extractors) { Class c = Class.forName(className); Object obj = c.newInstance(); TermExtractor termExtractor = (TermExtractor) obj; termExtractor.configure(prop); termDictionaray.putAll(termExtractor.termXtraction(docs)); } writeDictionary2File(termDictionaray, out); calculateTermTFIDF(docs, out, out); }
From source file:net.oauth.signature.OAuthSignatureMethod.java
/** The factory for signature methods. */ public static OAuthSignatureMethod newMethod(String name, OAuthAccessor accessor) throws OAuthException { try {/*from ww w . ja va 2s. co m*/ Class methodClass = NAME_TO_CLASS.get(name); if (methodClass != null) { OAuthSignatureMethod method = (OAuthSignatureMethod) methodClass.newInstance(); method.initialize(name, accessor); return method; } OAuthProblemException problem = new OAuthProblemException(OAuth.Problems.SIGNATURE_METHOD_REJECTED); String acceptable = OAuth.percentEncode(NAME_TO_CLASS.keySet()); if (acceptable.length() > 0) { problem.setParameter("oauth_acceptable_signature_methods", acceptable.toString()); } throw problem; } catch (InstantiationException e) { throw new OAuthException(e); } catch (IllegalAccessException e) { throw new OAuthException(e); } }
From source file:com.greenline.hrs.admin.cache.redis.util.RedisInfoParser.java
/** * RedisInfoMapT?RedisInfoMapRedis?//from w ww . jav a2 s .c o m * RedisServerInfoPO * * @param instanceType class * @param redisInfoMap Map * @param <T> * @return instanceTypeinfomapnullinfoMap?instancenullRedisServerInfoPO */ public static <T> T parserRedisInfo(Class<T> instanceType, Map<String, String> redisInfoMap) { if (instanceType == null || redisInfoMap == null || redisInfoMap.isEmpty()) { return null; } T instance = null; try { instance = instanceType.newInstance(); } catch (Exception e) { LOG.error("Instance:" + instanceType.getName(), e); return null; } Field[] fields = instanceType.getDeclaredFields(); String inputName = null; for (Field field : fields) { ParserField parserField = field.getAnnotation(ParserField.class); if (parserField != null) { inputName = parserField.inputName(); } else { inputName = field.getName(); } if (redisInfoMap.containsKey(inputName)) { field.setAccessible(true); try { field.set(instance, ConvertUtils.convert(redisInfoMap.get(inputName), field.getType())); } catch (Exception e) { LOG.error("Field:" + field.getName() + "\t|\t value:" + redisInfoMap.get(inputName), e); } } } return instance; }
From source file:fi.foyt.foursquare.api.JSONFieldParser.java
/** * Initializes new entity instance/*from w w w. j av a 2 s.c o m*/ * * @param clazz class * @return new entity instance */ private static FoursquareEntity createNewEntity(Class<?> clazz) { try { return (FoursquareEntity) clazz.newInstance(); } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } }
From source file:com.xidu.framework.common.util.Utils.java
public static Object convertDtoToDomain(Object origDto, Class<?> domainClass) { if (null == origDto) { return null; }/*from www . ja v a 2s . c o m*/ Object dest = null; try { dest = domainClass.newInstance(); ConvertUtils.register(new DateConverter(null), Date.class); BeanUtils.copyProperties(dest, origDto); } catch (Exception e) { e.printStackTrace(); return null; } return dest; }
From source file:ShowComponent.java
/** * This method loops through the command line arguments looking for class * names of components to create and property settings for those components * in the form name=value. This method demonstrates reflection and JavaBeans * introspection as they can be applied to dynamically created GUIs *///w ww . j a v a2 s . co m public static Vector getComponentsFromArgs(String[] args) { Vector components = new Vector(); // List of components to return Component component = null; // The current component PropertyDescriptor[] properties = null; // Properties of the component Object[] methodArgs = new Object[1]; // We'll use this below nextarg: // This is a labeled loop for (int i = 0; i < args.length; i++) { // Loop through all arguments // If the argument does not contain an equal sign, then it is // a component class name. Otherwise it is a property setting int equalsPos = args[i].indexOf('='); if (equalsPos == -1) { // Its the name of a component try { // Load the named component class Class componentClass = Class.forName(args[i]); // Instantiate it to create the component instance component = (Component) componentClass.newInstance(); // Use JavaBeans to introspect the component // And get the list of properties it supports BeanInfo componentBeanInfo = Introspector.getBeanInfo(componentClass); properties = componentBeanInfo.getPropertyDescriptors(); } catch (Exception e) { // If any step failed, print an error and exit System.out.println("Can't load, instantiate, " + "or introspect: " + args[i]); System.exit(1); } // If we succeeded, store the component in the vector components.addElement(component); } else { // The arg is a name=value property specification String name = args[i].substring(0, equalsPos); // property name String value = args[i].substring(equalsPos + 1); // property // value // If we don't have a component to set this property on, skip! if (component == null) continue nextarg; // Now look through the properties descriptors for this // component to find one with the same name. for (int p = 0; p < properties.length; p++) { if (properties[p].getName().equals(name)) { // Okay, we found a property of the right name. // Now get its type, and the setter method Class type = properties[p].getPropertyType(); Method setter = properties[p].getWriteMethod(); // Check if property is read-only! if (setter == null) { System.err.println("Property " + name + " is read-only"); continue nextarg; // continue with next argument } // Try to convert the property value to the right type // We support a small set of common property types here // Store the converted value in an Object[] so it can // be easily passed when we invoke the property setter try { if (type == String.class) { // no conversion needed methodArgs[0] = value; } else if (type == int.class) { // String to int methodArgs[0] = Integer.valueOf(value); } else if (type == boolean.class) { // to boolean methodArgs[0] = Boolean.valueOf(value); } else if (type == Color.class) { // to Color methodArgs[0] = Color.decode(value); } else if (type == Font.class) { // String to Font methodArgs[0] = Font.decode(value); } else { // If we can't convert, ignore the property System.err .println("Property " + name + " is of unsupported type " + type.getName()); continue nextarg; } } catch (Exception e) { // If conversion failed, continue with the next arg System.err.println("Can't convert '" + value + "' to type " + type.getName() + " for property " + name); continue nextarg; } // Finally, use reflection to invoke the property // setter method of the component we created, and pass // in the converted property value. try { setter.invoke(component, methodArgs); } catch (Exception e) { System.err.println("Can't set property: " + name); } // Now go on to next command-line arg continue nextarg; } } // If we get here, we didn't find the named property System.err.println("Warning: No such property: " + name); } } return components; }
From source file:com.aol.advertising.qiao.util.ContextUtils.java
public static Object createBeanSilently(String className) throws ClassNotFoundException { Object o = null;//from w ww . j a v a 2 s. com Class<?> clz = Class.forName(className); try { o = clz.newInstance(); } catch (Exception e) { logger.error("Failed to instantiate class " + className); } return o; }
From source file:com.ricemap.spateDB.core.SpatialSite.java
public static Shape getShape(Configuration conf, String param) { String str = conf.get(param); String[] parts = str.split(",", 2); String shapeClassName = parts[0]; Shape shape = null;//from w w w . j a va 2 s. c om try { Class<? extends Shape> shapeClass = Class.forName(shapeClassName).asSubclass(Shape.class); shape = shapeClass.newInstance(); shape.fromText(new Text(parts[1])); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return shape; }
From source file:com.xidu.framework.common.util.Utils.java
public static Object convertDomainToDto(Object origDomain, Class<?> dtoClass) { if (null == origDomain) { return null; }/*from www .ja v a2s .co m*/ Object dest = null; try { dest = dtoClass.newInstance(); ConvertUtils.register(new DateConverter(null), Date.class); BeanUtils.copyProperties(dest, origDomain); } catch (Exception e) { e.printStackTrace(); return null; } return dest; }