Example usage for java.lang Class getInterfaces

List of usage examples for java.lang Class getInterfaces

Introduction

In this page you can find the example usage for java.lang Class getInterfaces.

Prototype

public Class<?>[] getInterfaces() 

Source Link

Document

Returns the interfaces directly implemented by the class or interface represented by this object.

Usage

From source file:cn.aposoft.util.spring.ReflectionUtils.java

private static List<Method> findConcreteMethodsOnInterfaces(Class<?> clazz) {
    List<Method> result = null;
    for (Class<?> ifc : clazz.getInterfaces()) {
        for (Method ifcMethod : ifc.getMethods()) {
            if (!Modifier.isAbstract(ifcMethod.getModifiers())) {
                if (result == null) {
                    result = new LinkedList<Method>();
                }// w ww  .j  a v  a2 s.co  m
                result.add(ifcMethod);
            }
        }
    }
    return result;
}

From source file:org.gwtspringhibernate.reference.rlogman.spring.GwtServiceExporter.java

/**
 * Find the invoked method on either the specified interface or any super.
 *///from   w  ww . ja v a2  s.c o  m
private static Method findInterfaceMethod(Class intf, String methodName, Class[] paramTypes,
        boolean includeInherited) {
    try {
        return intf.getDeclaredMethod(methodName, paramTypes);
    } catch (NoSuchMethodException e) {
        if (includeInherited) {
            Class[] superintfs = intf.getInterfaces();
            for (int i = 0; i < superintfs.length; i++) {
                Method method = findInterfaceMethod(superintfs[i], methodName, paramTypes, true);
                if (method != null) {
                    return method;
                }
            }
        }

        return null;
    }
}

From source file:com.google.gdt.eclipse.designer.ie.util.ReflectionUtils.java

/**
 * Returns the {@link Method} defined in {@link Class}. This method can have any visibility, i.e. we can
 * find even protected/private methods. Can return <code>null</code> if no method with given signature
 * found./*  w w  w.j  a va  2 s .c o  m*/
 * 
 * @param clazz
 *            the {@link Class} to get method from it, or its superclass.
 * @param signature
 *            the signature of method in same format as {@link #getMethodSignature(Method)}.
 * 
 * @return the {@link Method} for given signature, or <code>null</code> if no such method found.
 */
public static Method getMethodBySignature(Class<?> clazz, String signature) {
    Assert.isNotNull(clazz);
    Assert.isNotNull(signature);
    // extract name of method to perform fast check only by name, full signature requires too much time
    String signatureName = StringUtils.substringBefore(signature, "(");
    // check methods of given class and its super classes
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        Method method = getMethodBySignature0(c, signatureName, signature);
        if (method != null) {
            return method;
        }
    }
    // check methods of implemented interfaces
    for (Class<?> interfaceClass : clazz.getInterfaces()) {
        Method method = getMethodBySignature0(interfaceClass, signatureName, signature);
        if (method != null) {
            return method;
        }
    }
    // not found
    return null;
}

From source file:org.dozer.util.ReflectionUtils.java

static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceClass) {
    List<PropertyDescriptor> propDescriptors = new ArrayList<PropertyDescriptor>();
    // Add prop descriptors for interface passed in
    propDescriptors.addAll(Arrays.asList(PropertyUtils.getPropertyDescriptors(interfaceClass)));

    // Look for interface inheritance. If super interfaces are found, recurse up the hierarchy tree and add prop
    // descriptors for each interface found.
    // PropertyUtils.getPropertyDescriptors() does not correctly walk the inheritance hierarchy for interfaces.
    Class<?>[] interfaces = interfaceClass.getInterfaces();
    if (interfaces != null) {
        for (Class<?> superInterfaceClass : interfaces) {
            List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays
                    .asList(getInterfacePropertyDescriptors(superInterfaceClass));
            /*/*from  w ww  .jav  a  2s.  c  o m*/
             * #1814758
             * Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added
             * to the result list.  This caused issues when getter and setter of an attribute on different interfaces in
             * an inheritance hierarchy
             */
            for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) {
                PropertyDescriptor existingPropDescriptor = findPropDescriptorByName(propDescriptors,
                        superPropDescriptor.getName());
                if (existingPropDescriptor == null) {
                    propDescriptors.add(superPropDescriptor);
                } else {
                    try {
                        if (existingPropDescriptor.getReadMethod() == null) {
                            existingPropDescriptor.setReadMethod(superPropDescriptor.getReadMethod());
                        }
                        if (existingPropDescriptor.getWriteMethod() == null) {
                            existingPropDescriptor.setWriteMethod(superPropDescriptor.getWriteMethod());
                        }
                    } catch (IntrospectionException e) {
                        throw new MappingException(e);
                    }

                }
            }
        }
    }
    return propDescriptors.toArray(new PropertyDescriptor[propDescriptors.size()]);
}

