Skip to content

Commit

Permalink
KAA-1279: Fixed about 100 warns
Browse files Browse the repository at this point in the history
  • Loading branch information
sashadidukh committed Sep 29, 2016
1 parent 926b41c commit fc69589
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 78 deletions.
Expand Up @@ -25,6 +25,13 @@ public class GenerationContext {
private final String fieldName;
private DirectionType direction;

/**
* Instantiates a new GenerationContext.
*
* @param parentName the parent name
* @param fieldName the field name
* @param direction the direction
*/
public GenerationContext(String parentName, String fieldName, String direction) {
this.parentName = parentName;
this.fieldName = fieldName;
Expand All @@ -41,6 +48,11 @@ public GenerationContext(String parentName, String fieldName, String direction)
}
}

/**
* Update direction type of GenerationContext.
*
* @param context the GenerationContext
*/
public void updateDirection(GenerationContext context) {
if (direction != DirectionType.INOUT && context != null && direction != context.direction) {
direction = DirectionType.INOUT;
Expand Down
Expand Up @@ -21,6 +21,12 @@
import org.kaaproject.kaa.avro.avrogen.compiler.ObjectiveCCompiler;

public class Main {

/**
* The main method.
*
* @param args the input arguments
*/
public static void main(String[] args) {
try {
if (args.length < 3) {
Expand Down
Expand Up @@ -21,47 +21,64 @@ public class StyleUtils {
private StyleUtils() {
}

/**
* Convert to lower underscore format.
*
* @param camelCaseName the name in camel case format
* @return the string in lower underscore format
*/
public static String toLowerUnderScore(String camelCaseName) {
StringBuilder convertedName = new StringBuilder();

for (int i = 0; i < camelCaseName.length(); ++i) {
char c = camelCaseName.charAt(i);
if (Character.isUpperCase(c)) {
c = Character.toLowerCase(c);
char character = camelCaseName.charAt(i);
if (Character.isUpperCase(character)) {
character = Character.toLowerCase(character);
if (convertedName.length() > 0 && ((i + 1) < camelCaseName.length())
&& (Character.isLowerCase(camelCaseName.charAt(i + 1))
|| Character.isLowerCase(camelCaseName.charAt(i - 1)))) {
convertedName.append("_");
}
convertedName.append(c);
convertedName.append(character);
} else {
convertedName.append(c);
convertedName.append(character);
}
}

return convertedName.toString();
}

/**
* Convert to upper underscore format.
*
* @param camelCaseName the input name
* @return the string in upper underscore format
*/
public static String toUpperUnderScore(String camelCaseName) {
StringBuilder convertedName = new StringBuilder();

for (int i = 0; i < camelCaseName.length(); ++i) {
char c = camelCaseName.charAt(i);
if (Character.isUpperCase(c)) {
if (convertedName.length() > 0 && ((i + 1) < camelCaseName.length()) &&
(Character.isLowerCase(camelCaseName.charAt(i + 1))
char character = camelCaseName.charAt(i);
if (Character.isUpperCase(character)) {
if (convertedName.length() > 0 && ((i + 1) < camelCaseName.length())
&& (Character.isLowerCase(camelCaseName.charAt(i + 1))
|| Character.isLowerCase(camelCaseName.charAt(i - 1)))) {
convertedName.append("_");
}
convertedName.append(c);
convertedName.append(character);
} else {
convertedName.append(Character.toUpperCase(c));
convertedName.append(Character.toUpperCase(character));
}
}

return convertedName.toString();
}

/**
* Fix camel humps.
* @param name the input name
* @return the string with fixed camel humps
*/
public static String fixCamelHumps(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name couldn't be null or empty");
Expand Down
Expand Up @@ -32,51 +32,64 @@ public static String convertToCType(Schema schema) {
return convertToCType("kaa", schema);
}

/**
* Convert schema type to the C type.
*
* @param namespace the namespace
* @param schema the schema
* @return the converted schema type
*/
public static String convertToCType(String namespace, Schema schema) {
String cType = "";
String typeC = "";
switch (schema.getType()) {
case BOOLEAN:
cType = "int8_t";
typeC = "int8_t";
break;
case INT:
cType = "int32_t";
typeC = "int32_t";
break;
case LONG:
cType = "int64_t";
typeC = "int64_t";
break;
case FLOAT:
cType = "float";
typeC = "float";
break;
case DOUBLE:
cType = "double";
typeC = "double";
break;
case STRING:
cType = "kaa_string_t *";
typeC = "kaa_string_t *";
break;
case BYTES:
case FIXED:
cType = "kaa_bytes_t *";
typeC = "kaa_bytes_t *";
break;
case ARRAY:
cType = "kaa_list_t *";
typeC = "kaa_list_t *";
break;
case UNION:
cType = "kaa_union_t *";
typeC = "kaa_union_t *";
break;
case ENUM:
cType = namespace + "_" + StyleUtils.toLowerUnderScore(schema.getName()) + "_t";
typeC = namespace + "_" + StyleUtils.toLowerUnderScore(schema.getName()) + "_t";
break;
case RECORD:
cType = namespace + "_" + StyleUtils.toLowerUnderScore(schema.getName()) + "_t *";
typeC = namespace + "_" + StyleUtils.toLowerUnderScore(schema.getName()) + "_t *";
break;
default:
// TODO: add handling
break;
}

return cType;
return typeC;
}

/**
* Convert schema type to the object C type.
*
* @param schema the schema
* @return the converted schema type
*/
public static String convertToObjCType(Schema schema) {
String objCType = "";
switch (schema.getType()) {
Expand Down Expand Up @@ -126,6 +139,13 @@ public static String generateUnionName(Schema schema) {
return generateUnionName("", schema);
}

/**
* Generate union name.
*
* @param prefix the prefix to union name
* @param schema the schema
* @return generated union name
*/
public static String generateUnionName(String prefix, Schema schema) {
StringBuilder builder = new StringBuilder(prefix + "_UNION_");

Expand Down Expand Up @@ -158,23 +178,35 @@ public static String generateUnionName(String prefix, Schema schema) {
return builder.toString();
}

/**
* Check is record need deal locator.
*
* @param schema the input schema
* @return boolean 'true' if record need deal locator
*/
public static boolean isRecordNeedDeallocator(Schema schema) {
if (schema.getType() == Type.RECORD) {
for (Field f : schema.getFields()) {
Type type = f.schema().getType();
if (type == Type.ARRAY || type == Type.BYTES || type == Type.STRING ||
type == Type.FIXED || type == Type.RECORD || type == Type.UNION) {
if (type == Type.ARRAY || type == Type.BYTES || type == Type.STRING
|| type == Type.FIXED || type == Type.RECORD || type == Type.UNION) {
return true;
}
}
}
return false;
}

/**
* Check is schema an Avro primitive.
*
* @param schema the schema
* @return boolean 'true' if schema is an Avro primitive
*/
public static boolean isAvroPrimitive(Schema schema) {
Type type = schema.getType();
return type == Type.BOOLEAN || type == Type.INT || type == Type.LONG ||
type == Type.ENUM || type == Type.FLOAT || type == Type.DOUBLE;
return type == Type.BOOLEAN || type == Type.INT || type == Type.LONG
|| type == Type.ENUM || type == Type.FLOAT || type == Type.DOUBLE;
}

public static boolean isAvroNull(Schema schema) {
Expand Down Expand Up @@ -243,6 +275,12 @@ public static String getLastBranchNumber(Schema schema) {
return "" + (schema.getTypes().size() - 1);
}

/**
* Check if the schema contains union.
*
* @param schema the schema
* @return boolean 'true' if the schema contains union
*/
public static boolean containsUnion(Schema schema) {
for (Field field : schema.getFields()) {
if (isAvroUnion(field.schema())) {
Expand Down
Expand Up @@ -27,22 +27,27 @@

public class ObjectiveCCompiler extends Compiler {

public ObjectiveCCompiler(Schema schema, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException {
public ObjectiveCCompiler(Schema schema, String sourceName, OutputStream hdrS, OutputStream srcS)
throws KaaGeneratorException {
super(schema, sourceName, hdrS, srcS);
setNamespacePrefix("");
}

public ObjectiveCCompiler(String schemaPath, String outputPath, String sourceName) throws KaaGeneratorException {
public ObjectiveCCompiler(String schemaPath, String outputPath, String sourceName)
throws KaaGeneratorException {
super(schemaPath, outputPath, sourceName);
setNamespacePrefix("");
}


public ObjectiveCCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException {
public ObjectiveCCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS,
OutputStream srcS) throws KaaGeneratorException {
super(schemas, sourceName, hdrS, srcS);
}

public ObjectiveCCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS, Set<Schema> generatedSchemas) throws KaaGeneratorException {
public ObjectiveCCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS,
OutputStream srcS, Set<Schema> generatedSchemas)
throws KaaGeneratorException {
super(schemas, sourceName, hdrS, srcS, generatedSchemas);
}

Expand Down Expand Up @@ -82,6 +87,8 @@ protected void doGenerate() {
case ENUM:
processEnum(cursor.getKey(), "ObjC/enumObjC.h.vm");
break;
default:
break;
}
}
}
Expand Down
Expand Up @@ -29,7 +29,7 @@ public interface Constants { //NOSONAR
String RESPONSE_CONTENT_TYPE = "\"application/x-kaa\""; //NOSONAR

/**
* HTTP response custom header for set RSA Signature encoded in base64
* HTTP response custom header for set RSA Signature encoded in base64.
*/
String SIGNATURE_HEADER_NAME = "X-SIGNATURE"; //NOSONAR

Expand Down
Expand Up @@ -28,8 +28,9 @@
import java.io.IOException;

/**
* The Class AvroByteArrayConverter is used to convert {#link org.apache.avro.specific.SpecificRecordBase
* specific Avro records} to/from bytes. NOT Thread safe.
* The Class AvroByteArrayConverter is used to convert
* {#link org.apache.avro.specific.SpecificRecordBase specific Avro records} to/from bytes.
* NOT Thread safe.
*
* @param <T> the generic type that extends SpecificRecordBase
* @author Andrew Shvayka
Expand Down
Expand Up @@ -36,8 +36,9 @@
import java.nio.charset.Charset;

/**
* The Class AvroByteArrayConverter is used to convert {#link org.apache.avro.generic.GenericContainer
* specific avro records} to/from bytes. NOT Thread safe.
* The Class AvroByteArrayConverter is used to convert
* {#link org.apache.avro.generic.GenericContainer specific avro records} to/from bytes.
* NOT Thread safe.
*
* @param <T> the generic type that extends GenericContainer
*/
Expand Down Expand Up @@ -78,47 +79,49 @@ public GenericAvroConverter(Schema schema) {
}

/**
* Convert binary data using schema to Json
* Convert binary data using schema to Json.
*
* @param rawData the encoded data
* @param dataSchema the encoded data schema
* @return the string
*/
public static String toJson(byte[] rawData, String dataSchema) {
Schema schema = new Schema.Parser().parse(dataSchema);
GenericAvroConverter<GenericContainer> converter = new GenericAvroConverter<GenericContainer>(schema);
GenericAvroConverter<GenericContainer> converter = new
GenericAvroConverter<GenericContainer>(schema);

String json;

try {
GenericContainer record = converter.decodeBinary(rawData);
json = converter.encodeToJson(record);
} catch (IOException e) {
LOG.warn("Can't parse json data", e);
throw new RuntimeException(e); //NOSONAR
} catch (IOException ex) {
LOG.warn("Can't parse json data", ex);
throw new RuntimeException(ex); //NOSONAR
}
return json;
}

/**
* Convert json string using schema to binary data
* Convert json string using schema to binary data.
*
* @param json the json string
* @param dataSchema the encoded data schema
* @return the byte[]
*/
public static byte[] toRawData(String json, String dataSchema) {
Schema schema = new Schema.Parser().parse(dataSchema);
GenericAvroConverter<GenericContainer> converter = new GenericAvroConverter<GenericContainer>(schema);
GenericAvroConverter<GenericContainer> converter = new
GenericAvroConverter<GenericContainer>(schema);

byte[] rawData;

try {
GenericContainer record = converter.decodeJson(json);
rawData = converter.encode(record);
} catch (IOException e) {
LOG.warn("Can't parse json data", e);
throw new RuntimeException(e); //NOSONAR
} catch (IOException ex) {
LOG.warn("Can't parse json data", ex);
throw new RuntimeException(ex); //NOSONAR
}
return rawData;
}
Expand Down

0 comments on commit fc69589

Please sign in to comment.