Argomento precedente: Configurazione dell'operatore Run Java Code (Esegui codice Java)Argomento successivo: Operatori personalizzati


Utilizzo di un JavaObject

Gli oggetti Java vengono salvati dopo che l'operatore Run Java Code (Esegui codice Java) è stato completato in un tipo di dati JavaObject. È possibile utilizzare una variabile di set di dati JavaObject nei modi seguenti:

Tenere conto dei vincoli seguenti quando si lavora in JavaScript:

Il codice Java scritto può essere costituito da istruzioni ed espressioni Java normali. È anche possibile definire i propri metodi e utilizzarli all'interno del codice. Ad esempio:

// Import the classes that you want to use
import ca.tech.pam.MyAccount;
// Note: no need to import StringBuffer and Date because they are part of the 
// automatically imported packages
// import java.lang.StringBuffer;
// import java.util.Date;
// Note: the jar that contains the ca.tech.pam.MyAccount class 
// must be in the list of External Jars of the operator or the module;
// but java lang and java util are in rt.jar, which is automatically put in the classpath

MyAccount acct = new MyAccount(1000.00);

// Use the public methods of the MyAccount object
acct.addFunds(34.44);
acct.subFunds(10);

// Define your own method
String getStatement(MyAccount acc) {
    StringBuffer strBuff = new StringBuffer("Account Balance: " + acc.getBalance());
    Date dt = new Date(System.currentTimeMillis());
    strBuff.append(" on date: " + dt);
    return strBuff.toString();
}
// Use the method you defined
// also print the statement using the 'logger' object that you 
// setup in the 'Logger' page of the operator 
logger.info(getStatement(acct));

Dopo che è stato eseguito questo codice Java, il messaggio di registro mostra il bilancio dell'account, la data e l'ora:

Account Balance: 124.44 on date: Wed Jul 13 12:53:37 EDT 2011