Skip to content

Instantly share code, notes, and snippets.

Avatar

Takao Nakaguchi takawitter

View GitHub Profile
@takawitter
takawitter / TestServiceSOAP.groovy
Last active Mar 9, 2017
SOAP Service and Client using language grid libraries and Groovy.
View TestServiceSOAP.groovy
@Grab('org.eclipse.jetty:jetty-webapp:9.4.0.v20161208')
@Grab('org.langrid:jp.go.nict.langrid.service.language_1_2:1.0.10')
@Grab('org.langrid:jp.go.nict.langrid.servicecontainer.handler.axis:1.0.10')
import jp.go.nict.langrid.service_1_2.translation.TranslationService;
import jp.go.nict.langrid.servicecontainer.handler.annotation.Service;
import jp.go.nict.langrid.servicecontainer.handler.axis.SGAxisServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class TestServiceImpl implements TranslationService{
View CollectionFixture.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;
public class CollectionFixture<T> {
public CollectionFixture(Collection<T> collection){
@takawitter
takawitter / Test.java
Created Oct 19, 2016
Using Iterable.forEach by tunneling exception that may occur inside Consumer.
View Test.java
public class Test{
@Test
public void test_tunnelingExecute() throws Throwable{
try{
tunnelingExecute(
Arrays.asList(1, 2, 3)::forEach,
v -> {throw new IOException();}
);
Assert.fail();
} catch(IOException e){
View result
Testing LinkedList
com.dzyoba.alex.TestAlloc: [7, 25, 276]
com.dzyoba.alex.TestInsert: [7069, 19437, 24856]
com.dzyoba.alex.TestInsertHead: [0, 1, 2]
com.dzyoba.alex.TestInsertTail: [0, 1, 1]
com.dzyoba.alex.TestSearch: [17372, 53823, 70404]
com.dzyoba.alex.TestDelete: [5, 9, 11]
com.dzyoba.alex.TestAlloc: [6, 36, 98]
com.dzyoba.alex.TestRemove: [6472, 8434, 9857]
@takawitter
takawitter / invoke.html
Last active Sep 7, 2016
New Langrid service invocation API for JavaScript.
View invoke.html
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="${langrid-address}/js"></script>
<script>
new LangridClientFactory({
apiKey: "${your-api-key}"
})
.create("${service-id}")
.translate("en", "ja", "Hello")
.done(function(res){
console.log(res.result);
@takawitter
takawitter / reflectionInvoker.groovy
Created Apr 21, 2016
sample code to invoke ReflectionMethodInvoker of groovy
View reflectionInvoker.groovy
import org.codehaus.groovy.runtime.ReflectionMethodInvoker
class Base{}
class Derived extends Base{}
class Derived2 extends Derived{}
class Hello{
public void hello(Base b){
println "base"
}
public void hello(Derived d){
@takawitter
takawitter / LimitClassReferenceWhenDeserializingObject.java
Last active Nov 11, 2015
Limit class reference when deserializing object by using custom class loader.
View LimitClassReferenceWhenDeserializingObject.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.HashMap;
View JSONObjectExample.java
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
public class JSONObjectExample {
@Test
public void java_json_literal() throws Throwable{
@takawitter
takawitter / EventListenerList.java
Created Jul 24, 2015
Typed and easily to fire event, EventListenerList
View EventListenerList.java
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.EventListener;
public class EventListenerList<T extends EventListener> extends javax.swing.event.EventListenerList{
public EventListenerList(Class<T> clazz){
this.clazz = clazz;
this.fireProxy = clazz.cast(Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
@takawitter
takawitter / HtmlEncoderTest.java
Last active Aug 29, 2015
ちゃんと5回呼ばれるやつ(シングルクオートをバッククオートに修正)
View HtmlEncoderTest.java
import java.util.HashMap;
import java.util.Map;
public class HtmlEncoderTest {
static public void main(String[] args) {
System.out.println(new HtmlEncoder().encode("<script>alert('注意!');</script>"));
System.out.println(HtmlEncoder.count);
}
}