Ejemplo n.º 1
0
  private boolean matchAnnotationConditions(
      GraphRewrite event,
      EvaluationContext context,
      EvaluationStrategy evaluationStrategy,
      JavaTypeReferenceModel model) {
    boolean annotationMatched = true;
    if (this.annotationCondition != null) {
      annotationMatched = model instanceof JavaAnnotationTypeValueModel;

      annotationMatched &=
          annotationCondition.evaluate(
              event, context, evaluationStrategy, (JavaAnnotationTypeValueModel) model);
    }

    if (!additionalAnnotationConditions.isEmpty()) {
      JavaTypeReferenceModel referencedTypeModel;
      if (model.getReferenceLocation() == TypeReferenceLocation.ANNOTATION)
        referencedTypeModel = ((JavaAnnotationTypeReferenceModel) model).getAnnotatedType();
      else referencedTypeModel = model;

      // iterate the conditions and make sure there is at least one matching annotation for each
      for (AnnotationCondition condition : this.additionalAnnotationConditions) {
        boolean oneMatches = false;
        // now get the annotations
        for (JavaAnnotationTypeReferenceModel annotationModel :
            referencedTypeModel.getAnnotations()) {
          if (condition.evaluate(event, context, evaluationStrategy, annotationModel)) {
            oneMatches = true;
          }
        }

        if (!oneMatches) annotationMatched = false;
      }
    }

    return annotationMatched;
  }
Ejemplo 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");
    }
  }