Search in sources :

Example 1 with Class

use of java.lang.Class in project ceylon by eclipse.

the class javaSerializationRoundTrip_ method javaSerializationRoundTrip.

public static void javaSerializationRoundTrip() throws Exception {
    Class cls = Class.forName("org.eclipse.ceylon.compiler.java.test.interop.sdk.javaSerialization_");
    Method meth = cls.getMethod("javaSerialization");
    final Object o = meth.invoke(null);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(o);
    oos.close();
    System.err.println("wrote: " + o);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(is);
    final Object read = ois.readObject();
    System.err.println("read: " + read);
    cls = Class.forName("org.eclipse.ceylon.compiler.java.test.interop.sdk.javaSerializationCompare_");
    meth = cls.getMethod("javaSerializationCompare", Object.class, Object.class);
    meth.invoke(null, o, read);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Class(java.lang.Class) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with Class

use of java.lang.Class in project javaee7-samples by javaee-samples.

the class TestClient method processRequest.

/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
        List<Class<? extends Encoder>> encoders = new ArrayList<>();
        encoders.add(MyMessageEncoder.class);
        List<Class<? extends Decoder>> decoders = new ArrayList<>();
        decoders.add(MyMessageDecoder.class);
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:" + request.getLocalPort() + request.getContextPath() + "/websocket";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class, ClientEndpointConfig.Builder.create().encoders(encoders).decoders(decoders).build(), URI.create(uri));
        out.println("<br><br>Look in server.log for message exchange between client/server.");
        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Encoder(javax.websocket.Encoder) ArrayList(java.util.ArrayList) Class(java.lang.Class) DeploymentException(javax.websocket.DeploymentException) Decoder(javax.websocket.Decoder) PrintWriter(java.io.PrintWriter)

Example 3 with Class

use of java.lang.Class in project groovy-core by groovy.

the class GroovyScriptEngineImpl method eval.

public Object eval(String script, ScriptContext ctx) throws ScriptException {
    try {
        String val = (String) ctx.getAttribute("#jsr223.groovy.engine.keep.globals", ScriptContext.ENGINE_SCOPE);
        ReferenceBundle bundle = ReferenceBundle.getHardBundle();
        if (val != null && val.length() > 0) {
            if (val.equalsIgnoreCase("soft")) {
                bundle = ReferenceBundle.getSoftBundle();
            } else if (val.equalsIgnoreCase("weak")) {
                bundle = ReferenceBundle.getWeakBundle();
            } else if (val.equalsIgnoreCase("phantom")) {
                bundle = ReferenceBundle.getPhantomBundle();
            }
        }
        globalClosures.setBundle(bundle);
    } catch (ClassCastException cce) {
    /*ignore.*/
    }
    try {
        Class clazz = getScriptClass(script);
        if (clazz == null)
            throw new ScriptException("Script class is null");
        return eval(clazz, ctx);
    } catch (SyntaxException e) {
        throw new ScriptException(e.getMessage(), e.getSourceLocator(), e.getLine());
    } catch (Exception e) {
        if (debug)
            e.printStackTrace();
        throw new ScriptException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ReferenceBundle(org.codehaus.groovy.util.ReferenceBundle) MetaClass(groovy.lang.MetaClass) Class(java.lang.Class) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) String(java.lang.String) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 4 with Class

use of java.lang.Class in project groovy-core by groovy.

the class GroovyScriptEngineImpl method getScriptClass.

Class getScriptClass(String script) throws SyntaxException, CompilationFailedException, IOException {
    Class clazz = classMap.get(script);
    if (clazz != null) {
        return clazz;
    }
    clazz = loader.parseClass(script, generateScriptName());
    classMap.put(script, clazz);
    return clazz;
}
Also used : MetaClass(groovy.lang.MetaClass) Class(java.lang.Class) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Example 5 with Class

use of java.lang.Class in project vespa by vespa-engine.

the class DocumentGenPluginTest method pack.

/**
 * Packs the given doc to Book
 * @param d doc, never null
 * @return a Book object or null if input doc isn't a Book
 */
private Book pack(Document d) {
    if (!Book.type.getName().equals(d.getDataType().getName()))
        return null;
    String dataType = d.getDataType().getName();
    Class generated;
    try {
        generated = Class.forName("com.yahoo.vespa.documentgen.test." + className(dataType));
    } catch (ClassNotFoundException e) {
        return null;
    }
    if (generated.getAnnotation(com.yahoo.document.Generated.class) == null)
        return null;
    Book book = new Book(d.getId());
    for (Iterator<Map.Entry<Field, FieldValue>> i = d.iterator(); i.hasNext(); ) {
        Map.Entry<Field, FieldValue> e = i.next();
        Field f = e.getKey();
        FieldValue fv = e.getValue();
        book.setFieldValue(f, fv);
    }
    return book;
}
Also used : Class(java.lang.Class)

Aggregations

Class (java.lang.Class)6 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 MetaClass (groovy.lang.MetaClass)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Method (java.lang.reflect.Method)2 MissingMethodException (groovy.lang.MissingMethodException)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 String (java.lang.String)1 ArrayList (java.util.ArrayList)1 ScriptException (javax.script.ScriptException)1 Decoder (javax.websocket.Decoder)1 DeploymentException (javax.websocket.DeploymentException)1 Encoder (javax.websocket.Encoder)1 WebSocketContainer (javax.websocket.WebSocketContainer)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1