From source file:org.ballerinalang.composer.tools.ModelGenerator.java

public static JsonObject getContext() {

    // Set alias for the classes
    alias.put("ImportNode", "ImportPackageNode");
    alias.put("ArrayLiteralExprNode", "ArrayLiteralNode");
    alias.put("BinaryExprNode", "BinaryExpressionNode");
    alias.put("BracedTupleExprNode", "BracedOrTupleExpression");
    alias.put("TypeInitExprNode", "TypeInitNode");
    alias.put("FieldBasedAccessExprNode", "FieldBasedAccessNode");
    alias.put("IndexBasedAccessExprNode", "IndexBasedAccessNode");
    alias.put("IntRangeExprNode", "IntRangeExpression");
    alias.put("LambdaNode", "LambdaFunctionNode");
    alias.put("SimpleVariableRefNode", "SimpleVariableReferenceNode");
    alias.put("TernaryExprNode", "TernaryExpressionNode");
    alias.put("AwaitExprNode", "AwaitExpressionNode");
    alias.put("TypeCastExprNode", "TypeCastNode");
    alias.put("TypeConversionExprNode", "TypeConversionNode");
    alias.put("UnaryExprNode", "UnaryExpressionNode");
    alias.put("RestArgsExprNode", "RestArgsNode");
    alias.put("NamedArgsExprNode", "NamedArgNode");
    alias.put("MatchExpressionPatternClauseNode", "MatchExpressionNode");
    alias.put("MatchPatternClauseNode", "MatchStatementPatternNode");
    alias.put("TryNode", "TryCatchFinallyNode");
    alias.put("VariableDefNode", "VariableDefinitionNode");
    alias.put("UnionTypeNodeNode", "UnionTypeNode");
    alias.put("TupleTypeNodeNode", "TupleTypeNode");
    alias.put("EndpointTypeNode", "UserDefinedTypeNode");
    alias.put("StreamingQueryNode", "StreamingQueryStatementNode");
    alias.put("WithinNode", "WithinClause");
    alias.put("PatternClauseNode", "PatternClause");
    alias.put("ElvisExprNode", "ElvisExpressionNode");
    alias.put("CheckExprNode", "CheckedExpressionNode");
    alias.put("RecordLiteralExprNode", "RecordLiteralNode");
    alias.put("TypeDefinitionNode", "TypeDefinition");

    alias.put("EnumeratorNode", "");
    alias.put("RecordLiteralKeyValueNode", "");
    alias.put("TableNode", "");
    alias.put("XmlnsNode", "");
    alias.put("IsAssignableExprNode", "");
    alias.put("XmlQnameNode", "");
    alias.put("XmlAttributeNode", "");
    alias.put("XmlAttributeAccessExprNode", "");
    alias.put("XmlQuotedStringNode", "");
    alias.put("XmlElementLiteralNode", "");
    alias.put("XmlTextLiteralNode", "");
    alias.put("XmlCommentLiteralNode", "");
    alias.put("XmlPiLiteralNode", "");
    alias.put("XmlSequenceLiteralNode", "");
    alias.put("TableQueryExpressionNode", "");
    alias.put("NextNode", "");
    alias.put("TransformNode", "");
    alias.put("StreamNode", "");
    alias.put("FiniteTypeNodeNode", "");
    alias.put("BuiltInRefTypeNode", "");
    alias.put("StreamingInputNode", "");
    alias.put("JoinStreamingInputNode", "");
    alias.put("TableQueryNode", "");
    alias.put("SetAssignmentClauseNode", "");
    alias.put("SetNode", "");
    alias.put("QueryNode", "");
    alias.put("StreamingQueryDeclarationNode", "");

    List<Class<?>> list = ModelGenerator.find("org.ballerinalang.model.tree");

    NodeKind[] nodeKinds = NodeKind.class.getEnumConstants();
    JsonObject nodes = new JsonObject();
    for (NodeKind node : nodeKinds) {
        String nodeKind = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, node.toString());
        String nodeClassName = nodeKind + "Node";
        try {//from   w  ww .ja  va  2 s  . c om
            String actualClassName = (alias.get(nodeClassName) != null) ? alias.get(nodeClassName)
                    : nodeClassName;
            Class<?> clazz = list.stream()
                    .filter(nodeClass -> nodeClass.getSimpleName().equals(actualClassName)).findFirst().get();

            JsonObject nodeObj = new JsonObject();
            nodeObj.addProperty("kind", nodeKind);
            nodeObj.addProperty("name", nodeClassName);
            nodeObj.addProperty("fileName", CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, nodeClassName));
            JsonArray attr = new JsonArray();
            JsonArray bools = new JsonArray();
            JsonArray imports = new JsonArray();
            List<String> parents = Arrays.asList(clazz.getInterfaces()).stream()
                    .map(parent -> parent.getSimpleName()).collect(Collectors.toList());

            // tag object with supper type
            if (parents.contains("StatementNode")) {
                nodeObj.addProperty("isStatement", true);
                JsonObject imp = new JsonObject();
                imp.addProperty("returnType", "StatementNode");
                imp.addProperty("returnTypeFile", "statement-node");
                imports.add(imp);
            } else {
                nodeObj.addProperty("isStatement", false);
            }

            if (parents.contains("ExpressionNode")) {
                nodeObj.addProperty("isExpression", true);
                JsonObject imp = new JsonObject();
                imp.addProperty("returnType", "ExpressionNode");
                imp.addProperty("returnTypeFile", "expression-node");
                imports.add(imp);
            } else {
                nodeObj.addProperty("isExpression", false);
            }

            if (!parents.contains("StatementNode") && !parents.contains("ExpressionNode")) {
                JsonObject imp = new JsonObject();
                imp.addProperty("returnType", "Node");
                imp.addProperty("returnTypeFile", "node");
                imports.add(imp);
            }

            Method[] methods = clazz.getMethods();
            for (Method m : methods) {
                String methodName = m.getName();
                if ("getKind".equals(methodName) || "getWS".equals(methodName)
                        || "getPosition".equals(methodName)) {
                    continue;
                }
                if (methodName.startsWith("get")) {
                    JsonObject attribute = new JsonObject();
                    JsonObject imp = new JsonObject();
                    attribute.addProperty("property", toJsonName(m.getName(), 3));
                    attribute.addProperty("methodSuffix", m.getName().substring(3));
                    attribute.addProperty("list", List.class.isAssignableFrom(m.getReturnType()));
                    attribute.addProperty("isNode", Node.class.isAssignableFrom(m.getReturnType()));
                    if (Node.class.isAssignableFrom(m.getReturnType())) {
                        String returnClass = m.getReturnType().getSimpleName();
                        if (alias.containsValue(m.getReturnType().getSimpleName())) {
                            returnClass = getKindForAliasClass(m.getReturnType().getSimpleName());
                        }
                        imp.addProperty("returnType", returnClass);
                        attribute.addProperty("returnType", returnClass);
                        imp.addProperty("returnTypeFile",
                                CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, returnClass));
                        if (!imports.contains(imp)) {
                            imports.add(imp);
                        }
                    }
                    attr.add(attribute);
                }
                if (methodName.startsWith("is")) {
                    JsonObject attribute = new JsonObject();
                    JsonObject imp = new JsonObject();
                    attribute.addProperty("property", toJsonName(m.getName(), 2));
                    attribute.addProperty("methodSuffix", m.getName().substring(2));
                    attribute.addProperty("list", List.class.isAssignableFrom(m.getReturnType()));
                    attribute.addProperty("isNode", Node.class.isAssignableFrom(m.getReturnType()));
                    if (Node.class.isAssignableFrom(m.getReturnType())) {
                        String returnClass = m.getReturnType().getSimpleName();
                        if (alias.containsValue(m.getReturnType().getSimpleName())) {
                            returnClass = getKindForAliasClass(m.getReturnType().getSimpleName());
                        }
                        imp.addProperty("returnType", returnClass);
                        attribute.addProperty("returnType", returnClass);
                        imp.addProperty("returnTypeFile",
                                CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, returnClass));
                        if (!imports.contains(imp)) {
                            imports.add(imp);
                        }
                    }
                    bools.add(attribute);
                }
            }
            nodeObj.add("attributes", attr);
            nodeObj.add("bools", bools);
            nodeObj.add("imports", imports);
            nodes.add(nodeClassName, nodeObj);
        } catch (NoSuchElementException e) {
            out.println("alias.put(\"" + nodeClassName + "\", \"\");");
        }
    }
    out.println(nodes);
    return nodes;
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Collect class annotations./*  w  w  w .j  av a 2  s.c  o  m*/
 *
 * @param <T> the generic type
 * @param clazz the clazz
 * @param annotClass the annot class
 * @param annots the annots
 * @param visitedClasses the visited classes
 */
