Commit 0caf80fe authored by Ryan Berkheimer's avatar Ryan Berkheimer

completed factoring of fieldutils into standalone class

parent 94b35bfe
......@@ -3,15 +3,14 @@
#include <jni.h>
/**
Constructor for the FieldUtils object.
*/
FieldUtils::FieldUtils(JNIEnv *jvm, ListUtils *listUtils)
/** Constructor */
FieldUtils::FieldUtils(JNIEnv *jvm, TypeUtils *TypeUtils, ListUtils *listUtils)
{
this->loadGlobalRefs(jvm, listUtils);
this->loadGlobalRefs(jvm, typeUtils, listUtils);
this->loadMethodIds();
}
/* Destructor */
FieldUtils::~FieldUtils()
{
try
......@@ -22,12 +21,234 @@ FieldUtils::~FieldUtils()
}
}
void FieldUtils::loadGlobalRefs(JNIEnv *jvm, ListUtils *listUtils)
/* Public API */
const char *FieldUtils::getId(struct field *field)
{
jstring jFieldId = static_cast<jstring>(this->jvm->CallObjectMethod(field->jfield, this->getIdMethodId));
const char *fieldId = this->typeUtils->fromJavaString(jFieldId);
this->jvm->DeleteLocalRef(jFieldId);
return fieldId;
}
const char *FieldUtils::getType(struct field *field)
{
jstring jFieldType = static_cast<jstring>(this->jvm->CallObjectMethod(field->jfield, this->getTypeMethodId));
const char *fieldType = this->typeUtils->fromJavaString(jFieldType);
this->jvm->DeleteLocalRef(jFieldType);
return fieldType;
}
bool FieldUtils::isValid(struct field *field)
{
return (bool)this->jvm->CallBooleanMethod(field->jfield, this->isValidMethodId);
}
bool FieldUtils::isRequired(struct field *field)
{
return (bool)this->jvm->CallBooleanMethod(field->jfield, this->isRequiredMethodId);
}
bool FieldUtils::isNull(struct field *field)
{
struct val *value = this->getVal(field);
if (value->jvalue == NULL)
{
return true;
}
else
{
return false;
}
}
struct val *FieldUtils::getVal(struct field *field)
{
jobject jFieldValue = this->jvm->CallObjectMethod(field->jfield, this->getValueMethodId);
struct val *value = (struct val *)malloc(sizeof(value) + sizeof(jFieldValue));
value->jvalue = jFieldValue;
return value;
}
int FieldUtils::getIntVal(struct field *field)
{
struct val *value = this->getVal(field);
int intVal = (int)this->jvm->CallIntMethod(value->jvalue, this->typeUtils->getIntMethod());
return intVal;
}
long FieldUtils::getLongVal(struct field *field)
{
struct val *value = this->getVal(field);
long longVal = (long)this->jvm->CallLongMethod(value->jvalue, this->typeUtils->getLongMethod());
return longVal;
}
float FieldUtils::getFloatVal(struct field *field)
{
struct val *value = this->getVal(field);
float floatVal = (float)this->jvm->CallFloatMethod(value->jvalue, this->typeUtils->getFloatMethod());
return floatVal;
}
double FieldUtils::getDoubleVal(struct field *field)
{
struct val *value = this->getVal(field);
double doubleVal = (double)this->jvm->CallDoubleMethod(value->jvalue, this->typeUtils->getDoubleMethod());
return doubleVal;
}
signed char FieldUtils::getByteVal(struct field *field)
{
struct val *value = this->getVal(field);
jbyte jByte = this->jvm->CallByteMethod(value->jvalue, this->typeUtils->getByteMethod());
return (signed char)jByte;
}
const char *FieldUtils::getStringVal(struct field *field)
{
struct val *value = this->getVal(field);
jstring jString = (jstring)value->jvalue;
const char *returnString = this->typeUtils->fromJavaString(jString);
jvm->DeleteLocalRef(jString);
return returnString;
}
bool FieldUtils::getBoolVal(struct field *field)
{
struct val *value = this->getVal(field);
bool boolVal = (bool)jvm->CallBooleanMethod(value->jvalue, this->typeUtils->getBoolMethod());
return boolVal;
}
short FieldUtils::getShortVal(struct field *field)
{
struct val *value = this->getVal(field);
short shortVal = (short)this->jvm->CallShortMethod(value->jvalue, this->typeUtils->getShortMethod());
return shortVal;
}
struct val_list *FieldUtils::getListVal(struct field *field)
{
struct val *value = this->getVal(field);
int itemCount = this->listUtils->getListLength(value->jvalue);
struct val_list *valueList = (struct val_list *)malloc(sizeof(struct val_list));
valueList->count = itemCount;
valueList->jlist = value->jvalue;
return valueList;
}
void FieldUtils::setVal(struct field *field, struct val *value)
{
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, value->jvalue);
}
void FieldUtils::setIntVal(struct field *field, int value)
{
jobject jIntVal = jvm->NewObject(this->typeUtils->getIntClass(), this->typeUtils->createIntMethod(), (jint)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jIntVal);
this->jvm->DeleteLocalRef(jIntVal);
}
void FieldUtils::setLongVal(struct field *field, long value)
{
jobject jLongVal = jvm->NewObject(this->typeUtils->getLongClass(), this->typeUtils->createLongMethod(), (jlong)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jLongVal);
this->jvm->DeleteLocalRef(jLongVal);
}
void FieldUtils::setFloatVal(struct field *field, float value)
{
jobject jFloatVal = jvm->NewObject(this->typeUtils->getFloatClass(), this->typeUtils->createFloatMethod(), (jfloat)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jFloatVal);
this->jvm->DeleteLocalRef(jFloatVal);
}
void FieldUtils::setDoubleVal(struct field *field, double value)
{
jobject jDoubleVal = jvm->NewObject(this->typeUtils->getDoubleClass(), this->typeUtils->createDoubleMethod(), (jdouble)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jDoubleVal);
this->jvm->DeleteLocalRef(jDoubleVal);
}
void FieldUtils::setByteVal(struct field *field, signed char value)
{
jobject jByteVal = jvm->NewObject(this->typeUtils->getByteClass(), this->typeUtils->createByteMethod(), (jbyte)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jByteVal);
this->jvm->DeleteLocalRef(jByteVal);
}
void FieldUtils::setStringVal(struct field *field, const char *value)
{
jstring jStringVal = this->typeUtils->toJavaString(value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jStringVal);
this->jvm->DeleteLocalRef(jStringVal);
}
void FieldUtils::setBoolVal(struct field *field, bool value)
{
jobject jBoolVal = jvm->NewObject(this->typeUtils->getBoolClass(), this->typeUtils->createBoolMethod(), (jboolean)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jBoolVal);
this->jvm->DeleteLocalRef(jBoolVal);
}
void FieldUtils::setShortVal(struct field *field, short value)
{
jobject jShortVal = jvm->NewObject(this->typeUtils->getShortClass(), this->typeUtils->createShortMethod(), (jshort)value);
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, jShortVal);
this->jvm->DeleteLocalRef(jShortVal);
}
void FieldUtils::setListVal(struct field *field, struct val_list *value)
{
this->jvm->CallVoidMethod(field->jfield, this->setValueMethodId, value->jlist);
}
/* Private Methods */
void FieldUtils::loadGlobalRefs(JNIEnv *jvm, TypeUtils *typeUtils, ListUtils *listUtils)
{
this->jvm = jvm;
this->typeUtils = typeUtils;
this->listUtils = listUtils;
}
void FieldUtils::loadMethodIds()
{
}
\ No newline at end of file
jclass fieldClass = JniUtils::getNamedClass(this->jvm, "gov/noaa/messageapi/interfaces/IField");
this->getIdMethodId = JniUtils::getMethod(this->jvm, fieldClass, "getId", this->getMethodSignature("getId"), false);
this->getTypeMethodId = JniUtils::getMethod(this->jvm, fieldClass, "getType", this->getMethodSignature("getType"), false);
this->getValueMethodId = JniUtils::getMethod(this->jvm, fieldClass, "getValue", this->getMethodSignature("getValue"), false);
this->isValidMethodId = JniUtils::getMethod(this->jvm, fieldClass, "isValid", this->getMethodSignature("isValid"), false);
this->isRequiredMethodId = JniUtils::getMethod(this->jvm, fieldClass, "isRequired", this->getMethodSignature("isRequired"), false);
this->setValueMethodId = JniUtils::getMethod(this->jvm, fieldClass, "setValue", this->getMethodSignature("setValue"), false);
this->jvm->DeleteLocalRef(fieldClass);
}
const char *FieldUtils::getMethodSignature(const char *methodName)
{
if (strcmp(methodName, "getId") == 0)
{
return "()Ljava/lang/String;";
}
else if (strcmp(methodName, "getType") == 0)
{
return "()Ljava/lang/String;";
}
else if (strcmp(methodName, "getValue") == 0)
{
return "()Ljava/lang/Object;";
}
else if (strcmp(methodName, "isValid") == 0)
{
return "()Z";
}
else if (strcmp(methodName, "isRequired") == 0)
{
return "()Z";
}
else if (strcmp(methodName, "setValue") == 0)
{
return "(Ljava/lang/Object;)V";
}
return NULL;
}
......@@ -11,6 +11,7 @@
#include <string>
#include "JniUtils.h"
#include "TypeUtils.h"
#include "ListUtils.h"
/**
......@@ -22,17 +23,54 @@ class FieldUtils
public:
/*Default constructor/destructors*/
FieldUtils(JNIEnv *javaEnv, ListUtils *listUtils);
FieldUtils(JNIEnv *javaEnv, TypeUtils *typeUtils, ListUtils *listUtils);
~FieldUtils();
/*Field Methods*/
const char *getId(struct field *field);
const char *getType(struct field *field);
bool isValid(struct field *field);
bool isRequired(struct field *field);
bool isNull(struct field *field);
struct val *getVal(struct field *field);
int getIntVal(struct field *field);
long getLongVal(struct field *field);
float getFloatVal(struct field *field);
double getDoubleVal(struct field *field);
signed char getByteVal(struct field *field);
const char *getStringVal(struct field *field);
bool getBoolVal(struct field *field);
short getShortVal(struct field *field);
struct val_list *getListVal(struct field *field);
void setVal(struct field *field, struct val *value);
void setIntVal(struct field *field, int value);
void setLongVal(struct field *field, long value);
void setFloatVal(struct field *field, float value);
void setDoubleVal(struct field *field, double value);
void setByteVal(struct field *field, signed char value);
void setStringVal(struct field *field, const char *value);
void setBoolVal(struct field *field, bool value);
void setShortVal(struct field *field, short value);
void setListVal(struct field *field, struct val_list *value);
private:
/*Vars*/
JNIEnv *jvm;
TypeUtils *typeUtils;
ListUtils *listUtils;
/*Field Methods*/
jmethodID getIdMethodId;
jmethodID getTypeMethodId;
jmethodID getValueMethodId;
jmethodID isValidMethodId;
jmethodID isRequiredMethodId;
jmethodID setValueMethodId;
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadMethodIds();
void loadGlobalRefs(JNIEnv *env, ListUtils *listUtils);
void loadGlobalRefs(JNIEnv *env, TypeUtils *typeUtils, ListUtils *listUtils);
/*Grouped methods for returning the matching method signature string for a given interface*/
const char *getMethodSignature(const char *methodName);
......
This diff is collapsed.
......@@ -16,6 +16,7 @@
#include "ProtocolRecordUtils.h"
#include "RecordUtils.h"
#include "RejectionUtils.h"
#include "FieldUtils.h"
#include "PacketUtils.h"
/**
......@@ -41,37 +42,11 @@ public:
ProtocolRecordUtils *getProtocolRecordUtils();
RecordUtils *getRecordUtils();
RejectionUtils *getRejectionUtils();
FieldUtils *getFieldUtils();
PacketUtils *getPacketUtils();
ListUtils *getListUtils();
TypeUtils *getTypeUtils();
/*Field Methods*/
const char *getFieldId(struct field *field);
const char *getFieldType(struct field *field);
bool getFieldIsValid(struct field *field);
bool getFieldIsRequired(struct field *field);
bool getFieldIsNull(struct field *field);
struct val *getFieldVal(struct field *field);
int getFieldIntVal(struct field *field);
long getFieldLongVal(struct field *field);
float getFieldFloatVal(struct field *field);
double getFieldDoubleVal(struct field *field);
signed char getFieldByteVal(struct field *field);
const char *getFieldStringVal(struct field *field);
bool getFieldBoolVal(struct field *field);
short getFieldShortVal(struct field *field);
struct val_list *getFieldListVal(struct field *field);
void setFieldVal(struct field *field, struct val *value);
void setFieldIntVal(struct field *field, int value);
void setFieldLongVal(struct field *field, long value);
void setFieldFloatVal(struct field *field, float value);
void setFieldDoubleVal(struct field *field, double value);
void setFieldByteVal(struct field *field, signed char value);
void setFieldStringVal(struct field *field, const char *value);
void setFieldBoolVal(struct field *field, bool value);
void setFieldShortVal(struct field *field, short value);
void setFieldListVal(struct field *field, struct val_list *value);
/*Condition Methods*/
const char *getConditionId(struct condition *condition);
const char *getConditionType(struct condition *condition);
......@@ -108,18 +83,11 @@ private :
ProtocolRecordUtils *protocolRecordUtils;
RecordUtils *recordUtils;
RejectionUtils *rejectionUtils;
FieldUtils *fieldUtils;
PacketUtils *packetUtils;
TypeUtils *typeUtils;
ListUtils *listUtils;
/*Field Methods*/
jmethodID getFieldIdMethodId;
jmethodID getFieldTypeMethodId;
jmethodID getFieldValueMethodId;
jmethodID getFieldIsValidMethodId;
jmethodID getFieldIsRequiredMethodId;
jmethodID setFieldValueMethodId;
/*Condition Methods*/
jmethodID getConditionIdMethodId;
jmethodID getConditionTypeMethodId;
......@@ -129,11 +97,9 @@ private :
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadFieldMethodIds();
void loadConditionMethodIds();
/*Grouped methods for returning the matching method signature string for a given interface*/
const char *getFieldMethodSignature(const char *fieldMethodName);
const char *getConditionMethodSignature(const char *conditionMethodName);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment