Skip to content

Commit 9b78feb

Browse files
committed
[Java]: Fix warnings and style.
1 parent 47e7fc3 commit 9b78feb

File tree

5 files changed

+127
-62
lines changed

5 files changed

+127
-62
lines changed

main/java/uk/co/real_logic/sbe/codec/java/GroupOrder.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import java.lang.annotation.ElementType;
@@ -6,7 +21,7 @@
621
import java.lang.annotation.Target;
722

823
/**
9-
* Daneel Yaitskov
24+
* Group order for repeating groups in encoded messages.
1025
*/
1126
@Retention(RetentionPolicy.RUNTIME)
1227
@Target({ElementType.TYPE})

main/java/uk/co/real_logic/sbe/codec/java/MethodSelector.java

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import java.lang.reflect.Method;
@@ -12,78 +27,81 @@
1227
import java.util.Map;
1328
import java.util.Set;
1429

15-
/**
16-
* Daneel Yaitskov
17-
*/
1830
public class MethodSelector
1931
{
2032
private final Set<String> ignoredMethods;
2133
private final Map<Class, Set<String>> sortedMethods = new HashMap<>();
2234

2335
public static Set<String> objectAndIteratorMethods()
2436
{
25-
return new HashSet<>(Arrays.asList("hashCode", "clone", "toString", "getClass",
26-
"next", "hasNext", "remove", "iterator"));
37+
return new HashSet<>(
38+
Arrays.asList("hashCode", "clone", "toString", "getClass", "next", "hasNext", "remove", "iterator"));
2739
}
2840

29-
public MethodSelector(Set<String> ignoredMethods)
41+
public MethodSelector(final Set<String> ignoredMethods)
3042
{
3143
this.ignoredMethods = ignoredMethods;
3244
}
3345

34-
public List<Method> select(Class clazz)
46+
public List<Method> select(final Class clazz)
3547
{
3648
final Method[] methods = clazz.getMethods();
37-
final Set<String> sortedMethNames = getSortedMethods(clazz, methods);
38-
final Map<String, Method> sortedMethods = new HashMap<String, Method>();
39-
final List<Method> unsortedMethods = new ArrayList<Method>();
40-
for (Method method : methods)
49+
final Set<String> sortedMethodNames = getSortedMethods(clazz, methods);
50+
final Map<String, Method> sortedMethods = new HashMap<>();
51+
final List<Method> unsortedMethods = new ArrayList<>();
52+
53+
for (final Method method : methods)
4154
{
42-
selectMethod(sortedMethNames, sortedMethods, unsortedMethods, method);
55+
selectMethod(sortedMethodNames, sortedMethods, unsortedMethods, method);
4356
}
44-
for (String name : sortedMethNames)
57+
58+
for (final String name : sortedMethodNames)
4559
{
4660
unsortedMethods.add(sortedMethods.get(name));
4761
}
62+
4863
return unsortedMethods;
4964
}
5065

51-
private Set<String> getSortedMethods(Class clazz, Method[] methods)
66+
private Set<String> getSortedMethods(final Class clazz, final Method[] methods)
5267
{
53-
final Set<String> sortedMethNames = sortedMethods.get(clazz);
54-
if (sortedMethNames == null)
68+
final Set<String> sortedMethodNames = sortedMethods.get(clazz);
69+
if (sortedMethodNames == null)
5570
{
56-
GroupOrder order = (GroupOrder) clazz.getAnnotation(GroupOrder.class);
71+
final GroupOrder order = (GroupOrder)clazz.getAnnotation(GroupOrder.class);
5772
if (order == null)
5873
{
5974
sortedMethods.put(clazz, Collections.<String>emptySet());
75+
6076
return Collections.emptySet();
6177
}
6278
else
6379
{
64-
Set<String> result = new LinkedHashSet<>();
65-
for (Class groupClazz : order.value())
80+
final Set<String> result = new LinkedHashSet<>();
81+
for (final Class groupClazz : order.value())
6682
{
67-
for (Method method : methods)
83+
for (final Method method : methods)
6884
{
69-
if (method.getReturnType() == groupClazz
70-
&& method.getParameterTypes().length == 0)
85+
if (method.getReturnType() == groupClazz && method.getParameterTypes().length == 0)
7186
{
7287
result.add(method.getName());
7388
}
7489
}
7590
}
7691
sortedMethods.put(clazz, result);
92+
7793
return result;
7894
}
7995
}
80-
return sortedMethNames;
96+
97+
return sortedMethodNames;
8198
}
8299

83-
private void selectMethod(Set<String> sortedMethNames,
84-
Map<String, Method> sortedMethods,
85-
List<Method> unsortedMethods,
86-
Method method)
100+
private void selectMethod(
101+
final Set<String> sortedMethodNames,
102+
final Map<String, Method> sortedMethods,
103+
final List<Method> unsortedMethods,
104+
final Method method)
87105
{
88106
final int mods = method.getModifiers();
89107
if (!Modifier.isPublic(mods))
@@ -102,18 +120,20 @@ private void selectMethod(Set<String> sortedMethNames,
102120
{
103121
return;
104122
}
123+
105124
final String name = method.getName();
106125
if (ignoredMethods.contains(name))
107126
{
108127
return;
109128
}
110-
if (sortedMethNames == null)
129+
130+
if (sortedMethodNames == null)
111131
{
112132
unsortedMethods.add(method);
113133
}
114134
else
115135
{
116-
if (sortedMethNames.contains(name))
136+
if (sortedMethodNames.contains(name))
117137
{
118138
sortedMethods.put(name, method);
119139
}
Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import com.google.gson.JsonArray;
@@ -12,75 +27,76 @@
1227
import java.util.Iterator;
1328
import java.util.Map;
1429

15-
/**
16-
* Daneel Yaitskov
17-
*/
1830
public class MethodValuesSerializer
1931
{
2032
private final MethodSelector methodSelector;
2133

22-
public MethodValuesSerializer(MethodSelector methodSelector)
34+
public MethodValuesSerializer(final MethodSelector methodSelector)
2335
{
2436
this.methodSelector = methodSelector;
2537
}
2638

27-
public JsonElement serialize(Object object)
28-
throws InvocationTargetException, IllegalAccessException
39+
public JsonElement serialize(final Object object)
40+
throws InvocationTargetException, IllegalAccessException
2941
{
3042
return serialize(object, true);
3143
}
3244

33-
JsonElement serialize(Object object, boolean visitIterable)
34-
throws InvocationTargetException, IllegalAccessException
45+
JsonElement serialize(final Object object, final boolean visitIterable)
46+
throws InvocationTargetException, IllegalAccessException
3547
{
3648
if (object == null)
3749
{
3850
return JsonNull.INSTANCE;
3951
}
40-
Class clazz = object.getClass();
52+
53+
final Class clazz = object.getClass();
4154
if (Number.class.isAssignableFrom(clazz))
4255
{
43-
return new JsonPrimitive((Number) object);
56+
return new JsonPrimitive((Number)object);
4457
}
4558
else if (clazz == String.class)
4659
{
47-
return new JsonPrimitive((String) object);
60+
return new JsonPrimitive((String)object);
4861
}
4962
else if (clazz == Boolean.class)
5063
{
51-
return new JsonPrimitive((Boolean) object);
64+
return new JsonPrimitive((Boolean)object);
5265
}
5366
else if (object instanceof Enum)
5467
{
55-
return new JsonPrimitive(((Enum) object).name());
68+
return new JsonPrimitive(((Enum)object).name());
5669
}
5770
else if (clazz.isArray())
5871
{
59-
JsonArray result = new JsonArray();
60-
int len = Array.getLength(object);
72+
final JsonArray result = new JsonArray();
73+
final int len = Array.getLength(object);
6174
for (int i = 0; i < len; ++i)
6275
{
6376
result.add(serialize(Array.get(object, i)));
6477
}
78+
6579
return result;
6680
}
6781
else if (visitIterable && Iterable.class.isAssignableFrom(clazz))
6882
{
69-
Iterator iter = ((Iterable) object).iterator();
70-
JsonArray result = new JsonArray();
83+
final Iterator iter = ((Iterable)object).iterator();
84+
final JsonArray result = new JsonArray();
7185
while (iter.hasNext())
7286
{
7387
result.add(serialize(iter.next(), false));
7488
}
89+
7590
return result;
7691
}
7792
else if (Map.class.isAssignableFrom(clazz))
7893
{
79-
JsonObject result = new JsonObject();
80-
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) object).entrySet())
94+
final JsonObject result = new JsonObject();
95+
for (final Map.Entry<?, ?> entry : ((Map<?, ?>)object).entrySet())
8196
{
8297
result.add(entry.getKey().toString(), serialize(entry.getValue()));
8398
}
99+
84100
return result;
85101
}
86102
else
@@ -89,19 +105,21 @@ else if (Map.class.isAssignableFrom(clazz))
89105
}
90106
}
91107

92-
private JsonElement serializeObject(Object object, Class clazz)
93-
throws InvocationTargetException, IllegalAccessException
108+
private JsonElement serializeObject(final Object object, final Class clazz)
109+
throws InvocationTargetException, IllegalAccessException
94110
{
95-
JsonObject result = new JsonObject();
111+
final JsonObject jsonObject = new JsonObject();
96112
for (Method method : methodSelector.select(clazz))
97113
{
98-
Object re = method.invoke(object);
99-
if (re == object)
114+
final Object result = method.invoke(object);
115+
if (result == object)
100116
{
101117
continue;
102118
}
103-
result.add(method.getName(), serialize(re));
119+
120+
jsonObject.add(method.getName(), serialize(result));
104121
}
105-
return result;
122+
123+
return jsonObject;
106124
}
107125
}

