Javaに投稿されたコード一覧

Java Javaにデリゲートがないとか…。仕方ないからAction作ったったし

public interface ActionInterface {
	void invoke();
}

Java 現在時刻を偽装するJava Agent

package org.ocharake.matobaa.utlib;

import java.io.ByteArrayInputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import java.util.Calendar;
import java.util.Date;

import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;

public class NowDateMockAgent {
	/**
	 * An entry point for insert NowDateMockAgent functionality to
	 * java.util.Date and java.util.GregorianCalendar.
	 *
	 * This method will be invoked before enter the main mathod with VM's
	 * runtime argument as follows: -javaagent:nowdateagent.jar
	 *
	 * @param agentArgs
	 *            ignored. don't care.
	 * @param instrumentation
	 *            ignored. don't care.
	 */
	public static void premain(String agentArgs, Instrumentation instrumentation) {
		instrumentation.addTransformer(new DateTransformer());
	}

	private static final class DateTransformer implements ClassFileTransformer {

		static String expected = "Long.parseLong(System.getProperty(\""
				+ NowDateMockAgent.class.getCanonicalName() + "\"));";

		public byte[] transform(ClassLoader loader, String className,
				Class<?> void1, ProtectionDomain void2, byte[] classfileBuffer)
				throws IllegalClassFormatException {
			try {
				// insert "time=0" at end of
				// GregorianCalengar.<init>(TimeZone,Locale)
				if (className.equals("java/util/GregorianCalendar")) {
					ClassPool pool = ClassPool.getDefault();
					ByteArrayInputStream stream = new ByteArrayInputStream(
							classfileBuffer);
					CtClass targetType = pool.makeClass(stream);
					CtClass[] paramTypes = pool.get(new String[] {
							"java/util/TimeZone", "java/util/Locale" });
					CtBehavior constructor = targetType
							.getDeclaredConstructor(paramTypes);
					constructor.insertAfter("time = " + expected);
					return targetType.toBytecode();
				}
				// insert "fastTime=0" at end of java.util.Date.<init>()
				if (className.equals("java/util/Date")) {
					ClassPool pool = ClassPool.getDefault();
					ByteArrayInputStream stream = new ByteArrayInputStream(
							classfileBuffer);
					CtClass targetType = pool.makeClass(stream);
					targetType.getDeclaredConstructor(new CtClass[0])
							.insertAfter("fastTime = " + expected);
					return targetType.toBytecode();
				}
			} catch (Exception initCause) {
				IllegalClassFormatException e = new IllegalClassFormatException();
				e.initCause(initCause);
				throw e;
			}
			return null;
		}
	}

	static {
		System.setProperty(NowDateMockAgent.class.getCanonicalName(), "0");
	}

	/**
	 * Set the date value you expected.
	 *
	 * @param dateExpected
	 *            expected date as long
	 */
	public static void setExpected(long dateExpected) {
		System.setProperty(NowDateMockAgent.class.getCanonicalName(),
				Long.toString(dateExpected));
	}

	/**
	 * Set the date value you expected
	 *
	 * @param dateExpected
	 *            expected date as java.util.Date
	 */
	public static void setExpected(Date dateExpected) {
		setExpected(dateExpected.getTime());
	}

	/**
	 * Set the date value you expected
	 *
	 * @param dateExpected
	 *            expected date as java.util.Calendar
	 */
	public static void setExpected(Calendar dateExpected) {
		setExpected(dateExpected.getTime().getTime());
	}
}

Java 文字列のバイト数を返します

public static int getLength(String str) {
	int ret = 0;
	byte[] bytes;
	try {
		// MS932エンコード
		bytes = str.getBytes("MS932");
		if (bytes != null)
			ret = bytes.length;
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
	return ret;
}

Java テスト

public class Test {
	public static void main(String[] args){
		System.out.println("Hello world!");
	}
}

Java やってはいけないカスタムassert

public static void assertByYourEyes(Object actual) {
    System.out.println(actual);
}

Java Java 7で「確実に閉じる」コード

try (
  FileInputStream in = new FileInputStream("in.txt");
  FileOutputStream out = new FileOutputStream("out.txt")
) {
  // ...ここで処理
}

Java Java 5で「確実に閉じる」コード

FileInputStream in = null;
try {
  in = new FileInputStream("in.txt");
  FileOutputStream out = null;
  try {
    out = new FileOutputStream("out.txt");
    // ...ここで処理
  } finally {
    XxUtil.close(out);
  }
} finally {
  XxUtil.close(in);
}

//----------

// XxUtil

public static void close(java.io.Closable closable) throws IOException {
  if (closable != null) {
    closable.close();
  }
}

Java Java 1.4で「確実に閉じる」コード

FileInputStream in = null;
try {
  in = new FileInputStream("in.txt");
  FileOutputStream out = null;
  try {
    out = new FileOutputStream("out.txt");
    // ...ここで処理
  } finally {
    if (out != null) {
       out.close();
    }
  }
} finally {
  if (in != null) {
    in.close();
  }
}

Java Java 1.4で確実に閉じる]コード

FileInputStream in = null;
try {
  in = new FileInputStream("in.txt");
  FileOutputStream out = null;
  try {
    out = new FileOutputStream("out.txt");
    // ...ここで処理
  } finally {
    if (out != null) {
       out.close();
    }
  }
} finally {
  if (in != null) {
    in.close();
  }
}

Java こーどったー

public static void main(String[] args) {
	system.out.println("test");
}
Total Pages: 10 / 11« 先頭...7891011

よく投稿されているコード

タグ

最近投稿されたコード