Search in sources :

Example 11 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class DOMPrefixExpression method handle.

@Override
public DSubTree handle() {
    DSubTree sub = new DOMExpression(expression.getOperand(), visitor).handle();
    if (expression.getOperator() == PrefixExpression.Operator.NOT && sub.getNodes().size() == 1) {
        // encode the ! predicate into the API call name
        DAPICall call = (DAPICall) sub.getNodes().get(0);
        call.setNotPredicate();
    }
    return sub;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) DAPICall(edu.rice.cs.caper.bayou.core.dsl.DAPICall)

Example 12 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class DOMSynchronizedStatement method handle.

@Override
public DSubTree handle() {
    DSubTree tree = new DSubTree();
    DSubTree Texpr = new DOMExpression(statement.getExpression(), visitor).handle();
    DSubTree Tbody = new DOMBlock(statement.getBody(), visitor).handle();
    tree.addNodes(Texpr.getNodes());
    tree.addNodes(Tbody.getNodes());
    return tree;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree)

Example 13 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class DOMTryStatement method handle.

@Override
public DSubTree handle() {
    DSubTree tree = new DSubTree();
    // restriction: considering only the first catch clause
    DSubTree Ttry = new DOMBlock(statement.getBody(), visitor).handle();
    DSubTree Tcatch;
    if (!statement.catchClauses().isEmpty())
        Tcatch = new DOMCatchClause((CatchClause) statement.catchClauses().get(0), visitor).handle();
    else
        Tcatch = new DSubTree();
    DSubTree Tfinally = new DOMBlock(statement.getFinally(), visitor).handle();
    boolean except = Ttry.isValid() && Tcatch.isValid();
    if (except)
        tree.addNode(new DExcept(Ttry.getNodes(), Tcatch.getNodes()));
    else {
        // only one of these will add nodes
        tree.addNodes(Ttry.getNodes());
        tree.addNodes(Tcatch.getNodes());
    }
    tree.addNodes(Tfinally.getNodes());
    return tree;
}
Also used : DExcept(edu.rice.cs.caper.bayou.core.dsl.DExcept) DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 14 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class DOMVariableDeclarationExpression method handle.

@Override
public DSubTree handle() {
    DSubTree tree = new DSubTree();
    for (Object o : expression.fragments()) {
        VariableDeclarationFragment fragment = (VariableDeclarationFragment) o;
        DSubTree t = new DOMVariableDeclarationFragment(fragment, visitor).handle();
        tree.addNodes(t.getNodes());
    }
    return tree;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment)

Example 15 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class Visitor method visit.

