Skip to content

Instantly share code, notes, and snippets.

@hishidama
hishidama / jshell.java
Created April 5, 2018 03:02
JShellで以下のコードを打ち込むと固まる(Java10, CentOS7.3)
class Foo {
public String getValue() { return "foo" ; }
}
interface Bar {
Integer getId();
}
class Baz extends Foo implements Bar {
public Integer getId() { return 123; }
@hishidama
hishidama / EnumExample3.java
Created November 30, 2017 04:48
enumの相互参照
import java.util.function.Supplier;
public class EnumExample3 {
enum A {
A1(B.B1, () -> B.B1), A2(B.B2, () -> B.B2);
private B b;
private Supplier<B> s;
@hishidama
hishidama / gist:17b7d6673a7f07ffa3771ba75f937c2f
Created September 26, 2017 05:49
getなのに、なぜエラーメッセージが「set」なのか?
package example;
import java.lang.reflect.Field;
public class FieldExample {
public static void main(String[] args) throws Exception {
Field field = Integer.class.getDeclaredField("value");
field.setAccessible(true);
field.getInt("");
@hishidama
hishidama / MethodReferenceExample.java
Last active July 13, 2017 02:54
メソッド参照できる・できない
package example;
// https://ideone.com/cx8hsF
public class MethodReferenceExample {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
Test test11 = String::trim;
// Test test12 = Hogehoge::trim; // NG
Hogehoge h = new Hogehoge();
2017-06-14 18:40:15.984 +0900: Embulk v0.8.20
2017-06-14 18:40:18.664 +0900 [INFO] (0001:transaction): Loaded plugin embulk-output-postgresql (0.7.5)
2017-06-14 18:40:18.714 +0900 [INFO] (0001:transaction): Listing local files at directory 'S:\data' filtering filename by prefix 'hoge.csv'
2017-06-14 18:40:18.714 +0900 [INFO] (0001:transaction): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-06-14 18:42:16.571 +0900 [INFO] (0001:transaction): Loading files [S:\data\hoge.csv]
2017-06-14 18:42:16.665 +0900 [INFO] (0001:transaction): Using local thread executor with max_threads=16 / output tasks 8 = input tasks 1 * 8
2017-06-14 18:42:16.665 +0900 [INFO] (0001:transaction): Connecting to jdbc:postgresql://db-server:5432/hoge options {user=hoge, tcpKeepAlive=true, loginTimeout=300, socketTimeout=1800}
2017-06-14 18:42:16.805 +0900 [INFO] (0001:transaction): SQL: SET search_path TO "public"
2017-06-14 18:42:16.821 +0900 [INFO] (0001:transaction): > 0.02 seconds
2017-06-14 1
@hishidama
hishidama / gist:3286635f490297cdeb35461dc1d888f5
Created April 11, 2017 07:34
embulk-input-postgresqlでviewを読もうとしたときのエラー
2017-04-11 16:30:30.549 +0900: Embulk v0.7.7
2017-04-11 16:30:32.407 +0900 [INFO] (preview): Loaded plugin embulk-input-postgresql (0.8.2)
2017-04-11 16:30:32.639 +0900 [INFO] (preview): SQL: SET search_path TO "public"
LoadError: load error: embulk/command/embulk_main -- java.lang.NoClassDefFoundError: org/embulk/spi/json/JsonParser
require at org/jruby/RubyKernel.java:940
require at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:54
<top> at file:/home/apis/.embulk/bin/embulk!/embulk/command/embulk_bundle.rb:55
@hishidama
hishidama / gist:b9b82a449a030a7dfd3f428f0fb48bbd
Created February 27, 2017 08:42
embulkをWindowsのJava9-eaで動かしたら
2017-02-27T17:30:12.651+09:00 [main] ERROR JavaProxyClassFactory : could not use ClassLoader.defineClass method
java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @569cfc36
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(Unknown Source)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Unknown Source)
at java.base/java.lang.reflect.Method.setAccessible(Unknown Source)
at org.jruby.javasupport.proxy.JavaProxyClassFactory$1.run(JavaProxyClassFactory.java:219)
at org.jruby.javasupport.proxy.JavaProxyClassFactory$1.run(JavaProxyClassFactory.java:212)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at org.jruby.javasupport.proxy.JavaProxyClassFactory.<clinit>(
@hishidama
hishidama / ToStringFizzBuzz.java
Created October 19, 2016 03:56
toStringにロジックを書いたFizzBuzz
package example;
import java.util.function.Supplier;
public class ToStringFizzBuzz {
private final int value;
public ToStringFizzBuzz(int value) {
assert value >= 1;
@hishidama
hishidama / gist:6a2bf263608d09502d7b
Created June 30, 2015 14:16
うるう秒が表示されるかどうか見てみる
package example.time;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class LeapSecondPrint {
public static void main(String... args) {
ZonedDateTime end = ZonedDateTime.of(2015, 7, 1, 0, 0, 2, 0, ZoneId.systemDefault());
for (ZonedDateTime now = ZonedDateTime.now(), prev = null; now.compareTo(end) <= 0; prev = now, now = ZonedDateTime.now()) {
@hishidama
hishidama / JankenExample3.java
Created December 27, 2014 03:49
Javaじゃんけん3
package example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;