[Demonstration 1]

[Buggy Method]
private synchronized Copy fetchCopy(int localNodeId) {
    Session session = null;
    try {
        session = HibernateUtil.openSession();
        session.beginTransaction();
        @SuppressWarnings("rawtypes")
        List l = null;
        l = session.createSQLQuery("select id from copies c where c.node_id = ?1 " + "order by c.checksumdate asc NULLS FIRST").setParameter("1", localNodeId).setReadOnly(true).list();
        @SuppressWarnings("rawtypes")
        List k = null;
        k = session.createQuery("from Copy c where c.id = ?1 ").setParameter("1", l.get(0)).setReadOnly(true).list();
        Copy copy = (Copy) k.get(0);
        session.close();
        return copy;
    } catch (IndexOutOfBoundsException e) {
        if (session != null)
            session.close();
        return null;
    }
}

[Buggy Hunk]
} catch (IndexOutOfBoundsException e){

[Patch]
} catch (Exception e){

[Demonstration 2]

[Buggy Method]
private static QueryBuilder matchFieldQuery(ElasticFields field, String value) {
    if (field == ElasticFields.ALL) {
        return QueryBuilders.queryStringQuery(value);
    }
    return QueryBuilders.queryStringQuery(field.field() + ":" + value);
}

[Buggy Hunk]
return QueryBuilders.queryStringQuery(value);

[Patch]
return QueryBuilders.queryStringQuery("*" + value + "*");

[Demonstration 3]

[Buggy Method]
protected BoolQueryBuilder buildBooleanConstraints(final PropertyMatchDescriptorHelper pmdh, BoolQueryBuilder bool) {
    final String key = pmdh.getKey();
    final G_Constraint constraint = pmdh.getConstraint();
    boolean constraintUsed = false;
    boolean createdNew = false;
    if (bool == null) {
        bool = QueryBuilders.boolQuery();
        createdNew = true;
    }
    String[] fieldArray = new String[1];
    fieldArray[0] = key;
    final Map<String, ArrayList<String>> fieldMappings = dao.getFieldMappings();
    if (ValidationUtils.isValid(fieldMappings)) {
        final ArrayList<String> specificFields = fieldMappings.get(key);
        if (specificFields != null) {
            fieldArray = specificFields.toArray(new String[specificFields.size()]);
        } else {
            logger.warn("Could not find specific fields for the key " + pmdh.getKey());
        }
        // Try the singleton range first
        if (ValidationUtils.isValid(pmdh.getSingletonRange())) {
            final String text = (String) pmdh.getSingletonRange().getValue();
            switch(constraint) {
                case EQUALS:
                    bool = bool.must(QueryBuilders.multiMatchQuery(text, fieldArray));
                    constraintUsed = true;
                    break;
                case CONTAINS:
                    for (final String sf : fieldArray) {
                        bool = bool.should(QueryBuilders.matchPhraseQuery(sf, text));
                        constraintUsed = true;
                    }
                    break;
                case STARTS_WITH:
                    for (final String sf : fieldArray) {
                        bool = bool.should(QueryBuilders.prefixQuery(sf, text));
                        constraintUsed = true;
                    }
                    break;
                case NOT:
                    for (final String sf : fieldArray) {
                        bool = bool.mustNot(QueryBuilders.matchPhraseQuery(sf, text));
                        constraintUsed = true;
                    }
                    break;
                case LIKE:
                    bool = bool.must(QueryBuilders.fuzzyLikeThisQuery(fieldArray).likeText(text));
                    constraintUsed = true;
                    break;
                default:
                    break;
            }
        } else if (ValidationUtils.isValid(pmdh.getListRange())) {
            for (final Object text : pmdh.getListRange().getValues()) {
                switch(constraint) {
                    case EQUALS:
                        bool = bool.must(QueryBuilders.matchPhraseQuery(key, text));
                        constraintUsed = true;
                        break;
                    case CONTAINS:
                        bool = bool.should(QueryBuilders.matchPhraseQuery(key, text));
                        constraintUsed = true;
                        break;
                    case STARTS_WITH:
                        bool = bool.must(QueryBuilders.matchPhrasePrefixQuery(key, text));
                        constraintUsed = true;
                        break;
                    case NOT:
                        bool = bool.mustNot(QueryBuilders.matchPhraseQuery(key, text));
                        constraintUsed = true;
                        break;
                    default:
                        break;
                }
            }
        } else if (ValidationUtils.isValid(pmdh.getBoundedRange())) {
            final G_BoundedRange br = pmdh.getBoundedRange();
            // Enumerate any specific fields that this range can apply to.
            String[] rangeFieldArray = new String[1];
            rangeFieldArray[0] = key;
            final ArrayList<String> specificRangeFields = dao.getRangeMappings().get(pmdh.getKey());
            if (specificRangeFields != null) {
                rangeFieldArray = specificRangeFields.toArray(new String[specificRangeFields.size()]);
            } else {
                logger.warn("Could not find specific fields for the key " + pmdh.getKey());
            }
            Object start = null;
            Object end = null;
            if (br.getStart() != null) {
                switch(pmdh.getType()) {
                    case LONG:
                        start = br.getStart();
                        break;
                    case STRING:
                        start = br.getStart().toString();
                        break;
                    case DATE:
                        final DateTime dt = (DateTime) br.getStart();
                        start = dt.toString(DATE_FORMAT);
                        break;
                    default:
                        break;
                }
            }
            if (br.getEnd() != null) {
                switch(pmdh.getType()) {
                    case LONG:
                        end = br.getEnd();
                        break;
                    case STRING:
                        end = br.getEnd().toString();
                        break;
                    case DATE:
                        final DateTime dt = (DateTime) br.getEnd();
                        end = dt.toString(DATE_FORMAT);
                        break;
                    default:
                        break;
                }
            }
            for (final String sf : rangeFieldArray) {
                if (ValidationUtils.isValid(start, end)) {
                    bool = bool.should(QueryBuilders.rangeQuery(sf).from(start).to(end));
                    constraintUsed = true;
                } else if (ValidationUtils.isValid(start)) {
                    bool = bool.should(QueryBuilders.rangeQuery(sf).gt(start));
                    constraintUsed = true;
                } else if (ValidationUtils.isValid(end)) {
                    bool = bool.should(QueryBuilders.rangeQuery(sf).lt(end));
                    constraintUsed = true;
                }
            }
        } else {
            logger.error("Unknown range type for " + pmdh);
        }
    } else {
        logger.error("No field mappings available");
    }
    if ((constraintUsed == false) && (createdNew == true)) {
        // boolean was created but not used.
        bool = null;
    }
    return bool;
}

[Buggy Hunk]
bool = bool.should(QueryBuilders.prefixQuery(sf, text));

[Patch]
bool = bool.should(QueryBuilders.prefixQuery(sf, text.toLowerCase()));