[Demonstration 1]

[Buggy Method]
public void register(Context context) {
    IntentFilter filter = new IntentFilter(ACTION_CROP_COMPLETED);
    context.registerReceiver(this, filter);
}

[Buggy Hunk]
context.registerReceiver(this, filter);

[Patch]
LocalBroadcastManager.getInstance(context).registerReceiver(this, filter);

[Demonstration 2]

[Buggy Method]
public static void initSubscriber() {
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
    final MqttClient client;
    String accessToken = null;
    MqttConnectOptions options = new MqttConnectOptions();
    //TODO:: Use constant strings
    options.setWill("senseme/disconnection", "Connection-Lost".getBytes(StandardCharsets.UTF_8), 2, true);
    String[] tags = { "sense_me" };
    accessToken = generateAccessToken("alertme-event-receiver", tags);
    if (accessToken != null) {
        options.setUserName(accessToken);
        options.setPassword("".toCharArray());
        options.setCleanSession(false);
    }
    try {
        client = new MqttClient("tcp://localhost:1886", "SenseMeSubscription", null);
        //TODO:: Need to check for debug
        log.info("MQTT subscriber was created with ClientID : " + "SenseMeSubscription");
        client.setCallback(new MqttCallback() {

            public void messageArrived(String topic, MqttMessage message) throws Exception {
                Gson gson = new Gson();
                Event event = gson.fromJson(message.toString(), Event.class);
                String deviceID = event.getMetaData().getDeviceId();
                int ultraSonicReading = event.getPayloadData().getULTRASONIC();
                Device device = new Device();
                device.setDeviceIdentifier(deviceID);
                String payload = "{\n" + "\t\"activate\": true\n" + "}";
                List<Device> mappedDevice = deviceTypeDAO.getDeviceTypeDAO().retrieveDeviceMappings(device);
                for (Device deviceEntry : mappedDevice) {
                    int distance = Integer.parseInt(deviceEntry.getProperties().get(0).getValue());
                    if (ultraSonicReading < distance) {
                        client.publish("carbon.super/alertme/" + deviceEntry.getDeviceIdentifier() + "/notify", payload.getBytes(StandardCharsets.UTF_8), 0, false);
                    }
                }
            }

            public void connectionLost(Throwable cause) {
                log.info("delivery lost");
            }

            public void deliveryComplete(IMqttDeliveryToken token) {
                log.info("delivery complete");
            }
        });
        client.connect(options);
        client.subscribe("carbon.super/senseme/+/ULTRASONIC");
    } catch (MqttException ex) {
        //TODO:: Remove unnecessary formatting and print exception
        String errorMsg = "MQTT Client Error\n" + "\tReason:  " + ex.getReasonCode() + "\n\tMessage: " + ex.getMessage() + "\n\tLocalMsg: " + ex.getLocalizedMessage() + "\n\tCause: " + ex.getCause() + "\n\tException: " + ex;
        log.error(errorMsg);
        //TODO:: Throw the error out
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}

[Buggy Hunk]
accessToken = generateAccessToken("alertme-event-receiver", tags);

[Patch]
accessToken = generateAccessToken("alertme-event-receiver-" + UUID.randomUUID().toString(), tags);

[Demonstration 3]

[Buggy Method]
synchronized void reconnect() {
    if (myClient == null) {
        service.traceError(TAG, "Reconnect myClient = null. Will not do reconnect");
        return;
    }
    if (isConnecting) {
        service.traceDebug(TAG, "The client is connecting. Reconnect return directly.");
        return;
    }
    if (!service.isOnline()) {
        service.traceDebug(TAG, "The network is not reachable. Will not do reconnect");
        return;
    }
    if (connectOptions.isAutomaticReconnect()) {
        //The Automatic reconnect functionality is enabled here
        Log.i(TAG, "Requesting Automatic reconnect using New Java AC");
        final Bundle resultBundle = new Bundle();
        resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, reconnectActivityToken);
        resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);
        resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.CONNECT_ACTION);
        try {
            myClient.reconnect();
        } catch (MqttException ex) {
            Log.e(TAG, "Exception occurred attempting to reconnect: " + ex.getMessage());
            setConnectingState(false);
            handleException(resultBundle, ex);
        }
    } else if (disconnected && !cleanSession) {
        // use the activityToke the same with action connect
        service.traceDebug(TAG, "Do Real Reconnect!");
        final Bundle resultBundle = new Bundle();
        resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, reconnectActivityToken);
        resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);
        resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.CONNECT_ACTION);
        try {
            IMqttActionListener listener = new MqttConnectionListener(resultBundle) {

                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    // since the device's cpu can go to sleep, acquire a
                    // wakelock and drop it later.
                    service.traceDebug(TAG, "Reconnect Success!");
                    service.traceDebug(TAG, "DeliverBacklog when reconnect.");
                    doAfterConnectSuccess(resultBundle);
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, exception.getLocalizedMessage());
                    resultBundle.putSerializable(MqttServiceConstants.CALLBACK_EXCEPTION, exception);
                    service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
                    doAfterConnectFail(resultBundle);
                }
            };
            myClient.connect(connectOptions, null, listener);
            setConnectingState(true);
        } catch (MqttException e) {
            service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage());
            setConnectingState(false);
            handleException(resultBundle, e);
        } catch (Exception e) {
            /*  TODO: Added Due to: https://github.com/eclipse/paho.mqtt.android/issues/101
				    For some reason in a small number of cases, myClient is null here and so
				    a NullPointer Exception is thrown. This is a workaround to pass the exception
				    up to the application. myClient should not be null so more investigation is
				    required.
				*/
            service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage());
            setConnectingState(false);
            MqttException newEx = new MqttException(MqttException.REASON_CODE_UNEXPECTED_ERROR, e.getCause());
            handleException(resultBundle, newEx);
        }
    }
}

[Buggy Hunk]
} catch (MqttException ex){

[Patch]
} catch (Exception ex){