I'm trying to write some Bytecode manipulation in my web application now when I try to inject my code into my methods it always throws me the error
javassist.CannotCompileException: [source error] ) is missing
I don't know why and what this is ... I've googled a bit and some people say It's a bug from version 1.0 javassist but I thinks that's really unrealistic.
private void changeMethod(CtMethod method) throws NotFoundException,
CannotCompileException {
if (method.hasAnnotation(Loggable.class)) {
method.getName();
method.insertBefore("long startTime = 0;" +
"long startTime = System.currentTimeMillis();" +
" Thread thread1 = new Thread(new Runnable(){\n" +
" #Override\n" +
" public void run() {\n" +
" threadLogger.info(\"Testlog\");\n" +
"\n" +
" try {\n" +
" threadLogger.logCall(Webservice.this.getClass().getMethod(startThread0), \"Thread\");\n" +
" \n" +
" } catch (Exception e) {\n" +
" e.printStackTrace();\n" +
" }\n" +
"\n" +
" }\n" +
" });\n" +
" thread1.start();");
}
}
enter code here
As you can read in the Javassist documentation, section 4.7 Limitations (bold is mine):
Inner classes or anonymous classes are not supported. Note that this
is a limitation of the compiler only. It cannot compile source code
including an anonymous-class declaration. Javassist can read and
modify a class file of inner/anonymous class.
You are attempting to inject an anonymous Runnable class, so it won't work. Your best way around this issue is to extract the Runnable class code to a new class that is available in the classpath at injection and runtime and use that class in the injection code.
Refers to compilation errors in the source code inside the string. The first problem I can spot is that you have
long startTime = 0;
long startTime = System.currentTimeMillis();
You are defining the variable twice and this won't compile.
Overall the easiest way I have found to write Javassist code is to copy this from your IDE class. This will help you spot most of the problems and can save you some time debugging code in Strings. Of course it is not perfect because most of the time the code won't compile in the IDE because it is referencing something that will work only in code insertion point but it will find problems like double variable etc.
I wrote a Method now and just injectet the method with bytecode manipulation ... was the simplest resolution.
Related
I downloaded a lot of blockchain data using https://bitcoin.org, I took some file and I try to analyse it with bitcoinj library.
I would like to get information from every transaction:
-who send bitcoins,
-how much,
-who receive bitcoins.
I use:
<dependency>
<groupId>org.bitcoinj</groupId>
<artifactId>bitcoinj-core</artifactId>
<version>0.15.10</version>
</dependency>
I have a code:
NetworkParameters np = new MainNetParams();
Context.getOrCreate(MainNetParams.get());
BlockFileLoader loader = new BlockFileLoader(np,List.of(new File("test/resources/blk00450.dat")));
for (Block block : loader) {
for (Transaction tx : block.getTransactions()) {
System.out.println("Transaction ID" + tx.getTxId().toString());
for (TransactionInput ti : tx.getInputs()) {
// how to get wallet addresses of inputs?
}
// this code works for 99% of transactions but for some throws exceptions
for (TransactionOutput to : tx.getOutputs()) {
// sometimes this line throws: org.bitcoinj.script.ScriptException: Cannot cast this script to an address
System.out.println("out address:" + to.getScriptPubKey().getToAddress(np));
System.out.println("out value:" + to.getValue().toString());
}
}
}
Can you share some snippet that will work for all transactions in the blockchain?
There are at least two type of transaction, P2PKH and P2SH.
Your code would work well with P2PKH, but wouldn not work with P2SH.
You can change the line from:
System.out.println("out address:" + to.getScriptPubKey().getToAddress(np));
to:
System.out.println("out address:" + to.getAddressFromP2PKHScript(np)!=null?to.getAddressFromP2PKHScript(np):to.getAddressFromP2SH(np));
The API of Bitcoin says the methods getAddressFromP2PKHScript() and getAddressFromP2SH() are deprecated, and I have not find suitable method.
However, P2SH means "Pay to Script Hash", which means it could contain two or more public keys to support multi-signature. Moreover, getAddressFromP2SH() returns only one address, perhaps this is the reason why it is deprecated.
I also wrote a convinient method to check the inputs and outputs of a block:
private void printCoinValueInOut(Block block) {
Coin blockInputSum = Coin.ZERO;
Coin blockOutputSum = Coin.ZERO;
System.out.println("--------------------Block["+block.getHashAsString()+"]------"+block.getPrevBlockHash()+"------------------------");
for(Transaction tx : block.getTransactions()) {
Coin txInputSum = tx.getOutputSum();
Coin txOutputSum = tx.getOutputSum();
blockInputSum = blockInputSum.add(txInputSum);
blockOutputSum = blockOutputSum.add(txOutputSum);
System.out.println("Tx["+tx.getTxId()+"]:\t" + txInputSum + "(satoshi) IN, " + txOutputSum + "(satoshi) OUT.");
}
System.out.println("Block total:\t" + blockInputSum + "(satoshi) IN, " + blockOutputSum + "(satoshi) OUT. \n");
}
In SAS Open Metadata reference (page 126), it says:
The UpdateMetadata method enables you to update the properties of existing metadata objects. It returns an error if the metadata object to be updated does not exist, unless the OMI_IGNORE_NOTFOUND (134217728) flag is set.
Here is my problem, if I specify the flag or I don't specify the flag, I still get the same error: ("SASLibrary : A5X8AHW1.B40000SQ cannot be found in the wlibrary container in the Foundation repository.")
Here is a snippet that reproduces the error:
import com.sas.meta.SASOMI.IOMI;
import com.sas.metadata.MetadataUtil;
import org.omg.CORBA.StringHolder;
IOMI iOMI = ... // an instance of IOMI connection
StringHolder outputMeta = new StringHolder();
String request = ""
+ "<UpdateMetadata>"
+ " <Metadata>"
+ " <SASLibrary Id=\"A5X8AHW1.B40000SQ\"/>"
+ " </Metadata>"
+ " <NS>SAS</NS>"
+ " <Flags>" + (MetadataUtil.OMI_IGNORE_NOTFOUND | MetadataUtil.OMI_TRUSTED_CLIENT | MetadataUtil.OMI_RETURN_LIST) + "</Flags>"
+ " <Options/>"
+ "</UpdateMetadata>"
;
iOMI.DoRequest(request, outputMeta);
Any ideas what is going wrong?
Contrary to what that document states, I have only seen OMI_IGNORE_NOTFOUND flag work with the DeleteMetadata method.
The javadoc also seems to support this by stating
OMI_IGNORE_NOTFOUND (134217728) This flag is for DeleteMetadata to tell it to ignore objects not found so that it will not return on error.
com.sas.metadata.remote.MdOMIUtil Interface Field Summery
//PRE-SET VARIABLES: symbolsToCheck, time
for (String s : symbolsToCheck) {
String fileName = "daylogs-" + time + "/" + s + ".txt";
File daylog = new File(fileName);
if (!daylog.exists()) {
if (!daylog.createNewFile()) {
System.out.println("ERROR creating day log for " + s);
} else {
System.out.println("Day log created: " + daylog.getCanonicalPath());
}
} else {
System.out.println("ERROR day log already exists for " + s);
}
}
Nothing is outputted from this, and I've confirmed that symbolsToCheck is populated (roughly a dozen strings). I can also confirm that time is set (integer timestamp) well before this code snippet is called. Been scratching my head for quite some time now, any ideas?
I've found the solution from a related post and Tom's suggestion, I've determined that the file creation is breaking due to my attempts to create a new folder and file at the same time, which does not work with createNewFile(). I followed the suggested in the related post and file creation works as expected now.
I'm looking for a way to mask specific code lines in the IDE. I prefer doing it in eclipse, but intellij would be fine also.
The purpose is, that when I have a method like this:
public String serialize() {
log.info("Starting to serialize");
StringBuilder sb = new StringBuilder();
sb.append("name: " + this.name);
sb.append("age: " + this.age);
sb.append("sex: " + this.sex);
log.info("Finished serializing. The result is: " + sb.toString());
return sb.toString();
}
I will make it look like this:
public String serialize() {
StringBuilder sb = new StringBuilder();
sb.append("name: " + this.name);
sb.append("age: " + this.age);
sb.append("sex: " + this.sex);
return sb.toString();
}
This is of course only an example. I work with code that contains a lot of log lines and a lot of metrics for better monitoring of the app. This clutters the code massively and I wish to only see code logic when I code.
Does either of the mentioned IDE (or another one) enable this feature?
That is which aspect oriented programming is for. This is a simple thing, only same programmer/marketing people gave it this flam name.
You should hook the serialize() function/method with your logging chunk. Quiet simple.
I suggest you start some googling for "AspectJ".
I have a program which scans a Java file for errors. I call the compiler from Eclipse and run a scan on the Java file and get the line numbers, start position and end positon as output. I have written a program to extract the error from the file.
What I want to do is to have access to the quickfix component in eclipse, have a list of possible fixes for the problem, and print that list to the console.
Below is a portion of the program of how I called the compiler and got the details printed on the console:
Iterable fileObjects = fileManager.getJavaFileObjectsFromStrings(
Arrays.asList(fileToCompile));
CompilationTask task = compiler.getTask(null, fileManager, listener, null,
null, fileObjects);
Boolean result = task.call();
if(result == true) {
System.out.println("Compilation has succeeded");
}
myerrors = listener.getlistofErrors();
for (CaptureErrors e : myerrors) {
System.out.println("Code: " + e.getCode());
System.out.println("Kind: " + e.getKind());
System.out.println("Line Number: " + e.getLinenumber());
// System.out.println("Message: "+ e.getMessage(Locale.ENGLISH));
// System.out.println("Source: " + diagnostic.getSource());
System.out.println("End position"+ e.getEndposition());
System.out.println("Position: "+ e.getPosition());
System.out.println("\n");
}
class MyDiagnosticListener implements DiagnosticListener {
List<CaptureErrors> errors = new ArrayList<CaptureErrors>();
public void report(Diagnostic diagnostic) {
CaptureErrors single_error = new CaptureErrors(diagnostic.getCode(),
diagnostic.getKind(), diagnostic.getLineNumber(),
diagnostic.getMessage(Locale.ENGLISH), diagnostic.getPosition(),
diagnostic.getEndPosition());
errors.add(single_error);
}
public List<CaptureErrors> getlistofErrors() {
return errors;
}
}
I also have a program to go to the line numbers and extract the text(error) at specific positions.
How can I call eclipse quickfix solutions for the specific errors that I find?
Yes, it is possible. The exact details are a bit larger than can easily be encompassed in even a StackOverflow answer.
Eclipse is extended through the use of plug-ins. A "Hello World" plugin is detailed here, and after you get through the initial learning curve, you can download other plugins to get a feel for how eclipse works internally.
I imagine that you would do well to examine the current code highlighting capabilities of eclipse and read the source code for those plugins as a guide, but only after you get some idea of how plugin development works.