Skip to content

Commit 0a4e975

Browse files
committed
Minor Java language feature tidying up
1 parent b53d9e3 commit 0a4e975

16 files changed

+80
-131
lines changed

core/src/main/java/software/coley/cafedude/classfile/Descriptor.java

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -356,28 +356,18 @@ public static Descriptor from(@Nullable String desc) {
356356
*/
357357
@Nonnull
358358
public static Descriptor from(char desc) {
359-
switch (desc) {
360-
case 'V':
361-
return VOID;
362-
case 'Z':
363-
return BOOLEAN;
364-
case 'B':
365-
return BYTE;
366-
case 'C':
367-
return CHAR;
368-
case 'S':
369-
return SHORT;
370-
case 'I':
371-
return INT;
372-
case 'F':
373-
return FLOAT;
374-
case 'D':
375-
return DOUBLE;
376-
case 'J':
377-
return LONG;
378-
default:
379-
return new Descriptor(Kind.ILLEGAL, String.valueOf(desc));
380-
}
359+
return switch (desc) {
360+
case 'V' -> VOID;
361+
case 'Z' -> BOOLEAN;
362+
case 'B' -> BYTE;
363+
case 'C' -> CHAR;
364+
case 'S' -> SHORT;
365+
case 'I' -> INT;
366+
case 'F' -> FLOAT;
367+
case 'D' -> DOUBLE;
368+
case 'J' -> LONG;
369+
default -> new Descriptor(Kind.ILLEGAL, String.valueOf(desc));
370+
};
381371
}
382372

383373
/**
@@ -394,28 +384,18 @@ public static Descriptor from(@Nonnull Class<?> clazz) {
394384
if (clazz.isArray()) {
395385
return Objects.requireNonNull(from(descriptor), "Failed to parse array descriptor from class reference");
396386
} else if (clazz.isPrimitive()) {
397-
switch (descriptor) {
398-
case "void":
399-
return VOID;
400-
case "boolean":
401-
return BOOLEAN;
402-
case "byte":
403-
return BYTE;
404-
case "char":
405-
return CHAR;
406-
case "short":
407-
return SHORT;
408-
case "int":
409-
return INT;
410-
case "float":
411-
return FLOAT;
412-
case "double":
413-
return DOUBLE;
414-
case "long":
415-
return LONG;
416-
default:
417-
throw new IllegalArgumentException("Unknown primitive type: " + descriptor);
418-
}
387+
return switch (descriptor) {
388+
case "void" -> VOID;
389+
case "boolean" -> BOOLEAN;
390+
case "byte" -> BYTE;
391+
case "char" -> CHAR;
392+
case "short" -> SHORT;
393+
case "int" -> INT;
394+
case "float" -> FLOAT;
395+
case "double" -> DOUBLE;
396+
case "long" -> LONG;
397+
default -> throw new IllegalArgumentException("Unknown primitive type: " + descriptor);
398+
};
419399
} else {
420400
return Objects.requireNonNull(from("L" + descriptor + ";"), "Failed to parse object descriptor from class reference");
421401
}
@@ -440,20 +420,10 @@ public static boolean isPrimitive(@Nullable String desc) {
440420
* @return {@code true} if it denotes a primitive.
441421
*/
442422
public static boolean isPrimitive(char desc) {
443-
switch (desc) {
444-
case 'V':
445-
case 'Z':
446-
case 'B':
447-
case 'C':
448-
case 'S':
449-
case 'I':
450-
case 'F':
451-
case 'D':
452-
case 'J':
453-
return true;
454-
default:
455-
return false;
456-
}
423+
return switch (desc) {
424+
case 'V', 'Z', 'B', 'C', 'S', 'I', 'F', 'D', 'J' -> true;
425+
default -> false;
426+
};
457427
}
458428

459429
/**

core/src/main/java/software/coley/cafedude/classfile/annotation/TypePathKind.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,12 @@ public int getValue() {
3838
*/
3939
@Nonnull
4040
public static TypePathKind fromValue(int value) {
41-
switch (value) {
42-
case 0:
43-
return ARRAY_DEEPER;
44-
case 1:
45-
return NESTED_DEEPER;
46-
case 2:
47-
return WILDCARD_BOUND;
48-
case 3:
49-
return TYPE_ARGUMENT;
50-
default:
51-
throw new IllegalArgumentException("Invalid type path kind: " + value);
52-
}
41+
return switch (value) {
42+
case 0 -> ARRAY_DEEPER;
43+
case 1 -> NESTED_DEEPER;
44+
case 2 -> WILDCARD_BOUND;
45+
case 3 -> TYPE_ARGUMENT;
46+
default -> throw new IllegalArgumentException("Invalid type path kind: " + value);
47+
};
5348
}
5449
}