public static <T extends Annotation> void collectClassAnnotations(Class<?> clazz, Class<T> annotClass,
        List<T> annots, Set<Class<?>> visitedClasses) {
    if (visitedClasses.contains(clazz) == true) {
        return;
    }
    visitedClasses.add(clazz);
    T[] ana = clazz.getAnnotationsByType(annotClass);
    if (ana != null) {
        for (T an : ana) {
            annots.add(an);
        }
    }
    Class<?> superclz = clazz.getSuperclass();
    if (superclz == null || superclz == Object.class) {
        return;
    }
    collectClassAnnotations(superclz, annotClass, annots, visitedClasses);
    Class<?>[] ifaces = clazz.getInterfaces();
    if (ifaces == null || ifaces.length == 0) {
        return;
    }
    for (Class<?> iclazz : ifaces) {
        collectClassAnnotations(iclazz, annotClass, annots, visitedClasses);
    }
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceClass) {
    List<PropertyDescriptor> propDescriptors = new ArrayList<>();
    // Add prop descriptors for interface passed in
    propDescriptors.addAll(Arrays.asList(PropertyUtils.getPropertyDescriptors(interfaceClass)));

    // Look for interface inheritance. If super interfaces are found, recurse up the hierarchy tree and add prop
    // descriptors for each interface found.
    // PropertyUtils.getPropertyDescriptors() does not correctly walk the inheritance hierarchy for interfaces.
    Class<?>[] interfaces = interfaceClass.getInterfaces();
    if (interfaces != null) {
        for (Class<?> superInterfaceClass : interfaces) {
            List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays
                    .asList(getInterfacePropertyDescriptors(superInterfaceClass));
            /*//  w w w  .  j av  a 2 s.  c  om
             * #1814758
             * Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added
             * to the result list.  This caused issues when getter and setter of an attribute on different interfaces in
             * an inheritance hierarchy
             */
            for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) {
                PropertyDescriptor existingPropDescriptor = findPropDescriptorByName(propDescriptors,
                        superPropDescriptor.getName());
                if (existingPropDescriptor == null) {
                    propDescriptors.add(superPropDescriptor);
                } else {
                    try {
                        if (existingPropDescriptor.getReadMethod() == null) {
                            existingPropDescriptor.setReadMethod(superPropDescriptor.getReadMethod());
                        }
                        if (existingPropDescriptor.getWriteMethod() == null) {
                            existingPropDescriptor.setWriteMethod(superPropDescriptor.getWriteMethod());
                        }
                    } catch (IntrospectionException e) {
                        throw new MappingException(e);
                    }

                }
            }
        }
    }
    return propDescriptors.toArray(new PropertyDescriptor[propDescriptors.size()]);
}

