Skip to content

Cleanup of warnings / possible not checked nullpointers #1610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ private static TreeNode getOrCreateNode(TreeNode root, NativeClassDescriptor cla

if (ClassUtil.isPrimitive(clazz)) {
TreeNode tmp = TreeNode.getPrimitive(clazz);
if(tmp == null)
throw new UnsupportedOperationException("tmp == null" + clazz.getClassName());
child.nodeType = tmp.nodeType;
} else {
child.nodeType = clazz.isInterface() ? TreeNode.Interface
Expand All @@ -449,7 +451,7 @@ private static TreeNode getOrCreateNode(TreeNode root, NativeClassDescriptor cla
}
node = child;
if (node.baseClassNode == null) {
NativeClassDescriptor baseClass = null;
NativeClassDescriptor baseClass;
if (predefinedSuperClassname != null) {
SecuredNativeClassDescriptor securedNativeClassDescriptor = SecuredClassRepository.INSTANCE.findNearestAllowedClass(predefinedSuperClassname);
baseClass = securedNativeClassDescriptor.isUsageAllowed() ? securedNativeClassDescriptor.getNativeDescriptor() : null;
Expand Down Expand Up @@ -503,6 +505,8 @@ private static TreeNode createArrayNode(TreeNode root, String className)
child = currentNode.createChild(name);
if (ClassUtil.isPrimitive(name)) {
TreeNode node = TreeNode.getPrimitive(name);
if(node == null)
throw new UnsupportedOperationException("node == null: " + name);
child.nodeType = node.nodeType;
child.arrayElement = node;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class ClassDirectory implements ClassMapProvider {
private final String path;
Expand All @@ -31,7 +28,7 @@ public class ClassDirectory implements ClassMapProvider {

private ClassDirectory(String path) {
this.path = path;
this.classMap = new HashMap<String, NativeClassDescriptor>();
this.classMap = new HashMap<>();
}

public Map<String, NativeClassDescriptor> getClassMap() {
Expand All @@ -50,9 +47,9 @@ public static ClassDirectory readDirectory(String path) throws IOException {

private static void readDirectory(ClassDirectory dir, String path)
throws IOException {
List<File> subDirs = new ArrayList<File>();
List<File> subDirs = new ArrayList<>();
File currentDir = new File(path);
for (File file : currentDir.listFiles()) {
for (File file : Objects.requireNonNull(currentDir.listFiles())) {
if (file.isFile()) {
String name = file.getName();
if (name.endsWith(CLASS_EXT)) {
Expand All @@ -73,7 +70,6 @@ private static NativeClassDescriptor getClassDescriptor(String name, File file)
if (name.endsWith(CLASS_EXT)) {
ClassParser cp = new ClassParser(file.getAbsolutePath());
JavaClass javaClass = cp.parse();
boolean isKotlinClass = false;

AnnotationEntry[] annotationEntries = javaClass.getAnnotationEntries();
if (annotationEntries != null) {
Expand All @@ -82,16 +78,13 @@ private static NativeClassDescriptor getClassDescriptor(String name, File file)
if ("Lkotlin/Metadata;".equals(annotationType)) {
MetadataAnnotation kotlinClassMetadataAnnotation = new BytecodeMetadataAnnotation(annotationEntry);
NativeClassDescriptor kotlinClassDescriptor = new KotlinClassDescriptor(javaClass, kotlinClassMetadataAnnotation);
isKotlinClass = true;
analyticsCollector.markHasKotlinRuntimeClassesIfNotMarkedAlready();
return kotlinClassDescriptor;
}
}
}

if (!isKotlinClass) {
return new JavaClassDescriptor(javaClass);
}
return new JavaClassDescriptor(javaClass);

This comment was marked as abuse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statement seems unnecessary as we return some lines above in the case of Kotlin originating bytecode and the only case we will reach this point is when we're dealing with Java originating bytecode

Copy link
Contributor Author

@saschaarthur saschaarthur May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IDE complained that the if statement is always false.
Which is correct, its never set to true and then reached.

This comment was marked as abuse.

}

return clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ public static String getSimpleName(NativeClassDescriptor clazz) {
if (idx < 0) {
idx = className.lastIndexOf(".");
}
String simpleName = className.substring(idx + 1);
return simpleName;
return className.substring(idx + 1);
}

static NativeMethodDescriptor[] getAllMethods(NativeClassDescriptor clazz) {
ArrayList<NativeMethodDescriptor> methods = new ArrayList<NativeMethodDescriptor>();
ArrayList<NativeMethodDescriptor> methods = new ArrayList<>();
NativeClassDescriptor currentClass = clazz;
while (currentClass != null) {
NativeMethodDescriptor[] currentClassMethods = currentClass.getMethods();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Generator {
private static final String MDG_BLACKLIST = "blacklist.mdg";

/**
* @param args
* @param args arguments
*/
public static void main(String[] args) {
enableFlaggedFeatures(args);
Expand All @@ -38,12 +38,12 @@ public static void main(String[] args) {
try {
metadataOutputDir = getFileRows(MDG_OUTPUT_DIR).get(0);
} catch (Exception e) {
throw new InvalidParameterException(String.format("You need to pass a file containing a single line: the output dir for the metadata generator1\n", e.getMessage()));
throw new InvalidParameterException(String.format("You need to pass a file containing a single line: the output dir for the metadata generator %s\n", e.getMessage()));
}
try {
params = getFileRows(MDG_JAVA_DEPENDENCIES);
} catch (Exception e) {
throw new InvalidParameterException(String.format("You need to pass a file containing a list of jar/class paths, so metadata can be generated for them!\n", e.getMessage()));
throw new InvalidParameterException(String.format("You need to pass a file containing a list of jar/class paths, so metadata can be generated for them! %s\n", e.getMessage()));
}

TreeNode root = Builder.build(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public String getPropertyName() {
}

public Optional<MethodInfo> getGetterMethod() {
return getterMethod == null ? Optional.<MethodInfo>empty() : Optional.of(getterMethod);
return getterMethod == null ? Optional.empty() : Optional.of(getterMethod);
}

public Optional<MethodInfo> getSetterMethod() {
return setterMethod == null ? Optional.<MethodInfo>empty() : Optional.of(setterMethod);
return setterMethod == null ? Optional.empty() : Optional.of(setterMethod);
}
}

Expand Down Expand Up @@ -144,51 +144,53 @@ public static TreeNode getPrimitive(NativeClassDescriptor clazz) throws Exceptio

String name = clazz.getClassName();

if (name.equals("byte")) {
return TreeNode.BYTE;
} else if (name.equals("short")) {
return TreeNode.SHORT;
} else if (name.equals("int")) {
return TreeNode.INTEGER;
} else if (name.equals("long")) {
return TreeNode.LONG;
} else if (name.equals("float")) {
return TreeNode.FLOAT;
} else if (name.equals("double")) {
return TreeNode.DOUBLE;
} else if (name.equals("boolean")) {
return TreeNode.BOOLEAN;
} else if (name.equals("char")) {
return TreeNode.CHAR;
} else if (name.equals("void")) {
return null;
} else {
throw new Exception("unknown type=" + name);
switch (name) {
case "byte":
return TreeNode.BYTE;
case "short":
return TreeNode.SHORT;
case "int":
return TreeNode.INTEGER;
case "long":
return TreeNode.LONG;
case "float":
return TreeNode.FLOAT;
case "double":
return TreeNode.DOUBLE;
case "boolean":
return TreeNode.BOOLEAN;
case "char":
return TreeNode.CHAR;
case "void":
return null;
default:
throw new Exception("unknown type=" + name);
}
}

public static TreeNode getPrimitive(String name)
throws IllegalArgumentException {
if (name.equals("B")) {
return TreeNode.BYTE;
} else if (name.equals("S")) {
return TreeNode.SHORT;
} else if (name.equals("I")) {
return TreeNode.INTEGER;
} else if (name.equals("J")) {
return TreeNode.LONG;
} else if (name.equals("F")) {
return TreeNode.FLOAT;
} else if (name.equals("D")) {
return TreeNode.DOUBLE;
} else if (name.equals("Z")) {
return TreeNode.BOOLEAN;
} else if (name.equals("C")) {
return TreeNode.CHAR;
} else if (name.equals("V")) {
return null;
} else {
throw new IllegalArgumentException("unknown type=" + name);
switch (name) {
case "B":
return TreeNode.BYTE;
case "S":
return TreeNode.SHORT;
case "I":
return TreeNode.INTEGER;
case "J":
return TreeNode.LONG;
case "F":
return TreeNode.FLOAT;
case "D":
return TreeNode.DOUBLE;
case "Z":
return TreeNode.BOOLEAN;
case "C":
return TreeNode.CHAR;
case "V":
return null;
default:
throw new IllegalArgumentException("unknown type=" + name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -42,7 +43,7 @@ private static int writeUniqueName(String name,
writeUniqueName_lenBuff[1] = (byte) ((len >> 8) & 0xFF);

outStringsStream.write(writeUniqueName_lenBuff);
outStringsStream.write(name.getBytes("UTF-8"));
outStringsStream.write(name.getBytes(StandardCharsets.UTF_8));

uniqueStrings.put(name, position);

Expand All @@ -61,7 +62,7 @@ private static void writeInt(int value, StreamWriter out) throws Exception {
private static void writeMethodInfo(MethodInfo mi,
HashMap<String, Integer> uniqueStrings, StreamWriter outValueStream)
throws Exception {
int pos = uniqueStrings.get(mi.name).intValue();

This comment was marked as abuse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be autounboxed by the JVM, and this is also a build time step. I believe this change is ok.

int pos = uniqueStrings.get(mi.name);
writeInt(pos, outValueStream);

byte isResolved = (byte) (mi.isResolved ? 1 : 0);
Expand Down Expand Up @@ -190,9 +191,9 @@ public void writeClassValue(StreamWriter writer,
public void writeTree(TreeNode root) throws Exception {
short curId = 0;

ArrayDeque<TreeNode> d = new ArrayDeque<TreeNode>();
ArrayDeque<TreeNode> d = new ArrayDeque<>();

HashMap<String, Integer> uniqueStrings = new HashMap<String, Integer>();
HashMap<String, Integer> uniqueStrings = new HashMap<>();

commonInterfacePrefixPosition = writeUniqueName("com/tns/gen/",
uniqueStrings, outStringsStream);
Expand Down Expand Up @@ -275,9 +276,7 @@ public void writeTree(TreeNode root) throws Exception {
}
}

for (TreeNode child : n.children) {
d.add(child);
}
d.addAll(n.children);
}

outStringsStream.flush();
Expand Down Expand Up @@ -307,9 +306,7 @@ public void writeTree(TreeNode root) throws Exception {
throw new Exception("should not happen");
}

for (TreeNode child : n.children) {
d.add(child);
}
d.addAll(n.children);
}

outValueStream.flush();
Expand Down