Created
March 29, 2024 23:17
-
-
Save asdf913/95a7a9717c799c6a2cd2238ea30730b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.time.LocalDate; | |
import java.time.chrono.JapaneseEra; | |
import java.util.Arrays; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Objects; | |
import org.apache.bcel.classfile.ClassParser; | |
import org.apache.bcel.classfile.JavaClass; | |
import org.apache.bcel.classfile.Method; | |
import org.apache.bcel.generic.BIPUSH; | |
import org.apache.bcel.generic.ConstantPoolGen; | |
import org.apache.bcel.generic.ICONST; | |
import org.apache.bcel.generic.Instruction; | |
import org.apache.bcel.generic.MethodGen; | |
import org.apache.bcel.generic.PUTSTATIC; | |
import org.apache.bcel.generic.SIPUSH; | |
import org.apache.commons.lang3.ObjectUtils; | |
import org.apache.commons.lang3.StringUtils; | |
public class JapaneseEraSinceMap { | |
public static void main(final String[] args) { | |
// | |
// https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/time/chrono/JapaneseEra.java | |
// | |
// {Meiji=1868-01-01, Taisho=1912-07-30, Showa=1926-12-25, Heisei=1989-01-08, | |
// Reiwa=2019-05-01} | |
// | |
System.out.println(getJapaneseEraSinceMap()); | |
// | |
} | |
private static Map<JapaneseEra, LocalDate> getJapaneseEraSinceMap() { | |
// | |
Map<JapaneseEra, LocalDate> map = null; | |
// | |
final Class<?> clz = JapaneseEra.class; | |
// | |
try (final InputStream is = clz != null | |
? clz.getResourceAsStream(String.format("/%1$s.class", StringUtils.replace(clz.getName(), ".", "/"))) | |
: null) { | |
// | |
final JavaClass jc = new ClassParser(is, null).parse(); | |
// | |
final List<Method> ms = Arrays.stream(jc.getMethods()) | |
.filter(m -> m != null ? Objects.equals(m.getName(), "<clinit>") : null).toList(); | |
// | |
if (ms != null && ms.size() > 1) { | |
// | |
throw new IllegalStateException(); | |
// | |
} // if | |
// | |
final Method m = ms != null && ms.size() == 1 ? ms.get(0) : null; | |
// | |
final Instruction[] ins = m != null ? new MethodGen(m, null, null).getInstructionList().getInstructions() | |
: null; | |
// | |
Instruction in = null; | |
// | |
SIPUSH sipush = null; | |
// | |
PUTSTATIC putstatic = null; | |
// s | |
ConstantPoolGen cpg = null; | |
// | |
final int length = ins != null ? ins.length : 0; | |
// | |
Number year, month, day = null; | |
// | |
List<JapaneseEra> jes = null; | |
// | |
for (int i = 0; ins != null && i < length; i++) { | |
// | |
if ((in = ins[i]) == null) { | |
// | |
continue; | |
// | |
} // if | |
// | |
if ((sipush = cast(SIPUSH.class, in)) != null && length > i + 5) { | |
// | |
year = sipush.getValue(); | |
// | |
month = getNumberValue(ins[i + 1]); | |
// | |
day = getNumberValue(ins[i + 2]); | |
// | |
if ((putstatic = cast(PUTSTATIC.class, ins[i + 5])) != null) { | |
// | |
if (map == null) { | |
// | |
map = new LinkedHashMap<>(); | |
// | |
} // if | |
// | |
final String name = putstatic.getFieldName( | |
cpg = ObjectUtils.getIfNull(cpg, () -> new ConstantPoolGen(jc.getConstantPool()))); | |
// | |
if ((jes = Arrays.stream(JapaneseEra.values()) | |
.filter(x -> x != null && StringUtils.equalsIgnoreCase(x.toString(), name)) | |
.toList()) != null && jes.size() > 1) { | |
// | |
throw new IllegalStateException(); | |
// | |
} // if | |
// | |
if (jes == null || jes.isEmpty()) { | |
// | |
continue; | |
// | |
} // if | |
// | |
map.put(jes.get(0), | |
year != null && month != null && day != null | |
? LocalDate.of(year.intValue(), month.intValue(), day.intValue()) | |
: null); | |
// | |
} // if | |
// | |
} // if | |
// | |
} // for | |
// | |
} catch (final IOException e) { | |
// | |
throw new RuntimeException(e); | |
// | |
} // try | |
// | |
return map; | |
// | |
} | |
private static Number getNumberValue(final Instruction in) { | |
// | |
final ICONST iconst = cast(ICONST.class, in); | |
// | |
if (iconst != null) { | |
// | |
return iconst.getValue(); | |
// | |
} // if | |
// | |
final BIPUSH bipush = cast(BIPUSH.class, in); | |
// s | |
if (bipush != null) { | |
// | |
return bipush.getValue(); | |
// | |
} // if | |
// | |
throw new IllegalStateException(); | |
// | |
} | |
private static <T> T cast(final Class<T> clz, final Object instance) { | |
return clz != null && clz.isInstance(instance) ? clz.cast(instance) : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required dependency
https://mvnrepository.com/artifact/org.apache.bcel/bcel