From source file:com.mylaensys.dhtmlx.adapter.DhtmlxHttpMessageConverter.java

@Override
protected boolean supports(Class<?> clazz) {
    Class[] theInterfaces = clazz.getInterfaces();
    for (int i = 0; i < theInterfaces.length; i++) {
        if (theInterfaces[i].getName().equalsIgnoreCase(Adapter.class.getName())) {
            return true;
        }// www .  java2  s .com
    }
    return true;
}

From source file:com.npower.dm.util.DMUtil.java

/**
 * Format a string buffer containing the Class, Interfaces, CodeSource, and
 * ClassLoader information for the given object clazz.
 * /*from  w w  w.  ja v  a  2s . c  o 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 ProbeHome
    ClassLoader cl = clazz.getClassLoader();
    results.append("\n" + clazz.getName() + ".ClassLoader=" + cl);
    ClassLoader parent = cl;
    while (parent != null) {
        results.append("\n.." + parent);
        URL[] urls = getClassLoaderURLs(parent);
        int length = urls != null ? urls.length : 0;
        for (int u = 0; u < length; u++) {
            results.append("\n...." + urls[u]);
        }
        if (parent != null)
            parent = parent.getParent();
    }
    CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource();
    if (clazzCS != null)
        results.append("\n++++CodeSource: " + clazzCS);
    else
        results.append("\n++++Null CodeSource");
    results.append("\nImplemented Interfaces:");
    Class<?>[] ifaces = clazz.getInterfaces();
    for (int i = 0; i < ifaces.length; i++) {
        results.append("\n++" + ifaces[i]);
        ClassLoader loader = ifaces[i].getClassLoader();
        results.append("\n++++ClassLoader: " + loader);
        ProtectionDomain pd = ifaces[i].getProtectionDomain();
        CodeSource cs = pd.getCodeSource();
        if (cs != null)
            results.append("\n++++CodeSource: " + cs);
        else
            results.append("\n++++Null CodeSource");
    }
}

From source file:com.github.aenygmatic.spring.osgi.registration.OsgiSpringComponentCollector.java

private Class<?> findImplementedInterface(Class<? extends Object> clazz) {
    Class<?>[] interfaces = clazz.getInterfaces();
    if (interfaces != null && interfaces.length > 0) {
        return interfaces[0];
    } else {/*w w  w.  ja va  2s .  c  o m*/
        throw new NotRegistrableServiceException(
                "Component annotated with @OsgiService must have a registration interface given or at least the class should implement an interface!");
    }
}