I am trying to use Soot to perform data flow analysis on a java file which is called Example.java.
Here is my Example.java file, my goal is to know which saySomething method animal.saySomething() will call. Here is the code for Example.java I am using:
package a1;
public class Example {
static Animal neverCalled() {
return new Fish();
}
static Animal selectAnimal() {
return new Cat();
}
public static void main(String[] args) {
Animal animal = selectAnimal();
animal.saySomething();
}
}
abstract class Animal {
public abstract void saySomething();
}
class Cat extends Animal {
public void saySomething() {
System.out.println("purr");
}
}
class Dog extends Animal {
public void saySomething() {
System.out.println("woof");
}
}
class Fish extends Animal {
public void saySomething() {
System.out.println("...");
}
}
class Car { // not an Animal
public void saySomething() {
System.out.println("honk!");
}
}
and here is the code I am using to analyze Example.java using Soot, this code is located in the file: TestSootCallGraph.java which follows here:
package a1;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import soot.*;
import soot.jimple.Stmt;
import soot.jimple.spark.SparkTransformer;
import soot.jimple.toolkits.callgraph.CHATransformer;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Targets;
import soot.options.Options;
public class TestSootCallGraph extends SceneTransformer {
static LinkedList<String> excludeList;
public static void main(String[] args) {
String mainclass = "Example";
// //set classpath
String javapath = System.getProperty("java.class.path");
String jredir = System.getProperty("java.home")+"/lib/rt.jar";
String path = javapath+File.pathSeparator+jredir;
Scene.v().setSootClassPath(path);
//add an intra-procedural analysis phase to Soot
TestSootCallGraph analysis = new TestSootCallGraph();
PackManager.v().getPack("wjtp").add(new Transform("wjtp.TestSootCallGraph", analysis));
excludeJDKLibrary();
//whole program analysis
Options.v().set_whole_program(true);
//load and set main class
Options.v().set_app(true);
SootClass appclass = Scene.v().loadClassAndSupport(mainclass);
System.out.println(appclass);
Scene.v().setMainClass(appclass);
Scene.v().loadNecessaryClasses();
//enable call graph
//enableCHACallGraph();
//enableSparkCallGraph();
//start working
PackManager.v().runPacks();
}
private static void excludeJDKLibrary()
{
//exclude jdk classes
Options.v().set_exclude(excludeList());
//this option must be disabled for a sound call graph
Options.v().set_no_bodies_for_excluded(true);
Options.v().set_allow_phantom_refs(true);
}
private static void enableSparkCallGraph() {
//Enable Spark
HashMap<String,String> opt = new HashMap<String,String>();
//opt.put("propagator","worklist");
//opt.put("simple-edges-bidirectional","false");
opt.put("on-fly-cg","true");
//opt.put("set-impl","double");
//opt.put("double-set-old","hybrid");
//opt.put("double-set-new","hybrid");
//opt.put("pre_jimplify", "true");
SparkTransformer.v().transform("",opt);
PhaseOptions.v().setPhaseOption("cg.spark", "enabled:true");
}
private static void enableCHACallGraph() {
CHATransformer.v().transform();
}
private static LinkedList<String> excludeList()
{
if(excludeList==null)
{
excludeList = new LinkedList<String> ();
excludeList.add("java.");
excludeList.add("javax.");
excludeList.add("sun.");
excludeList.add("sunw.");
excludeList.add("com.sun.");
excludeList.add("com.ibm.");
excludeList.add("com.apple.");
excludeList.add("apple.awt.");
}
return excludeList;
}
#Override
protected void internalTransform(String phaseName,
Map options) {
int numOfEdges =0;
CallGraph callGraph = Scene.v().getCallGraph();
for(SootClass sc : Scene.v().getApplicationClasses()){
for(SootMethod m : sc.getMethods()){
Iterator<MethodOrMethodContext> targets = new Targets(
callGraph.edgesOutOf(m));
while (targets.hasNext()) {
numOfEdges++;
SootMethod tgt = (SootMethod) targets.next();
System.out.println(m + " may call " + tgt);
}
}
}
System.err.println("Total Edges:" + numOfEdges);
}
}
I receive the following error when executing TestSootCallGraph.java which aims at analyzing Example.java. How can I fix this?
Example
Exception in thread "main" java.lang.RuntimeException: Main-class has no main method!
at soot.Scene.setMainClass(Scene.java:171)
at a1.TestSootCallGraph.main(TestSootCallGraph.java:47)
Related
Error: Main method not found in class jsone.testing, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
package jsone;
import java.io.File;
import java.io.IOException;
import org.apache.xpath.operations.String;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class testing {
#Test
public static void main(String args[]) {
ObjectMapper mapper = new ObjectMapper();
try {
File jsonInputFile = new File("D:\\workspace\\jsone\\car.json");
car emp = mapper.readValue(jsonInputFile, car.class);
System.out.println(emp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package jsone;
public class car{
private String colour;
public String getcolour() {
return colour;
}
public void setcolour(String colour) {
this.colour = colour;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\n----- Employee Information-----\n");
sb.append("Colour: " + getcolour() + "\n");
sb.append("*****************************");
return sb.toString();
}
}
In your example String class comes from org.apache.xpath.operations.String package which is wrong. You should use java.lang.String class instead. Classes from java.lang package are visible in class by default so you have to delete below line:
import org.apache.xpath.operations.String;
and it should start work. Also, your main method should not be annotated with #Test annotation.
Also, please take a look on this question:
Error: Main method not found in class MyClass, please define the main method as…
Suppose that I have a .class file, can I get all the methods included in that class ?
Straight from the source: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
Then I modified it to be self contained, not requiring anything from the command line. ;-)
import java.lang.reflect.*;
/**
Compile with this:
C:\Documents and Settings\glow\My Documents\j>javac DumpMethods.java
Run like this, and results follow
C:\Documents and Settings\glow\My Documents\j>java DumpMethods
public void DumpMethods.foo()
public int DumpMethods.bar()
public java.lang.String DumpMethods.baz()
public static void DumpMethods.main(java.lang.String[])
*/
public class DumpMethods {
public void foo() { }
public int bar() { return 12; }
public String baz() { return ""; }
public static void main(String args[]) {
try {
Class thisClass = DumpMethods.class;
Method[] methods = thisClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].toString());
}
} catch (Throwable e) {
System.err.println(e);
}
}
}
To know about all methods use this statement in console:
javap -cp jar-file.jar packagename.classname
or
javap class-file.class packagename.classname
or for example:
javap java.lang.StringBuffer
You can use the Reflection API
package tPoint;
import java.io.File;
import java.lang.reflect.Method;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class ReadClasses {
public static void main(String[] args) {
try {
Class c = Class.forName("tPoint" + ".Sample");
Object obj = c.newInstance();
Document doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new File("src/datasource.xml"));
Method[] m = c.getDeclaredMethods();
for (Method e : m) {
String mName = e.getName();
if (mName.startsWith("set")) {
System.out.println(mName);
e.invoke(obj, new
String(doc.getElementsByTagName(mName).item(0).getTextContent()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have started making a mod, it's not registering as an item. When i type /give Fidojj222 fcm:fuel_canister it should give me the item except it says it doesn't exist! I am using eclipse as my IDE I am suspecting it might be this warning when I compile it into a jar:
JAR export finished with warnings. See details for additional information.
Can not export external class folder at 'C:\Users\J.J\.gradle\caches\minecraft\net\minecraftforge\forge\1.8-11.14.3.1450\start'.
If that is the problem, then how can I fix it? If not here's my code:
CarsMod.java:
package com.fidojj222.carsmod;
import com.fidojj222.carsmod.init.CarsItems;
import com.fidojj222.carsmod.proxy.CommonProxy;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
#Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class CarsMod {
#SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
public static CommonProxy proxy;
public void PreInit(FMLPreInitializationEvent event){
CarsItems.init();
CarsItems.register();
}
public void Init(FMLInitializationEvent event){
proxy.registerRenders();
}
public void PostInit(FMLPostInitializationEvent event){
}
}
Reference.java:
package com.fidojj222.carsmod;
public class Reference {
public static final String MOD_ID = "fcm";
public static final String MOD_NAME = "Fidojj222\'s Cars Mod";
public static final String VERSION = "1.0";
public static final String CLIENT_PROXY_CLASS = "com.fidojj222.carsmod.proxy.ClientProxy";
public static final String SERVER_PROXY_CLASS = "com.fidojj222.carsmod.proxy.CommonProxy";
}
CarsItems.java:
package com.fidojj222.carsmod.init;
import com.fidojj222.carsmod.Reference;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;
public class CarsItems {
public static Item fuel_canister;
public static void init(){
fuel_canister = new Item().setUnlocalizedName("fuel_canister");
}
public static void register(){
GameRegistry.registerItem(fuel_canister, fuel_canister.getUnlocalizedName().substring(5));
}
public static void registerRenders(){
registerRender(fuel_canister);
}
public static void registerRender(Item item){
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}
}
CommonProxy.java:
package com.fidojj222.carsmod.proxy;
public class CommonProxy {
public void registerRenders(){
}
}
ClientProxy.java:
package com.fidojj222.carsmod.proxy;
import com.fidojj222.carsmod.init.CarsItems;
public class ClientProxy extends CommonProxy {
#Override
public void registerRenders(){
CarsItems.registerRenders();
}
}
What do you mean by not showing? The item isn't found at all in the creative search menu, or it is an untextured (purple/black checkered) block?
If it is untextured, you need to make sure these 2 things are done:
Make sure you have this texture in place src/main/resources/assets/fcm/textures/items/fuel_canister.png it needs to be 16x16 pixels.
Create a fuel_canister.json file at src/main/resources/assets/fcm/models/item/fuel_canister.json This file defines how the image should be rendered ingame.
The contents of that file should be
{
"parent": "builtin/generated",
"textures":{
"layer0":"fcm:items/fuel_canister"
},
"display":{
"thirdperson":{
"rotation":[-90, 0, 0],
"translation":[0, 1, -3],
"scale":[0.55,0.55,0.55]
},
"firstperson":{
"rotation":[0,-135,25],
"translation":[0,4,2],
"scale":[1.7,1.7,1.7]
}
}
}
How is it possible to export a java method or object using dbus?
I am writing this because the official documentation is very poor and it took me hours to figure out how to do it.
Ideally the DBus interface should go in a java package
DBus.java
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;
#DBusInterfaceName("org.printer")
public interface DBus extends DBusInterface {
//Methods to export
public void Print(String message);
}
Main.java
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
public class Main {
public static void main(String[] args) {
Printer p = new Printer();
try {
DBusConnection conn = DBusConnection.getConnection(DBusConnection.SESSION);
//Creates a bus name, it must contain some dots.
conn.requestBusName("org.printer");
//Exports the printer object
conn.exportObject("/org/printer/MessagePrinter", p);
} catch (DBusException DBe) {
DBe.printStackTrace();
conn.disconnect();
return;
}
}
}
//Printer object, implements the dbus interface and gets
//called when the methods are invoked.
class Printer implements DBus {
public boolean isRemote() {
return false;
}
public void Print(String message) {
System.out.println(message);
}
}
You can try this out with qdbus from the shell, running:
qdbus org.printer /org/printer/MessagePrinter org.printer.Print test
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());