[Javassist user questions] - Accessing static variables...
by DonnieDarko1985
I have the following classes:
public class A {
public static int n =10;
}
---------------------------------
public class B {
public int x;
public B(int n){
this.x =n;
}
public int getx (){
return this.x;
}
public static void main(String args[]){
B b= new B(10);
b.getx();
}
}
-------------------------------------------------------------
Now I am using a Translator to modify the methods in B:
----------------------------------------------------------------
import javassist.*;
public class ChangeClass{
public static void main(String[] args) {
if (args.length >= 2) {
try {
// set up class loader with translator
Translator xlat = new SimpleTranslator(args[0]);
ClassPool pool = ClassPool.getDefault();
Loader loader = new Loader();
loader.addTranslator(pool, xlat);
// invoke "main" method of target class
String[] pargs = new String[args.length-2];
System.arraycopy(args, 2, pargs, 0, pargs.length);
loader.run(args[1], pargs);
System.out.println(A.n);
} catch (Throwable ex) {
ex.printStackTrace();
}
} else {
System.out.println("Usage: TranslateTiming" +
" class-name method-mname main-class args...");
}
}
public static class SimpleTranslator implements Translator
{
private String m_className;
public SimpleTranslator(String cname) {
m_className = cname;
}
public void start(ClassPool pool) {}
public void onLoad(ClassPool pool, String cname)
throws NotFoundException, CannotCompileException {
System.out.println(cname + " "+ m_className);
if (cname.equals(m_className)) {
CtClass newclas = pool.get(cname);
CtMethod [] origMethods = newclas.getDeclaredMethods();
CtMethod currMet;
for (int i = 0; i < origMethods.length-2; i++) {
currMet=(CtMethod)origMethods;
currMet.setBody("{A.n=15;return this."+currMet.getName()+"($$);}");
}
}
}
}
}
--------------------------------------------------------------------------
Now in the bold section....I m setting the static variable(n) of A as 15.
But the output...I get is still 10.
------------------------------------------------------------------------------
Can someone please help me with this?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4151585#4151585
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4151585
18 years, 1 month
[JBoss Tools (users)] - DROOLS: Parsing input files
by amit_tomar
Hi All ,
I am new to this drools technology and want to do a prototype for checking its feasibility for our use.
Most of the places I believe it will work fine.. but I have a use case of parsing input files recd in different formats.
Scenario:
I have 2 input files
1.
##############################
Test config Start
100 bytes available 250 bytes total
1000 bytes is my life goal
##############################
2:
##############################
Test config Start
200 bytes available 300 bytes used 500 bytes total
2000 bytes is my life goal
##############################
I have to extract the numbers from here and populate an output objects.. containing whatever value is possible.. so for the first instance I will be able to populate 3 values.. but for the second one I will be able to extract 4 values
I wrote a file to work on the first case but I am unable to get it working.
Can somebody help me to complete this prototype.. (I am very new to this so please pardon my ignorance.. I just looked at help file and am trying to build this :) )
Thanks for looking..
- Amit
###########################################################
ParsingExample.java
###########################################################
package org.drools.examples;
import java.io.InputStreamReader;
import org.drools.FactHandle;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.compiler.PackageBuilder;
import java.util.*;
public class ParsingExample {
/**
* @param args
*/
public static void main(final String[] args) throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( ParsingExample.class.getResourceAsStream( "Parsing.drl" ) ) );
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( builder.getPackage() );
final StatefulSession session = ruleBase.newStatefulSession();
final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( session );
logger.setFileName( "log/OutputObj" );
String config = "";
config += "Test config Start\n";
config += " 100 bytes available 250 bytes total\n";
config += " 1000 bytes is my life goal";
String[] configA = config.split("\n");
List configL = new ArrayList();
for (int i=0; i < configA.length; i++) {
configL.add(new Row(configA));
}
final Lines l1 = new Lines( configL );
final InputConfig a = new InputConfig( l1 );
final OutputObj t1 = new OutputObj();
session.insert( a );
session.insert( t1 );
config = "";
config += "Test config Start\n";
config += " 200 bytes available 300 bytes used 500 bytes total\n";
config += " 2000 bytes is my life goal";
configA = config.split("\n");
configL = new ArrayList();
for (int i=0; i < configA.length; i++) {
configL.add(new Row(configA));
}
final Lines l2 = new Lines( configL );
final InputConfig b = new InputConfig( l2 );
final OutputObj t2 = new OutputObj();
session.insert( b );
session.insert( t2 );
session.fireAllRules();
session.dispose();
logger.writeToDisk();
}
public static class Row {
private String val;
public Row() {
}
public Row(final String val) {
this.val = val;
}
public String getVal() {
return this.val;
}
public String toString() {
return "[Row " + " : " + this.val + "]";
}
}
public static class Lines {
private List rows;
public Lines() {
}
public Lines(final List rows) {
this.rows = rows;
}
public List getRows() {
return this.rows;
}
public String toString() {
return "[Lines " + " : " + this.rows.toString() + "]";
}
}
public static class InputConfig {
private Lines config;
public InputConfig() {
}
public InputConfig(final Lines config) {
this.config = config;
}
public Lines getConfig() {
return this.config;
}
public String toString() {
return "[InputConfig " + " : " + this.config.toString() + "]";
}
}
public static class OutputObj {
private String bu = "";
private String ba = "";
private String bt = "";
private String blg = "";
public OutputObj() {
}
public String getBu() {
return this.bu;
}
public void setBu(final String bu) {
this.bu = bu;
}
public String getBa() {
return this.ba;
}
public void setBa(final String ba) {
this.ba = ba;
}
public String getBt() {
return this.bt;
}
public void setBt(final String bt) {
this.bt = bt;
}
public String getBlg() {
return this.blg;
}
public void setBlg(final String blg) {
this.blg = blg;
}
public String toString() {
return "[OutputObj " + " used: " + this.bu +" avail: " + this.ba + " total: " + this.bt + " Goal: " + this.blg + "]";
}
}
}
###########################################################
Parsing.drl
###########################################################
package org.drools.examples
import org.drools.examples.ParsingExample.InputConfig;
import org.drools.examples.ParsingExample.OutputObj;
import org.drools.examples.ParsingExample.Lines;
import org.drools.examples.ParsingExample.Row;
rule "get Bytes v10"
salience 10
when
$o : OutputObj()
$i : InputConfig()
InputConfig( $conf : config)
$row : rows( val matches "^\\s*\\d+\s+bytes available\s+\d+\s+bytes total\s*$") from $i.config
then
eval($o.bu = /$row/^\s*(\d+)\s+bytes available\s+\d+\s+bytes total\s*$/)
eval($o.bt = /$row/^\s*\d+\s+bytes available\s+(\d+)\s+bytes total\s*$/)
System.out.println("BU:" + $o.bu);
System.out.println("BT:" + $o.bt);
end
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4151581#4151581
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4151581
18 years, 1 month
[Persistence, JBoss/CMP, Hibernate, Database] - Database Connection Problem
by badhikary81
Hi! All
I have to create a enterprise application .I am using remote server jboss ,J2EE1.4,Mysql and Netbean IDE 6.0.1.1st I have created a Database in remote server using Mysql.When I create a connection using Netbean IDE
then create the problem , show connection failed error.
I have used Host name:remote server IP Address , PORT: 3306, DB: mysql database name which is in remote server database name and username and password of remote server Mysql.
I have also used HOST Name :localhost and rest of all are same.In both case connection failed problem created.
But I have created a JSP page which contain database connection of remote server.Where i mentioned HOST Name:localhost and PORT:3306 and rest of all are same then connection is established.
How to solve this problem please tell me. I need some help.
thanks & regards
Raj.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4151579#4151579
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4151579
18 years, 1 month