Search in sources :

Example 1 with CompactFieldNode

use of com.datatorrent.stram.webapp.asm.CompactFieldNode in project apex-core by apache.

the class TypeGraph method getPublicSetterGetterAndPorts.

private void getPublicSetterGetterAndPorts(TypeGraphVertex tgv, List<CompactMethodNode> setters, List<CompactMethodNode> getters, Map<Type, Type> typeReplacement, List<CompactFieldNode> ports) {
    CompactClassNode exClass = null;
    // check if the class needs to be excluded
    for (String e : EXCLUDE_CLASSES) {
        if (e.equals(tgv.getOrLoadClassNode().getName())) {
            exClass = tgv.getOrLoadClassNode();
            break;
        }
    }
    if (exClass != null) {
        // So the setter/getter methods in Operater, Object, Class won't be counted
        for (CompactMethodNode compactMethodNode : exClass.getGetterMethods()) {
            for (Iterator<CompactMethodNode> iterator = getters.iterator(); iterator.hasNext(); ) {
                CompactMethodNode cmn = iterator.next();
                if (cmn.getName().equals(compactMethodNode.getName())) {
                    iterator.remove();
                }
            }
        }
        for (CompactMethodNode compactMethodNode : exClass.getSetterMethods()) {
            for (Iterator<CompactMethodNode> iterator = setters.iterator(); iterator.hasNext(); ) {
                CompactMethodNode cmn = iterator.next();
                if (cmn.getName().equals(compactMethodNode.getName())) {
                    iterator.remove();
                }
            }
        }
    } else {
        if (tgv.getOrLoadClassNode().getSetterMethods() != null) {
            setters.addAll(tgv.getOrLoadClassNode().getSetterMethods());
        }
        if (tgv.getOrLoadClassNode().getGetterMethods() != null) {
            getters.addAll(tgv.getOrLoadClassNode().getGetterMethods());
        }
    }
    TypeGraphVertex portVertex = typeGraph.get(Operator.Port.class.getName());
    List<CompactFieldNode> fields = tgv.getOrLoadClassNode().getPorts();
    if (fields != null) {
        for (CompactFieldNode field : fields) {
            TypeGraphVertex fieldVertex = typeGraph.get(field.getDescription());
            if (isAncestor(portVertex, fieldVertex) && !isNodeInList(ports, field)) {
                ports.add(field);
            }
        }
    }
    ClassSignatureVisitor csv = tgv.getOrLoadClassNode().getCsv();
    Type superC = csv.getSuperClass();
    addReplacement(superC, typeReplacement);
    if (csv.getInterfaces() != null) {
        for (Type it : csv.getInterfaces()) {
            addReplacement(it, typeReplacement);
        }
    }
    for (TypeGraphVertex ancestor : tgv.ancestors) {
        getPublicSetterGetterAndPorts(ancestor, setters, getters, typeReplacement, ports);
    }
}
Also used : CompactClassNode(com.datatorrent.stram.webapp.asm.CompactClassNode) ClassNodeType(com.datatorrent.stram.webapp.asm.ClassNodeType) Type(com.datatorrent.stram.webapp.asm.Type) CompactFieldNode(com.datatorrent.stram.webapp.asm.CompactFieldNode) ClassSignatureVisitor(com.datatorrent.stram.webapp.asm.ClassSignatureVisitor) CompactMethodNode(com.datatorrent.stram.webapp.asm.CompactMethodNode)

Example 2 with CompactFieldNode

use of com.datatorrent.stram.webapp.asm.CompactFieldNode in project apex-core by apache.

the class OperatorDiscoverer method describeOperator.

