Esempio n. 1
0
 @Override
 public void query(GraphRewrite event, GremlinPipeline<Vertex, Vertex> pipeline) {
   Predicate regexPredicate =
       new Predicate() {
         @Override
         public boolean evaluate(Object first, Object second) {
           return ((String) first).matches((String) second);
         }
       };
   pipeline
       .as("result")
       .out(FileReferenceModel.FILE_MODEL)
       .out(JavaSourceFileModel.JAVA_CLASS_MODEL)
       .has(JavaClassModel.QUALIFIED_NAME, regexPredicate, compiledTypeFilterPattern.pattern())
       .back("result");
 }
Esempio n. 2
0
  private boolean evaluate(
      GraphRewrite event, EvaluationContext context, EvaluationStrategy evaluationStrategy) {
    try {
      ExecutionStatistics.get().begin("JavaClass.evaluate");

      final ParameterStore store = DefaultParameterStore.getInstance(context);
      final Pattern compiledPattern = referencePattern.getCompiledPattern(store);

      /*
       * Only set in the case of a query with no "from" variable.
       */
      String initialQueryID = null;

      QueryBuilderFrom query;
      initialQueryID = "iqi." + UUID.randomUUID().toString();

      // prepare initialQueryID
      if (!StringUtils.isBlank(getInputVariablesName())) {
        QueryBuilderFrom fromQuery = Query.from(getInputVariablesName());
        QueryBuilderPiped piped =
            fromQuery.piped(
                new QueryGremlinCriterion() {
                  @Override
                  public void query(GraphRewrite event, GremlinPipeline<Vertex, Vertex> pipeline) {
                    pipeline
                        .out(FileReferenceModel.FILE_MODEL)
                        .in(FileReferenceModel.FILE_MODEL)
                        .has(
                            JavaTypeReferenceModel.RESOLVED_SOURCE_SNIPPIT,
                            Text.REGEX,
                            compiledPattern.toString());
                  }
                });
        piped.as(initialQueryID).evaluate(event, context);
      } else {
        GremlinPipeline<Vertex, Vertex> resolvedTextSearch =
            new GremlinPipeline<>(event.getGraphContext().getGraph());
        resolvedTextSearch.V();
        resolvedTextSearch.has(
            JavaTypeReferenceModel.RESOLVED_SOURCE_SNIPPIT, Text.REGEX, titanify(compiledPattern));

        if (!resolvedTextSearch.iterator().hasNext()) return false;

        Variables.instance(event)
            .setVariable(
                initialQueryID,
                new FramedVertexIterable<>(
                    event.getGraphContext().getFramed(),
                    resolvedTextSearch,
                    JavaTypeReferenceModel.class));
      }
      query = Query.from(initialQueryID);

      if (lineMatchPattern != null) {
        final Pattern compiledLineMatchPattern = lineMatchPattern.getCompiledPattern(store);
        query.withProperty(
            JavaTypeReferenceModel.SOURCE_SNIPPIT,
            QueryPropertyComparisonType.REGEX,
            compiledLineMatchPattern.pattern());
      }
      String uuid = UUID.randomUUID().toString();
      query.as(uuid);

      if (typeFilterPattern != null) {
        Pattern compiledTypeFilterPattern = typeFilterPattern.getCompiledPattern(store);
        query.piped(new TypeFilterCriterion(compiledTypeFilterPattern));
      }
      if (!locations.isEmpty())
        query.withProperty(JavaTypeReferenceModel.REFERENCE_TYPE, locations);

      List<WindupVertexFrame> results = new ArrayList<>();
      if (query.evaluate(event, context)) {
        Iterable<? extends WindupVertexFrame> frames = Variables.instance(event).findVariable(uuid);
        for (WindupVertexFrame frame : frames) {
          FileModel fileModel = ((FileReferenceModel) frame).getFile();
          Iterable<JavaClassModel> javaClasses = null;
          if (fileModel instanceof AbstractJavaSourceModel)
            javaClasses = ((AbstractJavaSourceModel) fileModel).getJavaClasses();
          else if (fileModel instanceof JavaClassFileModel)
            javaClasses = Arrays.asList(((JavaClassFileModel) fileModel).getJavaClass());

          for (JavaClassModel javaClassModel : javaClasses) {
            if (typeFilterPattern == null
                || typeFilterPattern.parse(javaClassModel.getQualifiedName()).matches()) {
              JavaTypeReferenceModel model = (JavaTypeReferenceModel) frame;
              ParameterizedPatternResult referenceResult =
                  referencePattern.parse(model.getResolvedSourceSnippit());
              if (referenceResult.matches()) {
                evaluationStrategy.modelMatched();

                if (referenceResult.submit(event, context)
                    && (typeFilterPattern == null
                        || typeFilterPattern
                            .parse(javaClassModel.getQualifiedName())
                            .submit(event, context))) {
                  boolean annotationMatched =
                      matchAnnotationConditions(event, context, evaluationStrategy, model);
                  if (!annotationMatched) {
                    evaluationStrategy.modelSubmissionRejected();
                  } else {
                    results.add(model);
                    evaluationStrategy.modelSubmitted(model);
                  }
                } else {
                  evaluationStrategy.modelSubmissionRejected();
                }
              }
            }
          }
        }
        Variables.instance(event).removeVariable(uuid);
        if (initialQueryID != null) Variables.instance(event).removeVariable(initialQueryID);

        setResults(event, getVarname(), results);
        return !results.isEmpty();
      }
      return false;
    } finally {
      ExecutionStatistics.get().end("JavaClass.evaluate");
    }
  }