Falci
4/24/2014 - 1:32 PM

Javascript.java

package me.falci.planoB.javascript;

import java.util.Date;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {

	public static void main(String[] args) throws ScriptException {
		ScriptEngineManager factory = new ScriptEngineManager();
	    ScriptEngine engine = factory.getEngineByName("JavaScript");
	    
	    
	    Complex complex = new Complex(123L, "Falci", new Date());
		engine.put("complex", complex);
	    
		engine.eval("print( complex + '\\n');");
		engine.eval("complex.campoLivre = 'Teste';");
		engine.eval("print( complex + '\\n');");
		
	    engine.eval("print( complex.id + ') '+ complex.nome +' : '+ complex.dob.time )");
	}
	
	public static class Complex {
		private final Long id;
		private final String nome;
		private final Date dob;
		private String campoLivre;
		
		public Complex(Long id, String nome, Date dob) {
			super();
			this.id = id;
			this.nome = nome;
			this.dob = dob;
		}
		public Long getId() {
			return id;
		}
		public String getNome() {
			return nome;
		}
		public Date getDob() {
			return dob;
		}

		@Override
		public String toString() {
			return "Complex [id=" + id + ", nome=" + nome + ", dob=" + dob + ", campoLivre=" + campoLivre + "]";
		}
		public String getCampoLivre() {
			return campoLivre;
		}
		public void setCampoLivre(String campoLivre) {
			this.campoLivre = campoLivre;
		}
		
		
	}
}