public JSONObject describeOperator(String clazz) throws Exception {
    TypeGraphVertex tgv = typeGraph.getTypeGraphVertex(clazz);
    if (tgv.isInstantiable()) {
        JSONObject response = new JSONObject();
        JSONArray inputPorts = new JSONArray();
        JSONArray outputPorts = new JSONArray();
        // Get properties from ASM
        JSONObject operatorDescriptor = describeClassByASM(clazz);
        JSONArray properties = operatorDescriptor.getJSONArray("properties");
        properties = enrichProperties(clazz, properties);
        JSONArray portTypeInfo = operatorDescriptor.getJSONArray("portTypeInfo");
        List<CompactFieldNode> inputPortfields = typeGraph.getAllInputPorts(clazz);
        List<CompactFieldNode> outputPortfields = typeGraph.getAllOutputPorts(clazz);
        try {
            for (CompactFieldNode field : inputPortfields) {
                JSONObject inputPort = setFieldAttributes(clazz, field);
                if (!inputPort.has("optional")) {
                    // input port that is not annotated is default to be not optional
                    inputPort.put("optional", false);
                }
                if (!inputPort.has(SCHEMA_REQUIRED_KEY)) {
                    inputPort.put(SCHEMA_REQUIRED_KEY, false);
                }
                inputPorts.put(inputPort);
            }
            for (CompactFieldNode field : outputPortfields) {
                JSONObject outputPort = setFieldAttributes(clazz, field);
                if (!outputPort.has("optional")) {
                    // output port that is not annotated is default to be optional
                    outputPort.put("optional", true);
                }
                if (!outputPort.has("error")) {
                    outputPort.put("error", false);
                }
                if (!outputPort.has(SCHEMA_REQUIRED_KEY)) {
                    outputPort.put(SCHEMA_REQUIRED_KEY, false);
                }
                outputPorts.put(outputPort);
            }
            response.put("name", clazz);
            response.put("properties", properties);
            response.put(PORT_TYPE_INFO_KEY, portTypeInfo);
            response.put("inputPorts", inputPorts);
            response.put("outputPorts", outputPorts);
            String type = null;
            Class<?> genericOperator = classLoader.loadClass(clazz);
            if (Module.class.isAssignableFrom(genericOperator)) {
                type = GenericOperatorType.MODULE.getType();
            } else if (Operator.class.isAssignableFrom(genericOperator)) {
                type = GenericOperatorType.OPERATOR.getType();
            }
            if (type != null) {
                response.put("type", type);
            }
            OperatorClassInfo oci = classInfo.get(clazz);
            if (oci != null) {
                if (oci.comment != null) {
                    String[] descriptions;
                    // first look for a <p> tag
                    String keptPrefix = "<p>";
                    descriptions = oci.comment.split("<p>", 2);
                    if (descriptions.length == 0) {
                        keptPrefix = "";
                        // if no <p> tag, then look for a blank line
                        descriptions = oci.comment.split("\n\n", 2);
                    }
                    if (descriptions.length > 0) {
                        response.put("shortDesc", descriptions[0]);
                    }
                    if (descriptions.length > 1) {
                        response.put("longDesc", keptPrefix + descriptions[1]);
                    }
                }
                response.put("category", oci.tags.get("@category"));
                String displayName = oci.tags.get("@displayName");
                if (displayName == null) {
                    displayName = decamelizeClassName(ClassUtils.getShortClassName(clazz));
                }
                response.put("displayName", displayName);
                String tags = oci.tags.get("@tags");
                if (tags != null) {
                    JSONArray tagArray = new JSONArray();
                    for (String tag : StringUtils.split(tags, ',')) {
                        tagArray.put(tag.trim().toLowerCase());
                    }
                    response.put("tags", tagArray);
                }
                String doclink = oci.tags.get("@doclink");
                if (doclink != null) {
                    response.put("doclink", doclink + "?" + getDocName(clazz));
                } else if (clazz.startsWith("com.datatorrent.lib.") || clazz.startsWith("com.datatorrent.contrib.")) {
                    response.put("doclink", DT_OPERATOR_DOCLINK_PREFIX + "?" + getDocName(clazz));
                }
            }
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        return response;
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : Operator(com.datatorrent.api.Operator) GenericOperator(com.datatorrent.api.DAG.GenericOperator) TypeGraphVertex(com.datatorrent.stram.webapp.TypeGraph.TypeGraphVertex) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) CompactFieldNode(com.datatorrent.stram.webapp.asm.CompactFieldNode) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 3 with CompactFieldNode

use of com.datatorrent.stram.webapp.asm.CompactFieldNode in project apex-core by apache.

the class TypeGraph method getPortTypeInfo.

private Collection<JSONObject> getPortTypeInfo(String clazzName, Map<Type, Type> typeReplacement, List<CompactFieldNode> ports) throws JSONException {
    TypeGraphVertex tgv = typeGraph.get(clazzName);
    if (tgv == null) {
        return null;
    }
    Collection<JSONObject> portInfo = new ArrayList<>();
    for (CompactFieldNode port : ports) {
        Type fieldType = port.getFieldSignatureNode().getFieldType();
        Type t = fieldType;
        if (fieldType instanceof ParameterizedTypeNode) {
            // TODO: Right now getPortInfo assumes a single parameterized type
            t = ((ParameterizedTypeNode) fieldType).getActualTypeArguments()[0];
        } else {
            // TODO: Check behavior for Ports not using Default Input/output ports
            TypeGraphVertex portVertex = typeGraph.get(port.getDescription());
            t = findTypeArgument(portVertex, typeReplacement);
            LOG.debug("Field is of type {}", fieldType.getClass());
        }
        JSONObject meta = new JSONObject();
        try {
            meta.put("name", port.getName());
            setTypes(meta, t, typeReplacement);
            portInfo.add(meta);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }
    return portInfo;
}
Also used : ClassNodeType(com.datatorrent.stram.webapp.asm.ClassNodeType) Type(com.datatorrent.stram.webapp.asm.Type) JSONObject(org.codehaus.jettison.json.JSONObject) ParameterizedTypeNode(com.datatorrent.stram.webapp.asm.Type.ParameterizedTypeNode) ArrayList(java.util.ArrayList) CompactFieldNode(com.datatorrent.stram.webapp.asm.CompactFieldNode) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 4 with CompactFieldNode

use of com.datatorrent.stram.webapp.asm.CompactFieldNode in project apex-core by apache.

the class TypeGraph method addClassPropertiesAndPorts.

private void addClassPropertiesAndPorts(String clazzName, JSONObject desc) throws JSONException {
    TypeGraphVertex tgv = typeGraph.get(clazzName);
    if (tgv == null) {
        return;
    }
    Map<String, JSONObject> results = new TreeMap<>();
    List<CompactMethodNode> getters = new LinkedList<>();
    List<CompactMethodNode> setters = new LinkedList<>();
    Map<Type, Type> typeReplacement = new HashMap<>();
    List<CompactFieldNode> ports = new LinkedList<>();
    getPublicSetterGetterAndPorts(tgv, setters, getters, typeReplacement, ports);
    desc.put("portTypeInfo", getPortTypeInfo(clazzName, typeReplacement, ports));
    for (CompactMethodNode setter : setters) {
        String prop = WordUtils.uncapitalize(setter.getName().substring(3));
        JSONObject propJ = results.get(prop);
        if (propJ == null) {
            propJ = new JSONObject();
            propJ.put("name", prop);
            results.put(prop, propJ);
        }
        propJ.put("canSet", true);
        propJ.put("canGet", false);
        MethodSignatureVisitor msv = null;
        msv = setter.getMethodSignatureNode();
        if (msv == null) {
            continue;
        }
        List<Type> param = msv.getParameters();
        if (CollectionUtils.isEmpty(param)) {
            propJ.put("type", "UNKNOWN");
        } else {
            // only one param in setter method
            setTypes(propJ, param.get(0), typeReplacement);
        // propJ.put("type", param.getTypeObj().getClassName());
        }
    // propJ.put("type", typeString);
    }
    for (CompactMethodNode getter : getters) {
        int si = getter.getName().startsWith("is") ? 2 : 3;
        String prop = WordUtils.uncapitalize(getter.getName().substring(si));
        JSONObject propJ = results.get(prop);
        if (propJ == null) {
            propJ = new JSONObject();
            propJ.put("name", prop);
            results.put(prop, propJ);
            propJ.put("canSet", false);
            // propJ.put("type", Type.getReturnType(getter.desc).getClassName());
            MethodSignatureVisitor msv = null;
            msv = getter.getMethodSignatureNode();
            if (msv == null) {
                continue;
            }
            Type rt = msv.getReturnType();
            if (rt == null) {
                propJ.put("type", "UNKNOWN");
            } else {
                setTypes(propJ, rt, typeReplacement);
            // propJ.put("type", param.getTypeObj().getClassName());
            }
        }
        propJ.put("canGet", true);
    }
    desc.put("properties", results.values());
}
Also used : HashMap(java.util.HashMap) CompactFieldNode(com.datatorrent.stram.webapp.asm.CompactFieldNode) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) ClassNodeType(com.datatorrent.stram.webapp.asm.ClassNodeType) Type(com.datatorrent.stram.webapp.asm.Type) JSONObject(org.codehaus.jettison.json.JSONObject) MethodSignatureVisitor(com.datatorrent.stram.webapp.asm.MethodSignatureVisitor) CompactMethodNode(com.datatorrent.stram.webapp.asm.CompactMethodNode)

Aggregations

CompactFieldNode (com.datatorrent.stram.webapp.asm.CompactFieldNode)4 ClassNodeType (com.datatorrent.stram.webapp.asm.ClassNodeType)3 Type (com.datatorrent.stram.webapp.asm.Type)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 CompactMethodNode (com.datatorrent.stram.webapp.asm.CompactMethodNode)2 JSONException (org.codehaus.jettison.json.JSONException)2 GenericOperator (com.datatorrent.api.DAG.GenericOperator)1 Operator (com.datatorrent.api.Operator)1 TypeGraphVertex (com.datatorrent.stram.webapp.TypeGraph.TypeGraphVertex)1 ClassSignatureVisitor (com.datatorrent.stram.webapp.asm.ClassSignatureVisitor)1 CompactClassNode (com.datatorrent.stram.webapp.asm.CompactClassNode)1 MethodSignatureVisitor (com.datatorrent.stram.webapp.asm.MethodSignatureVisitor)1 ParameterizedTypeNode (com.datatorrent.stram.webapp.asm.Type.ParameterizedTypeNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 TreeMap (java.util.TreeMap)1 JSONArray (org.codehaus.jettison.json.JSONArray)1