Soot: How to analyze a java file in a package? - java

I have two java files under the tests directory.
I use the following code to set up Soot for further analysis(i.e., construct a call graph) but meet an error of are the packages set properly?.
Main.java:
public class Main {
static void setupSoot() {
Options.v().set_prepend_classpath(true);
Options.v().set_process_dir(Collections.singletonList("./tests"));
Options.v().set_whole_program(true);
Scene.v().loadNecessaryClasses();
}
// Following function also doesn't work and has a similar error message
//static void setupSoot() {
// Options.v().set_prepend_classpath(true);
// Options.v().set_process_dir(Collections.singletonList("./tests"));
// Scene.v().loadClassAndSupport("FooBar");
//}
public static void main(String[] args) {
setupSoot();
// ....
}
}
And I get the following error message:
Exception in thread "main" java.lang.RuntimeException: Error: couldn't find class: FooBar are the packages set properly?
at soot.JastAddInitialResolver.resolveFromJavaFile(JastAddInitialResolver.java:119)
at soot.JavaClassSource.resolve(JavaClassSource.java:69)
at soot.SootResolver.bringToHierarchyUnchecked(SootResolver.java:253)
at soot.SootResolver.bringToHierarchy(SootResolver.java:221)
at soot.SootResolver.bringToSignatures(SootResolver.java:292)
at soot.SootResolver.bringToBodies(SootResolver.java:332)
at soot.SootResolver.processResolveWorklist(SootResolver.java:171)
at soot.SootResolver.resolveClass(SootResolver.java:141)
at soot.Scene.loadClass(Scene.java:1009)
at soot.Scene.loadClassAndSupport(Scene.java:994)
at soot.Scene.loadNecessaryClasses(Scene.java:1822)
at Main.setupSoot(Main.java:18)
at Main.main(Main.java:21)
If I complie two test files into .class files then I will get following error message:
Exception in thread "main" soot.SootResolver$SootClassNotFoundException: couldn't find class: tests.FooBar (is your soot-class-path set properly?)
at soot.SootResolver.bringToHierarchyUnchecked(SootResolver.java:245)
at soot.SootResolver.bringToHierarchy(SootResolver.java:221)
at soot.SootResolver.bringToSignatures(SootResolver.java:292)
at soot.SootResolver.bringToBodies(SootResolver.java:332)
at soot.SootResolver.processResolveWorklist(SootResolver.java:171)
at soot.SootResolver.resolveClass(SootResolver.java:141)
at soot.Scene.loadClass(Scene.java:1009)
at soot.Scene.loadClassAndSupport(Scene.java:994)
at soot.Scene.loadNecessaryClasses(Scene.java:1822)
at Main.setupSoot(Main.java:18)
at Main.main(Main.java:21)
I beleive that soot prepend_classpath is true since it works well when two test files are not in the package (i.e., remove package tests from both files).
Two files in tests directory are as follows:
Pack.java:
package tests;
public class Pack {
public static void main(String[] args) {
int s = 10;
}
}
FooBar.java
package tests;
public class FooBar {
public static void main(String[] args) {
FooBar callFooBar = new FooBar();
callFooBar.foo(10);
}
void foo(int a) {
bar(a);
}
void bar(int a) {
for (int i = 0; i < a; i++) {
i += a;
}
}
}

I solved this problem. The issue is that the path of the test files is wrong.
Now my project structure is as follows:
-ProjectRoot
|--src
|--Main.java
|--testers
|--easycase
|--FooBar.java
|--Pack.java
and My Main.java:
public class Main {
static void setupSoot() {
Options.v().set_prepend_classpath(true);
// Options.v().set_soot_classpath("xxxxx:xxxxx/xxxxx.jar") // For external packages
Options.v().set_process_dir(Collections.singletonList("./testers"));
Options.v().set_whole_program(true);
Scene.v().loadNecessaryClasses();
}
public static void main(String[] args) {
setupSoot();
Scene.v().loadAndSupport("esaycase.FooBar");
// Do something here
}
}
Note that the first line is package easycase; for both test files.

Related

Error:(7, 22) java: ')' expected when executing my code

I receive the following error:
Error:(7, 22) java: ')' expected when executing my code:
public class Main {
public static void main(String[] args) {
// write your code here
myName( mName:"Gowtham");
}
public static void myName(String mName) {
System.out.println(mName);
}
}
This is the code I wanted to execute but it tells it has errors, learning it through some online courses.
The error is here
myName( mName:"Gowtham");
Just write it as following:
public class Main {
public static void main(String[] args) {
// write your code here
myName("Gowtham");
}
public static void myName(String mName) {
System.out.println(mName);
}
}