@Override
public boolean visit(TypeDeclaration clazz) {
    if (clazz.isInterface())
        return false;
    List<TypeDeclaration> classes = new ArrayList<>();
    classes.addAll(Arrays.asList(clazz.getTypes()));
    classes.add(clazz);
    for (TypeDeclaration cls : classes) allMethods.addAll(Arrays.asList(cls.getMethods()));
    List<MethodDeclaration> constructors = allMethods.stream().filter(m -> m.isConstructor()).collect(Collectors.toList());
    List<MethodDeclaration> publicMethods = allMethods.stream().filter(m -> !m.isConstructor() && Modifier.isPublic(m.getModifiers())).collect(Collectors.toList());
    List<String> skeletons = new ArrayList<>();
    List<Multiset<Integer>> cfg3_bfs = new ArrayList<>();
    List<Multiset<Integer>> cfg4_bfs = new ArrayList<>();
    List<Multiset<Integer>> cfg3_dfs = new ArrayList<>();
    List<Multiset<Integer>> cfg4_dfs = new ArrayList<>();
    for (MethodDeclaration m : allMethods) {
        DecoratedSkeletonFeature skeleton = new DecoratedSkeletonFeature(m);
        skeletons.add(skeleton.toString());
        CFGFeature cfg = new CFGFeature(m);
        cfg3_bfs.add(cfg.gen_subgraph(3, true));
        cfg4_bfs.add(cfg.gen_subgraph(4, true));
        cfg3_dfs.add(cfg.gen_subgraph(3, false));
        cfg4_dfs.add(cfg.gen_subgraph(4, false));
    }
    Set<Pair<DSubTree, String>> astsWithJavadoc = new HashSet<>();
    if (!constructors.isEmpty() && !publicMethods.isEmpty()) {
        for (MethodDeclaration c : constructors) for (MethodDeclaration m : publicMethods) {
            String javadoc = Utils.getJavadoc(m, options.JAVADOC_TYPE);
            callStack.push(c);
            DSubTree ast = new DOMMethodDeclaration(c, this).handle();
            callStack.push(m);
            ast.addNodes(new DOMMethodDeclaration(m, this).handle().getNodes());
            callStack.pop();
            callStack.pop();
            if (ast.isValid())
                astsWithJavadoc.add(new ImmutablePair<>(ast, javadoc));
        }
    } else if (!constructors.isEmpty()) {
        // no public methods, only constructor
        for (MethodDeclaration c : constructors) {
            String javadoc = Utils.getJavadoc(c, options.JAVADOC_TYPE);
            callStack.push(c);
            DSubTree ast = new DOMMethodDeclaration(c, this).handle();
            callStack.pop();
            if (ast.isValid())
                astsWithJavadoc.add(new ImmutablePair<>(ast, javadoc));
        }
    } else if (!publicMethods.isEmpty()) {
        // no constructors, methods executed typically through Android callbacks
        for (MethodDeclaration m : publicMethods) {
            String javadoc = Utils.getJavadoc(m, options.JAVADOC_TYPE);
            callStack.push(m);
            DSubTree ast = new DOMMethodDeclaration(m, this).handle();
            callStack.pop();
            if (ast.isValid())
                astsWithJavadoc.add(new ImmutablePair<>(ast, javadoc));
        }
    }
    for (Pair<DSubTree, String> astDoc : astsWithJavadoc) {
        List<Sequence> sequences = new ArrayList<>();
        sequences.add(new Sequence());
        try {
            astDoc.getLeft().updateSequences(sequences, options.MAX_SEQS, options.MAX_SEQ_LENGTH);
            List<Sequence> uniqSequences = new ArrayList<>(new HashSet<>(sequences));
            if (okToPrintAST(uniqSequences))
                addToJson(astDoc.getLeft(), uniqSequences, astDoc.getRight(), skeletons, cfg3_bfs, cfg4_bfs, cfg3_dfs, cfg4_dfs);
        } catch (DASTNode.TooManySequencesException e) {
            System.err.println("Too many sequences from AST");
        } catch (DASTNode.TooLongSequenceException e) {
            System.err.println("Too long sequence from AST");
        }
    }
    return false;
}
Also used : PrintWriter(java.io.PrintWriter) java.util(java.util) org.eclipse.jdt.core.dom(org.eclipse.jdt.core.dom) Multiset(com.google.common.collect.Multiset) IOException(java.io.IOException) DASTNode(edu.rice.cs.caper.bayou.core.dsl.DASTNode) Sequence(edu.rice.cs.caper.bayou.core.dsl.Sequence) Collectors(java.util.stream.Collectors) GsonBuilder(com.google.gson.GsonBuilder) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) FileNotFoundException(java.io.FileNotFoundException) Pair(org.apache.commons.lang3.tuple.Pair) Gson(com.google.gson.Gson) DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Sequence(edu.rice.cs.caper.bayou.core.dsl.Sequence) DASTNode(edu.rice.cs.caper.bayou.core.dsl.DASTNode) DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Multiset(com.google.common.collect.Multiset)

Aggregations

DSubTree (edu.rice.cs.caper.bayou.core.dsl.DSubTree)26 ArrayList (java.util.ArrayList)7 DAPICall (edu.rice.cs.caper.bayou.core.dsl.DAPICall)5 DBranch (edu.rice.cs.caper.bayou.core.dsl.DBranch)3 DLoop (edu.rice.cs.caper.bayou.core.dsl.DLoop)3 Sequence (edu.rice.cs.caper.bayou.core.dsl.Sequence)3 DASTNode (edu.rice.cs.caper.bayou.core.dsl.DASTNode)2 HashSet (java.util.HashSet)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 Multiset (com.google.common.collect.Multiset)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 DExcept (edu.rice.cs.caper.bayou.core.dsl.DExcept)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1