main/java/uk/co/real_logic/sbe/generation/cpp98/Cpp98Generator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ private CharSequence generatePrimitiveFieldMetaData(final String propertyName, f
845845
final PrimitiveType primitiveType = encoding.primitiveType();
846846
final String cpp98TypeName = cpp98TypeName(primitiveType);
847847
final CharSequence nullValueString = generateNullValueLiteral(primitiveType, encoding);
848-
848+
849849
sb.append(String.format(
850850
"\n" +
851851
indent + " static const %1$s %2$sNullValue()\n" +

test/java/uk/co/real_logic/sbe/codec/java/MethodValuesSerializerTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import com.google.gson.JsonArray;
@@ -12,9 +27,6 @@
1227
import java.util.Arrays;
1328
import java.util.List;
1429

15-
/**
16-
* Daneel Yaitskov
17-
*/
1830
public class MethodValuesSerializerTest
1931
{
2032
enum E1
@@ -75,7 +87,7 @@ public E1 publicEnum()
7587

7688
public int[] publicIntArr()
7789
{
78-
return new int[] { 1 };
90+
return new int[]{1};
7991
}
8092

8193
public String publicString()
@@ -103,7 +115,7 @@ public static int staticPublicIntIgnored()
103115
public void test() throws InvocationTargetException, IllegalAccessException
104116
{
105117
MethodValuesSerializer serializer = new MethodValuesSerializer(
106-
new MethodSelector(MethodSelector.objectAndIteratorMethods()));
118+
new MethodSelector(MethodSelector.objectAndIteratorMethods()));
107119
JsonObject expected = new JsonObject();
108120
expected.add("publicInt", new JsonPrimitive(1));
109121
expected.add("publicString", new JsonPrimitive("hello"));

0 commit comments

Comments
 (0)