core/src/main/java/software/coley/cafedude/classfile/attribute/CharacterRangeTableAttribute.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ public void setFlags(int flags) {
201201
@Override
202202
public boolean equals(Object o) {
203203
if (this == o) return true;
204-
if (!(o instanceof CharacterRangeInfo)) return false;
205-
206-
CharacterRangeInfo that = (CharacterRangeInfo) o;
204+
if (!(o instanceof CharacterRangeInfo that)) return false;
207205

208206
if (startPc != that.startPc) return false;
209207
if (endPc != that.endPc) return false;

core/src/main/java/software/coley/cafedude/classfile/constant/ConstRef.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.Collection;
77
import java.util.List;
8+
import java.util.Objects;
89

910
/**
1011
* Base reference pool entry. Points to a reference's {@link CpClass defining class}
@@ -73,12 +74,10 @@ public Collection<CpEntry> getReferences() {
7374
@Override
7475
public boolean equals(Object o) {
7576
if (this == o) return true;
76-
if (!(o instanceof ConstRef)) return false;
77+
if (!(o instanceof ConstRef constRef)) return false;
7778

78-
ConstRef constRef = (ConstRef) o;
79-
80-
if (!classRef.equals(constRef.classRef)) return false;
81-
return nameType.equals(constRef.nameType);
79+
if (!Objects.equals(classRef, constRef.classRef)) return false;
80+
return Objects.equals(nameType, constRef.nameType);
8281
}
8382

8483
@Override

core/src/main/java/software/coley/cafedude/classfile/constant/CpMethodHandle.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.Collection;
77
import java.util.Collections;
8+
import java.util.Objects;
89

910
/**
1011
* MethodHandle pool entry. Holds a byte to indicate behavior and points to a relevant reference constant
@@ -92,12 +93,10 @@ public Collection<CpEntry> getReferences() {
9293
@Override
9394
public boolean equals(Object o) {
9495
if (this == o) return true;
95-
if (!(o instanceof CpMethodHandle)) return false;
96-
97-
CpMethodHandle that = (CpMethodHandle) o;
96+
if (!(o instanceof CpMethodHandle that)) return false;
9897

9998
if (kind != that.kind) return false;
100-
return reference.equals(that.reference);
99+
return Objects.equals(reference, that.reference);
101100
}
102101

103102
@Override

core/src/main/java/software/coley/cafedude/classfile/constant/CpMethodType.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Collection;
66
import java.util.Collections;
7+
import java.util.Objects;
78

89
/**
910
* Method type pool entry. Points to an UTF constant.
@@ -47,11 +48,9 @@ public Collection<CpEntry> getReferences() {
4748
@Override
4849
public boolean equals(Object o) {
4950
if (this == o) return true;
50-
if (!(o instanceof CpMethodType)) return false;
51+
if (!(o instanceof CpMethodType that)) return false;
5152

52-
CpMethodType that = (CpMethodType) o;
53-
54-
return descriptor.equals(that.descriptor);
53+
return Objects.equals(descriptor, that.descriptor);
5554
}
5655

5756
@Override

core/src/main/java/software/coley/cafedude/classfile/constant/CpModule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Collection;
66
import java.util.Collections;
7+
import java.util.Objects;
78

89
/**
910
* Module pool entry. Points to an UTF constant.
@@ -47,11 +48,9 @@ public Collection<CpEntry> getReferences() {
4748
@Override
4849
public boolean equals(Object o) {
4950
if (this == o) return true;
50-
if (!(o instanceof CpModule)) return false;
51+
if (!(o instanceof CpModule cpModule)) return false;
5152

52-
CpModule cpModule = (CpModule) o;
53-
54-
return name.equals(cpModule.name);
53+
return Objects.equals(name, cpModule.name);
5554
}
5655

5756
@Override

core/src/main/java/software/coley/cafedude/classfile/constant/CpPackage.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Collection;
66
import java.util.Collections;
7+
import java.util.Objects;
78

89
/**
910
* Package pool entry. Points to an UTF constant.
@@ -47,11 +48,9 @@ public Collection<CpEntry> getReferences() {
4748
@Override
4849
public boolean equals(Object o) {
4950
if (this == o) return true;
50-
if (!(o instanceof CpPackage)) return false;
51+
if (!(o instanceof CpPackage cpPackage)) return false;
5152

52-
CpPackage cpPackage = (CpPackage) o;
53-
54-
return packageName.equals(cpPackage.packageName);
53+
return Objects.equals(packageName, cpPackage.packageName);
5554
}
5655

5756
@Override

core/src/main/java/software/coley/cafedude/classfile/instruction/CpRefInstruction.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import software.coley.cafedude.classfile.constant.CpEntry;
66

77
import java.util.Collections;
8+
import java.util.Objects;
89
import java.util.Set;
910

1011
/**
@@ -62,12 +63,10 @@ public int computeSize() {
6263
@Override
6364
public boolean equals(Object o) {
6465
if (this == o) return true;
65-
if (!(o instanceof CpRefInstruction)) return false;
66+
if (!(o instanceof CpRefInstruction that)) return false;
6667
if (!super.equals(o)) return false;
6768

68-
CpRefInstruction that = (CpRefInstruction) o;
69-
70-
return entry.equals(that.entry);
69+
return Objects.equals(entry, that.entry);
7170
}
7271

7372
@Override

core/src/main/java/software/coley/cafedude/classfile/instruction/IincInstruction.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ public void setIncrement(int increment) {
5858
@Override
5959
public boolean equals(Object o) {
6060
if (this == o) return true;
61-
if (!(o instanceof IincInstruction)) return false;
61+
if (!(o instanceof IincInstruction that)) return false;
6262
if (!super.equals(o)) return false;
6363

64-
IincInstruction that = (IincInstruction) o;
65-
6664
if (var != that.var) return false;
6765
return increment == that.increment;
6866
}

0 commit comments

Comments
 (0)