List of usage examples for java.lang Class getInterfaces
public Class<?>[] getInterfaces()
From source file:info.magnolia.beanmerger.ProxyBasedBeanMerger.java
@Override protected Object mergeBean(List sources) { Set<Class> types = new HashSet<Class>(); Class<?> mostSpecificAssignableClass = sources.get(0).getClass(); while (Enhancer.isEnhanced(mostSpecificAssignableClass)) { mostSpecificAssignableClass = mostSpecificAssignableClass.getSuperclass(); }/* ww w .j a va2 s .c o m*/ for (Object source : sources) { // Skip up the class hierarchy to avoid proxying cglib proxy classes Class clazz = source.getClass(); while (Enhancer.isEnhanced(clazz)) { clazz = clazz.getSuperclass(); } Collections.addAll(types, clazz.getInterfaces()); if (mostSpecificAssignableClass.isAssignableFrom(clazz)) { mostSpecificAssignableClass = clazz; } } types.add(mostSpecificAssignableClass); try { return new CglibProxyFactory().createInvokerProxy(newInvoker(sources), types.toArray(new Class<?>[types.size()])); } catch (RuntimeException e) { log.error("Failed to create proxy for sources {} with types {}", sources, types); throw e; } }
From source file:com.bstek.dorado.idesupport.initializer.FloatControlRuleTemplateInitializer.java
public void initRuleTemplate(RuleTemplate ruleTemplate, InitializerContext initializerContext) throws Exception { String typeName = ruleTemplate.getType(); if (StringUtils.isNotEmpty(typeName)) { Class<?> type = ClassUtils.forName(typeName); if (type.equals(FloatControl.class) || Modifier.isAbstract(type.getModifiers())) { return; }//from w w w. j a v a 2 s. c o m boolean found = false; for (Class<?> _interface : type.getInterfaces()) { if (_interface.equals(FloatControl.class)) { found = true; break; } } if (!found) { return; } } RuleTemplateManager ruleTemplateManager = initializerContext.getRuleTemplateManager(); RuleTemplate floatControlRule = ruleTemplateManager.getRuleTemplate("FloatControl"); if (floatControlRule == null) { floatControlRule = new AutoRuleTemplate("FloatControl", FloatControl.class.getName()); floatControlRule.setAbstract(true); ruleTemplateManager.addRuleTemplate(floatControlRule); } RuleTemplate[] parents = ruleTemplate.getParents(); if (parents != null) { RuleTemplate[] oldParents = parents; parents = new RuleTemplate[oldParents.length + 1]; System.arraycopy(oldParents, 0, parents, 0, oldParents.length); parents[oldParents.length] = floatControlRule; } else { parents = new RuleTemplate[] { floatControlRule }; } ruleTemplate.setParents(parents); }
From source file:Main.java
/** * Format a string buffer containing the Class, Interfaces, CodeSource, and * ClassLoader information for the given object clazz. * /* ww w .j a va 2 s.co m*/ * @param clazz * the Class * @param results - * the buffer to write the info to */ public static void displayClassInfo(Class clazz, StringBuffer results) { // Print out some codebase info for the clazz ClassLoader cl = clazz.getClassLoader(); results.append("\n"); results.append(clazz.getName()); results.append("("); results.append(Integer.toHexString(clazz.hashCode())); results.append(").ClassLoader="); results.append(cl); ClassLoader parent = cl; while (parent != null) { results.append("\n.."); results.append(parent); URL[] urls = getClassLoaderURLs(parent); int length = urls != null ? urls.length : 0; for (int u = 0; u < length; u++) { results.append("\n...."); results.append(urls[u]); } if (parent != null) parent = parent.getParent(); } CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource(); if (clazzCS != null) { results.append("\n++++CodeSource: "); results.append(clazzCS); } else results.append("\n++++Null CodeSource"); results.append("\nImplemented Interfaces:"); Class[] ifaces = clazz.getInterfaces(); for (int i = 0; i < ifaces.length; i++) { Class iface = ifaces[i]; results.append("\n++"); results.append(iface); results.append("("); results.append(Integer.toHexString(iface.hashCode())); results.append(")"); ClassLoader loader = ifaces[i].getClassLoader(); results.append("\n++++ClassLoader: "); results.append(loader); ProtectionDomain pd = ifaces[i].getProtectionDomain(); CodeSource cs = pd.getCodeSource(); if (cs != null) { results.append("\n++++CodeSource: "); results.append(cs); } else results.append("\n++++Null CodeSource"); } }
From source file:optional.BasicFilter.java
boolean isInterfaceImplementer(Class target, Class superclass) { Class[] interfaces = target.getInterfaces(); for (Class interface1 : interfaces) { if (interface1 == superclass) { return true; }/*ww w .jav a 2s . co m*/ } return false; }
From source file:org.apache.axis.utils.JavaUtils.java
/** * Determines if the Class is a Holder class. If so returns Class of held type * else returns null/* w w w . j a v a 2 s . c o m*/ * @param type the suspected Holder Class * @return class of held type or null */ public static Class getHolderValueType(Class type) { if (type != null) { Class[] intf = type.getInterfaces(); boolean isHolder = false; for (int i = 0; i < intf.length && !isHolder; i++) { if (intf[i] == javax.xml.rpc.holders.Holder.class) { isHolder = true; } } if (isHolder == false) { return null; } // Holder is supposed to have a public value field. java.lang.reflect.Field field; try { field = type.getField("value"); } catch (Exception e) { field = null; } if (field != null) { return field.getType(); } } return null; }
From source file:org.akita.proxy.ProxyInvocationHandler.java
/** * Dynamic proxy invoke/*from w w w. j a v a2 s. com*/ */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { AkPOST akPost = method.getAnnotation(AkPOST.class); AkGET akGet = method.getAnnotation(AkGET.class); AkAPI akApi = method.getAnnotation(AkAPI.class); Annotation[][] annosArr = method.getParameterAnnotations(); String invokeUrl = akApi.url(); ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); // AkApiParams to hashmap, filter out of null-value HashMap<String, File> filesToSend = new HashMap<String, File>(); HashMap<String, String> paramsMapOri = new HashMap<String, String>(); HashMap<String, String> paramsMap = getRawApiParams2HashMap(annosArr, args, filesToSend, paramsMapOri); // Record this invocation ApiInvokeInfo apiInvokeInfo = new ApiInvokeInfo(); apiInvokeInfo.apiName = method.getName(); apiInvokeInfo.paramsMap.putAll(paramsMapOri); apiInvokeInfo.url = invokeUrl; // parse '{}'s in url invokeUrl = parseUrlbyParams(invokeUrl, paramsMap); // cleared hashmap to params, and filter out of the null value Iterator<Entry<String, String>> iter = paramsMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, String> entry = iter.next(); params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } // get the signature string if using AkSignature akSig = method.getAnnotation(AkSignature.class); if (akSig != null) { Class<?> clazzSignature = akSig.using(); if (clazzSignature.getInterfaces().length > 0 // TODO: NEED VERIFY WHEN I HAVE TIME && InvokeSignature.class.getName().equals(clazzSignature.getInterfaces()[0].getName())) { InvokeSignature is = (InvokeSignature) clazzSignature.getConstructors()[0].newInstance(); String sigValue = is.signature(akSig, invokeUrl, params, paramsMapOri); String sigParamName = is.getSignatureParamName(); if (sigValue != null && sigParamName != null && sigValue.length() > 0 && sigParamName.length() > 0) { params.add(new BasicNameValuePair(sigParamName, sigValue)); } } } // choose POST GET PUT DELETE to use for this invoke String retString = ""; if (akGet != null) { StringBuilder sbUrl = new StringBuilder(invokeUrl); if (!(invokeUrl.endsWith("?") || invokeUrl.endsWith("&"))) { sbUrl.append("?"); } for (NameValuePair nvp : params) { sbUrl.append(nvp.getName()); sbUrl.append("="); sbUrl.append(nvp.getValue()); sbUrl.append("&"); } // now default using UTF-8, maybe improved later retString = HttpInvoker.get(sbUrl.toString()); } else if (akPost != null) { if (filesToSend.isEmpty()) { retString = HttpInvoker.post(invokeUrl, params); } else { retString = HttpInvoker.postWithFilesUsingURLConnection(invokeUrl, params, filesToSend); } } else { // use POST for default retString = HttpInvoker.post(invokeUrl, params); } // invoked, then add to history //ApiStats.addApiInvocation(apiInvokeInfo); //Log.d(TAG, retString); // parse the return-string final Class<?> returnType = method.getReturnType(); try { if (String.class.equals(returnType)) { // the result return raw string return retString; } else { // return object using json decode return JsonMapper.json2pojo(retString, returnType); } } catch (Exception e) { Log.e(TAG, retString, e); // log can print the error return-string throw new AkInvokeException(AkInvokeException.CODE_JSONPROCESS_EXCEPTION, e.getMessage(), e); } }
From source file:org.lansir.beautifulgirls.proxy.ProxyInvocationHandler.java
/** * Dynamic proxy invoke// ww w . j a v a2s.com */ public Object invoke(Object proxy, Method method, Object[] args) throws AkInvokeException, AkServerStatusException, AkException, Exception { AkPOST akPost = method.getAnnotation(AkPOST.class); AkGET akGet = method.getAnnotation(AkGET.class); AkAPI akApi = method.getAnnotation(AkAPI.class); Annotation[][] annosArr = method.getParameterAnnotations(); String invokeUrl = akApi.url(); ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); // AkApiParams to hashmap, filter out of null-value HashMap<String, File> filesToSend = new HashMap<String, File>(); HashMap<String, String> paramsMapOri = new HashMap<String, String>(); HashMap<String, String> paramsMap = getRawApiParams2HashMap(annosArr, args, filesToSend, paramsMapOri); // Record this invocation ApiInvokeInfo apiInvokeInfo = new ApiInvokeInfo(); apiInvokeInfo.apiName = method.getName(); apiInvokeInfo.paramsMap.putAll(paramsMap); apiInvokeInfo.url = invokeUrl; // parse '{}'s in url invokeUrl = parseUrlbyParams(invokeUrl, paramsMap); // cleared hashmap to params, and filter out of the null value Iterator<Entry<String, String>> iter = paramsMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, String> entry = iter.next(); params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } // get the signature string if using AkSignature akSig = method.getAnnotation(AkSignature.class); if (akSig != null) { Class<?> clazzSignature = akSig.using(); if (clazzSignature.getInterfaces().length > 0 // TODO: NEED VERIFY WHEN I HAVE TIME && InvokeSignature.class.getName().equals(clazzSignature.getInterfaces()[0].getName())) { InvokeSignature is = (InvokeSignature) clazzSignature.getConstructors()[0].newInstance(); String sigValue = is.signature(akSig, invokeUrl, params, paramsMapOri); String sigParamName = is.getSignatureParamName(); if (sigValue != null && sigParamName != null && sigValue.length() > 0 && sigParamName.length() > 0) { params.add(new BasicNameValuePair(sigParamName, sigValue)); } } } // choose POST GET PUT DELETE to use for this invoke String retString = ""; if (akGet != null) { StringBuilder sbUrl = new StringBuilder(invokeUrl); if (!(invokeUrl.endsWith("?") || invokeUrl.endsWith("&"))) { sbUrl.append("?"); } for (NameValuePair nvp : params) { sbUrl.append(nvp.getName()); sbUrl.append("="); sbUrl.append(nvp.getValue()); sbUrl.append("&"); } // now default using UTF-8, maybe improved later retString = HttpInvoker.get(sbUrl.toString()); } else if (akPost != null) { if (filesToSend.isEmpty()) { retString = HttpInvoker.post(invokeUrl, params); } else { retString = HttpInvoker.postWithFilesUsingURLConnection(invokeUrl, params, filesToSend); } } else { // use POST for default retString = HttpInvoker.post(invokeUrl, params); } // invoked, then add to history //ApiStats.addApiInvocation(apiInvokeInfo); //Log.d(TAG, retString); // parse the return-string Class<?> returnType = method.getReturnType(); try { if (String.class.equals(returnType)) { // the result return raw string return retString; } else { // return object using json decode return JsonMapper.json2pojo(retString, returnType); } } catch (IOException e) { throw new AkInvokeException(AkInvokeException.CODE_IO_EXCEPTION, e.getMessage(), e); } /* * also can use gson like this eg. * Gson gson = new Gson(); * return gson.fromJson(HttpInvoker.post(url, params), returnType); */ }
From source file:com.offbynull.coroutines.instrumenter.asm.ClassLoaderClassInformationRepository.java
@Override public ClassInformation getInformation(String internalClassName) { String className = Type.getObjectType(internalClassName).getClassName(); Class<?> cls; try {/*from w ww . j a v a2 s . c om*/ cls = Class.forName(className, false, classLoader); } catch (ClassNotFoundException cfne) { return null; } boolean interfaceMarker = cls.isInterface(); Class<?>[] interfaceClses = cls.getInterfaces(); List<String> internalInterfaceNames = new ArrayList<>(interfaceClses.length); for (Class<?> interfaceCls : interfaceClses) { internalInterfaceNames.add(Type.getInternalName(interfaceCls)); } String internalSuperClassName; if (interfaceMarker) { // If this is an interface, it needs to mimic how class files are structured. Normally a class file will have its super class // set to java/lang/Object if it's an interface. This isn't exposed through the classloader. Not sure why this is like this but // if we encounter an interface as the class being accessed through the classloader, always override it to have a "superclass" // of java/lang/Object. The classloader itself would return null in this case. // // This is what ASM does internally as well when it loads info from a classloader. internalSuperClassName = Type.getInternalName(Object.class); } else { Class<?> superCls = cls.getSuperclass(); internalSuperClassName = superCls == null ? null : Type.getInternalName(superCls); } return new ClassInformation(internalSuperClassName, internalInterfaceNames, interfaceMarker); }
From source file:org.jbpm.services.task.jaxb.ComparePair.java
private List<ComparePair> compareObjectsViaGetMethods(Object orig, Object copy, Class<?> objInterface) { List<ComparePair> cantCompare = new ArrayList<ComparePair>(); for (Method getIsMethod : objInterface.getDeclaredMethods()) { String methodName = getIsMethod.getName(); String fieldName;/*from w w w.ja v a 2 s. c o m*/ if (methodName.startsWith("get")) { fieldName = methodName.substring(3); } else if (methodName.startsWith("is")) { fieldName = methodName.substring(2); } else { continue; } // getField -> field (lowercase f) fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); try { Object origField = getIsMethod.invoke(orig, new Object[0]); Object copyField = getIsMethod.invoke(copy, new Object[0]); boolean skip = false; if (origField == null) { if (nullFields != null) { for (String nullField : nullFields) { if (fieldName.equals(nullField)) { skip = true; break; } } } if (!skip) { fail("Please fill in the " + fieldName + " field in the " + objInterface.getSimpleName() + "!"); } } if (skip) { continue; } if (origField != null && !(origField instanceof Enum) && origField.getClass().getPackage().getName().startsWith("org.")) { ComparePair newComPair = new ComparePair(origField, copyField, getInterface(origField)); newComPair.addNullFields(this.nullFields); cantCompare.add(newComPair); continue; } else if (origField instanceof List<?>) { List<?> origList = (List) origField; List<?> copyList = (List) copyField; for (int i = 0; i < origList.size(); ++i) { Class<?> newInterface = origField.getClass(); while (newInterface.getInterfaces().length > 0) { newInterface = newInterface.getInterfaces()[0]; } ComparePair newCompair = new ComparePair(origList.get(i), copyList.get(i), getInterface(origList.get(i))); newCompair.addNullFields(this.nullFields); cantCompare.add(newCompair); } continue; } assertEquals(fieldName, origField, copyField); } catch (Exception e) { throw new RuntimeException("Unable to compare " + fieldName, e); } } return cantCompare; }
From source file:com.freiheit.fuava.ctprofiler.spring.ProfilingPostProcessor.java
private Class<?> isCandidateCls(final Class<? extends Object> class1) { if (class1 == null) { return null; }/*from w ww. j a v a 2 s.c o m*/ if (isCandidateIface(class1)) { return class1; } for (final Class<?> iface : class1.getInterfaces()) { final Class<?> cc = isCandidateCls(iface); if (cc != null) { return cc; } } return isCandidateCls(class1.getSuperclass()); }