Esempio n. 1
0
  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    // TODO: this wrapping try is for debug only. Remove later.
    final int TIMES = 10;
    List<Long> runsTimer = new ArrayList<Long>(TIMES);
    for (int i = 0; i < TIMES; i++) {
      long startTimer = System.currentTimeMillis();
      try {
        ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
        Shell shell = HandlerUtil.getActiveShellChecked(event);

        if (!(selection instanceof ITextSelection))
          throw new ExecutionException("Not a text selection");

        IFile textSelectionFile =
            (IFile)
                HandlerUtil.getActiveEditorChecked(event).getEditorInput().getAdapter(IFile.class);

        ITextSelection textSelection = (ITextSelection) selection;
        SelectionNodesVisitor selectionNodesVisitor = new SelectionNodesVisitor(textSelection);

        ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(textSelectionFile);
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setSource(compilationUnit);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setResolveBindings(true);
        CompilationUnit jdtCompilationUnit = (CompilationUnit) parser.createAST(null);
        jdtCompilationUnit.accept(selectionNodesVisitor);

        Set<ASTNode> selectionNodes = selectionNodesVisitor.getNodes();
        System.out.println("Selection" + selectionNodes);

        IFeatureExtracter extracter = CIDEFeatureExtracterFactory.getInstance().newExtracter();

        String correspondentClasspath =
            MethodDeclarationSootMethodBridge.getCorrespondentClasspath(textSelectionFile);
        SootManager.configure(correspondentClasspath);
        MethodDeclaration methodDeclaration =
            MethodDeclarationSootMethodBridge.getParentMethod(selectionNodes.iterator().next());
        String declaringMethodClass =
            methodDeclaration.resolveBinding().getDeclaringClass().getQualifiedName();
        MethodDeclarationSootMethodBridge mdsm =
            new MethodDeclarationSootMethodBridge(methodDeclaration);
        SootMethod sootMethod =
            SootManager.getMethodBySignature(
                declaringMethodClass, mdsm.getSootMethodSubSignature());
        Body body = sootMethod.retrieveActiveBody();

        Collection<Unit> unitsInSelection =
            ASTNodeUnitBridge.getUnitsFromLines(
                ASTNodeUnitBridge.getLinesFromASTNodes(selectionNodes, jdtCompilationUnit), body);
        if (unitsInSelection.isEmpty()) {
          System.out.println("the selection doesn't map to any Soot Unit");
          return null;
        }

        FeatureModelInstrumentorTransformer instrumentorTransformer =
            new FeatureModelInstrumentorTransformer(extracter, correspondentClasspath);
        instrumentorTransformer.transform2(body, correspondentClasspath);

        FeatureTag<Set<String>> bodyFeatureTag =
            (FeatureTag<Set<String>>) body.getTag("FeatureTag");

        BriefUnitGraph bodyGraph = new BriefUnitGraph(body);
        LiftedReachingDefinitions reachingDefinitions =
            new LiftedReachingDefinitions(bodyGraph, bodyFeatureTag.getFeatures());
        reachingDefinitions.execute();

        Map<Pair<Unit, Set<String>>, Set<Unit>> createProvidesConfigMap =
            createProvidesConfigMap(unitsInSelection, reachingDefinitions, body);
        System.out.println(createProvidesConfigMap);
        String message = createMessage(createProvidesConfigMap);

        // EmergentPopup.pop(shell, message);
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        G.v().reset();
      }
      long estimatedTime = System.currentTimeMillis() - startTimer;
      runsTimer.add(estimatedTime);
    }
    System.out.println(runsTimer);
    return null;
  }
Esempio n. 2
0
  private Map<Pair<Unit, Set<String>>, Set<Unit>> createProvidesConfigMap(
      Collection<Unit> unitsInSelection, LiftedReachingDefinitions reachingDefinitions, Body body) {
    Map<Pair<Unit, Set<String>>, Set<Unit>> unitConfigurationMap =
        new HashMap<Pair<Unit, Set<String>>, Set<Unit>>();

    for (Unit unitFromSelection : unitsInSelection) {
      if (unitFromSelection instanceof DefinitionStmt) {
        /*
         * exclude definitions when it's $temp on the leftOp.
         */
        DefinitionStmt definition = (DefinitionStmt) unitFromSelection;
        Local leftOp = (Local) definition.getLeftOp();
        if (leftOp.getName().charAt(0) == '$') {
          continue;
        }

        System.out.println("Definition:" + definition);

        // for every unit in the body...
        Iterator<Unit> iterator = body.getUnits().snapshotIterator();
        while (iterator.hasNext()) {
          Unit nextUnit = iterator.next();
          LiftedFlowSet<Collection<Set<Object>>> liftedFlowAfter =
              reachingDefinitions.getFlowAfter(nextUnit);
          Set<String>[] configurations = liftedFlowAfter.getConfigurations();
          FlowSet[] lattices = liftedFlowAfter.getLattices();
          // and for every configuration...
          for (int configurationIndex = 0;
              configurationIndex < configurations.length;
              configurationIndex++) {
            FlowSet flowSet = lattices[configurationIndex];
            Set<String> currConfiguration = configurations[configurationIndex];
            FeatureTag nextUnitTag = (FeatureTag) nextUnit.getTag("FeatureTag");

            // if the unit belongs to the current configuration...
            if (nextUnitTag.belongsToConfiguration(currConfiguration)) {

              // if the definition reaches this unit...
              if (flowSet.contains(definition)) {
                List<ValueBox> useBoxes = nextUnit.getUseBoxes();
                for (ValueBox vbox : useBoxes) {
                  /*
                   * and the definition is used, add to the
                   * map...
                   */
                  if (vbox.getValue().equivTo(leftOp)) {
                    Pair<Unit, Set<String>> currentPair =
                        new Pair<Unit, Set<String>>(definition, currConfiguration);
                    Set<Unit> unitConfigurationReachesSet = unitConfigurationMap.get(currentPair);

                    if (unitConfigurationReachesSet == null) {
                      unitConfigurationReachesSet = new HashSet<Unit>();
                      unitConfigurationReachesSet.add(nextUnit);
                      unitConfigurationMap.put(currentPair, unitConfigurationReachesSet);
                    } else {
                      unitConfigurationReachesSet.add(nextUnit);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return unitConfigurationMap;
  }