First exception handling

I am trying to do a very basic code but got an error from jvm. Someone can help me ? I really can't find any solution :( The error states : cannot find any symbol when is trying to create the bello object.
import java.util.*;
import java.io.*;
public class Test {
public static void main (String[] args) {
try {
bello ola = new bello();
ola.ciao(3);
} catch( BadException se) {
} finally {
} // end of try
class bello {
void ciao (int i) throws BadException {
if (i == 5 ) {
throw new BadException();
} // end of if
}
}
class BadException extends Exception {
public BadException() {
}
}
}
}
Classes declared in methods may only be used after their declaration in the source code. Please note, however, that declaring classes in methods is strongly discouraged. Also, as noted by JB Nizet, please indent your code/respect naming conventions if you want to be able to debug anything, ever.
Did you write everything in this one file?
And you wrote the class-definitions into the main-function?
In Java you have do split up your classes in a separate file.
By the way the convention for classnames is Uppercase in the first letter.
Test.java
public class Test{
public static void main (String[] args) {
...
}
}
Bello.java
class Bello {
...
}
BadException.java
class BadException extends Exception {
public BadException() {
...
}
}

Fluent getters generate errors

I have a project with the following lombok.config file:
lombok.accessors.chain = true
lombok.accessors.fluent = true
So the following class should compile fine:
#Data class A {
private int i;
public static void main(String[] args) {
new A().i();
}
}
and it does when compiling with javac. But Intellij (with or without the lombok plugin) shows a compilation error and the auto completion suggests using getI() which does not exist.
How can I fix this?
I think you're facing issue 53.
As a workaround, you could use #Accessors:
#Accessors(fluent = true) // order matters
#Data
class A {
private int i;
public static void main(String[] args) {
new A().i();
}
}
compile fine here (IntelliJ 14.0.3, lombok-plugin 0.8.9)

Nashorn. Binding native Java objects?

I'd like to put native Java objects into the ScriptEngine bindings for easier access.
I mean to avoid lots of Java.type(...).
I tried in that way.
jsEngine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).put("manager", Manager.getInstance());
But that's failed with error "Manager has no such function "funcName" in eval...".
Is it possible at all?
UPD:
Example code
public class ManagerClass {
public void test()
{
System.out.println("Hello");
}
public static void test2()
{
System.out.println("Hello Static");
}
}
public class NewClass {
public static void main(String[] args) throws ScriptException {
final ScriptEngine s = new ScriptEngineManager().getEngineByExtension("js");
s.getBindings(ScriptContext.ENGINE_SCOPE).put("manager", new ManagerClass());
s.eval("manager.test(); manager.test2();");
}
}
Solved.
The correct way is
s.eval("manager.test(); manager.class.static.test2();");

Simple App Won't Compile in Eclipse (with plugin)?

my code, being practically identical to the code given in BlackBerry's tutorial, has a syntax error in Eclipse. i'm sure there is some small but i'm just not seeing, but my coworker could not find it as well. any ideas would be greatly appreciated. thanks!
Code:
pushScreen(new ABCScreen());
Error:
Cannot make a static reference to the
non-static method pushScreen(Screen)
from the type UiApplication
here is the complete source:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class AwesomeBBCalculator extends UiApplication {
public AwesomeBBCalculator() {
AwesomeBBCalculator app = new AwesomeBBCalculator();
app.enterEventDispatcher();
}
public static void main(String[] args) {
pushScreen(new ABCScreen()); // ERROR LINE
}
}
final class ABCScreen extends MainScreen {
public ABCScreen() {
super();
// add title
LabelField title = new LabelField("Awesome BlackBerry Calculator",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose() {
Dialog.alert("Thanks for using the Awesome BlackBerry Calculator!\nGoodbye.");
System.exit(0);
return true;
}
}
The pushScreen method can only be called within an instance of UiApplication. You are trying to call it from a static main method. That does not work. Do this instead...
public void foo()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new ABCScreen()).foo();
}
public void class1()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new NewScreen()).class1();
}
try making an object for the ABCScreen class and then use it or u may try this also:
UiApplication.getUiApplication().pushScreen(new ABCScreen());

Categories