[Demonstration 1]

[Buggy Method]
@RequestMapping(value = "/{gid}/role/{rid}/privilege", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
public Map<String, Object> insertGeroRolePrivilege(@PathVariable("rid") int rid, @PathVariable("gid") int gid, @RequestBody String inJson) {
    //		checkGero(gid);
    BasicReturnedJson result = new BasicReturnedJson();
    Role role = getRoleFromId(rid);
    checkRoleInGero(role, gid);
    Map<String, Object> requestBodyParamMap = ParamUtils.getMapByJson(inJson, logger);
    List<Integer> privilegeIdList = new ArrayList<Integer>();
    // 输入参数检查
    try {
        privilegeIdList = (List<Integer>) requestBodyParamMap.get("insert_privilege_ids");
        if (privilegeIdList == null) {
            throw new Exception();
        }
    } catch (Exception e) {
        String message = ErrorConstants.format(ErrorConstants.GERO_ROLE_PRIVILEGE_INSERT_PARAM_INVALID, "[insert_privilege_ids=" + requestBodyParamMap.get("insert_privilege_ids") + "]");
        logger.error(message);
        throw new RestException(HttpStatus.BAD_REQUEST, message);
    }
    //获取当前用户名
    User user = getCurrentUser();
    //获取当前用户权限
    List<Privilege> privilegeList = UserUtils.getPrivilegeList(user);
    if (privilegeList == null) {
        String message = ErrorConstants.format(ErrorConstants.GET_USER_PRIVILEGE_FAILED, "[" + "]");
        logger.error(message);
        throw new RestException(HttpStatus.UNAUTHORIZED, message);
    }
    //取新增权限列表并排序
    List<Privilege> inputPrivilegeList = new ArrayList<Privilege>();
    Map<String, String> inputPrivilegeMap = new HashMap<String, String>();
    for (int id : privilegeIdList) {
        for (Privilege privilege : privilegeList) {
            if (privilege.getId() == id) {
                inputPrivilegeList.add(privilege);
                inputPrivilegeMap.put(id + "", privilege.getParentIds());
            }
        }
    }
    // 将列表排序
    try {
        inputPrivilegeMap = sortMapByValue(inputPrivilegeMap);
    } catch (Exception e) {
        String message = ErrorConstants.format(ErrorConstants.SORT_PRIVILEGE_FAILED, "[" + "]");
        logger.error(message);
        throw new RestException(HttpStatus.UNAUTHORIZED, message);
    }
    // 获取角色的权限
    List<Privilege> rolePrivileges = getRolePrivileges(role);
    // 遍历排序过的权限列表
    for (Map.Entry<String, String> entry : inputPrivilegeMap.entrySet()) {
        int id = Integer.parseInt(entry.getKey());
        logger.debug(entry.getKey() + " " + entry.getValue());
        Privilege inputPrivilege = new Privilege();
        for (Privilege privilege : inputPrivilegeList) {
            if (privilege.getId() == id)
                inputPrivilege = privilege;
        }
        //insert privilege
        //判断权限是否存在，父节点是否存在
        Boolean parentExist = false;
        Boolean exist = false;
        for (Privilege privilege : rolePrivileges) {
            if (privilege.getId() == id) {
                // 已存在该权限
                exist = true;
                //					throw new RestException();
            }
            if (inputPrivilege.getParentId() == privilege.getId()) {
                // 父节点不存在
                parentExist = true;
            }
        }
        if (parentExist == false) {
            String message = ErrorConstants.format(ErrorConstants.GERO_ROLE_INSERT_PRIVILEGE_NO_PARENT, "[rid:" + rid + "]" + "[privilege_id:" + inputPrivilege.getId() + "]" + "[privilege_name:" + inputPrivilege.getName() + "]");
            logger.error(message);
            throw new RestException(HttpStatus.BAD_REQUEST, message);
        }
        if (exist == false) {
            rolePrivileges.add(inputPrivilege);
        }
    }
    role.setPrivilegeList(rolePrivileges);
    systemService.insertRolePrivilege(role);
    return result.getMap();
}

[Buggy Hunk]
if (inputPrivilege.getParentId() == privilege.getId()) {

[Patch]
if (inputPrivilege.getParentId() == privilege.getId() && inputPrivilege.getParentId() == 0) {

[Demonstration 2]

[Buggy Method]
public static OMElement getResourceDocumentElement(File resourceFile) throws XMLStreamException, IOException {
    OMElement resourceElement;
    FileInputStream fis = null;
    XMLStreamReader reader = null;
    try {
        fis = FileUtils.openInputStream(resourceFile);
        reader = xif.createXMLStreamReader(fis);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        resourceElement = builder.getDocumentElement();
        resourceElement.detach();
    } catch (XMLStreamException e) {
        throw new XMLStreamException(e.getMessage(), e);
    } catch (IOException e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (XMLStreamException e) {
            log.error(e.getMessage(), e);
        }
    }
    return resourceElement;
}

[Buggy Hunk]
reader = xif.createXMLStreamReader(fis);

[Patch]
reader = xmlInputFactory.createXMLStreamReader(fis);

[Demonstration 3]

[Buggy Method]
@Override
protected ArrayList<CCuser> doInBackground(CCuser... params) {
    URL url = null;
    ArrayList<CCuser> resultsArray = null;
    try {
        // try establish connection to the web service
        CCuser location = params[0];
        // build the url
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(webServiceURL);
        urlBuilder.append(FindLocationEndpoint);
        urlBuilder.append("?");
        urlBuilder.append(timestampGetKey);
        urlBuilder.append(location.getDate().getTime() / 1000);
        urlBuilder.append("&");
        urlBuilder.append(latitudeGetKey);
        urlBuilder.append(location.getLocation().getLatitude());
        urlBuilder.append("&");
        urlBuilder.append(longitudeGetKey);
        urlBuilder.append(location.getLocation().getLongitude());
        urlBuilder.append("&");
        urlBuilder.append(radGetKey);
        //default of 25km
        urlBuilder.append(25);
        url = new URL(urlBuilder.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int HttpResult = connection.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            // print out response.
            Log.e(TAG, "" + sb.toString());
            JSONArray serverResponseArray = new JSONArray(sb.toString());
            resultsArray = new ArrayList<>();
            for (int i = 0; i < serverResponseArray.length(); i++) {
                JSONObject obj = serverResponseArray.getJSONObject(i);
                CCuser newCCuser = new CCuser();
                newCCuser.setDate(new Date(Long.parseLong(obj.getString("timestamp")) * 1000));
                Location newLocation = new Location("Blank");
                newLocation.setLatitude(obj.getDouble("latitude"));
                newLocation.setLongitude(obj.getDouble("longitude"));
                newCCuser.setLocation(newLocation);
                resultsArray.add(newCCuser);
            }
            Log.e(TAG, "response" + serverResponseArray);
        } else {
            Log.e(TAG, "HttpResult was not OK");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultsArray;
}

[Buggy Hunk]
ArrayList<CCuser> resultsArray = null;

[Patch]
ArrayList<CCuser> resultsArray = new ArrayList<>();