I'm trying to analyse some bits of Java-code, looking if the code is written too complexly. I start with a String containing the contents of a Java-class.
From there I want to retrieve, given a function-name, the "inner code" by that function. In this example:
public class testClass{
public int testFunction(char x) throws Exception{
if(x=='a'){
return 1;
}else if(x=='{'){
return 2;
}else{
return 3;
}
}
public int testFunctionTwo(int y){
return y;
}
}
I want to get, when I call String code = getcode("testFunction");, that code contains if(x=='a'){ ... return 3; }. I've made the input code extra ugly, to demonstrate some of the problems one might encounter when doing character-by-character-analysis (because of the else if, the curly brackets will no longer match, because of the Exception thrown, the function declaration is not of the form functionName{ //contents }, etc.)
Is there a solid way to get the contents of testFunction, or should I implement all problems described manually?
You need to a java parser. I worked too with QDox. it is easy to use. example here:
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import java.io.File;
import java.io.IOException;
public class Parser {
public void parseFile() throws IOException {
File file = new File("/path/to/testClass.java");
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.addSource(file);
for (JavaClass javaClass : builder.getClasses()) {
if (javaClass.getName().equals("testClass")) {
for (JavaMethod javaMethod : javaClass.getMethods()) {
if (javaMethod.getName().equals("testMethod")) {
System.out.println(javaMethod.getSourceCode());
}
}
}
}
}
}
Have you considered using a parser to read your code? There are a lot of parsers out there, the last time I worked on a problem like this http://qdox.codehaus.org made short work of these kinds of problems.
Related
This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 3 years ago.
I want to create a java program that generates another java class in the same project. For example in the class Dragon.java, i want to write java code that creates another java class called fire.java. I do not want to use any GUI from eclipse, just pure code that generates another class from the execution of written programming in java.
I have tried making objects of a non existent class in hopes of the program automatically producing a class with that name.
Again, it doesn't have to be just a java class, is there a way to make other forms of files also? for example fol.flow, or of different names.
Creating a new Java file is easy. You can use any FileWriter technique. But what need to be taken care of is that new Java file is valid java file and can be compiled to class file.
This link has working example of doing the same.
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":\n\n");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(\""+todayMillis+"\");");
aWriter.write(" }}\n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = { new String(todaySource)};
ByteArrayOutputStream baos= new ByteArrayOutputStream();
new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
// public static int com.sun.tools.javac.Main.compile(source);
return (baos.toString().indexOf("error")==-1);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
At first I thought you wanted code generation, but you simply want to write to files or create them?
The simplest code to create file and write to it:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Testing {
public static void main(String[] args) throws IOException {
Files.writeString(Paths.get("D://output.txt"), "some text to write", StandardOpenOption.CREATE);
}
}
It uses only java standard classes, you don't need any libraries or anything external. Just make sure to write to the valid path, where you have access.
If you want to generate files with java code, you can just do it with the method above, but creating the String with code content is really hard, there are libraries for it and they are not easy to use for beginners. For example javapoet. I personally used javaparser, it has a lot of other possibilities besides generating code.
I ran into a very strange problem that I don't know if I'm even allowed to do.
Basically I have two functions witch should have the same name but get different parameter objects which have the same name.
This is because I want to write a plugin for the game Minecraft and this should be compatible with BungeeCord and Bukkit servers.
public static void sendMessage(org.bukkit.command.CommandSender p, String k, Object...i){
//fancy stuff
}
public static void sendMessage(net.md_5.bungee.api.CommandSender p, String k, Object...i){
//fancy stuff
}
If the plugin is loaded by a Bukkit server the plugin it doesn't know anything about net.md_5.bungee.api.CommandSender since this is a class of the BungeeCord server core and the same is for org.bukkit.command.CommandSender where it is used by Bukkit but not by BungeeCore.
I have no problem compiling the code with IntellIJ even dough I'm a bit sceptic because if decompiled it looks like this:
import org.bukkit.command.CommandSender;
public static void sendMessage(CommandSender p, String k, Object...i){
//fancy stuff
}
public static void sendMessage(net.md_5.bungee.api.CommandSender p, String k, Object...i){
//fancy stuff
}
My first question is: Can I even do this, or will this give exceptions since not all Classes are loaded, even dough it will never get accessed?
Now if the first question can be answered by Sure you can then why is there a compilation problem by compiling eigther a Bukkit or a BungeeCord plugin using this sendMessage( function?
Bukkit:
BungeeCord:
Because if this doesn't work I know for sure that you can at least work with Classes that aren't loaded if you put them into your codeblock since this code works just fine and isn't even throwing an exception when not loaded by a server that is using org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer aldough it is in the imports:
import org.bukkit.entity.Player;
import org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer;
public static int getPing(Player p) {
String version = getVersion(instance.getServer());
if (version.startsWith("v1_8")) {
return ((org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_9")) {
return ((org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_10")) {
return ((org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_11")) {
return ((org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_12")) {
return ((org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else {
return ((CraftPlayer)p).getHandle().playerConnection.player.ping;
}
}
So is this really a thing I simply cannot do or is this a problem of the compiler of IntellIJ and if so how can I fix it?
Well my attempt to your idea would be to call methods in sub classes. It might be an issue that the non found class is a parameter. When the class is accessed (just my speculations) the parameters are tried to load to determine which method to use.
So something like the following would be the output.
In the class you access have:
public static void sendMessage(Object player, String k, Object...i){
if(isBukkit())
MyBukkitUtils.sendMessage(player, k, i);
else
MyBungeeUtils.sendMessage(player, k, i);
}
MyBukkitUtils:
public static void sendMessage(Object player, String k, Object...i){
if(!(player instanceOf CommandSender))
return;
CommandSender p = (CommandSender) player;
//fancy stuff
}
Same for MyBungeeUtils just with the BunggeeCommandSender.
I don't know your code, but if you have to have everything seperated you can just code two plugins (one for spigot, one for bungee) and use a include a library in both were the common code is placed.
This question is about Android, although i dont think that this is android-specific.
I have a project that i want to use two files with: MainActivity.java and filetools.java . I have three methods in filetools.java, read, write and append.
I want to be able to do something like this in my MainActivity:
filetools.write("/sdcard/file.txt", "something");
The code for MainActivity is just the package, imports, the class, and onCreate.
The code for filetools:
package com.tylerr147.FileRW;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class filetools
{
public String read(String fName){
try{
File mFile = new File(fName);
String content = new Scanner(mFile).useDelimiter("\\Z").next();
return content;
}catch(Exception e) {
return "There was an error retrieving your file. The proccess returned this error:\n"+e.toString();
}
}
public boolean write(String loc, String stuff) {
File mfile = new File(loc);
try {
mfile.createNewFile();
FileOutputStream f = new FileOutputStream(mfile);
OutputStreamWriter f2 = new OutputStreamWriter(f);
f2.append(stuff);
f2.close();
f.close();
} catch(IOException e) {
return false;
}
return true;
}
public void append(String filename, String content) {
write(filename, read(filename)+content);
}
}
Another thing i would like to be able to do is to have a completely different app by the package com.app.importer
how could i do something like
import com.app.importer;
importerAppsMethod();
I have found a few posts on stackoverflow, but they do not help.
Importing my custom class and calling it's method?
There are a few more, and i have searched and can not find anything that works for me. Any help is appreciated
I am putting #ishmaelMakitla comment intocan answer.
To do something like filetools.write("/sdcard/file.txt", "something"); - you need to declare the write method as static. For example: public static boolean write(String loc, String stuff). You may have to do the same for all other methods if you want similar behavior. Is this what you are looking for?
I wanted to print the whole string pool which contains literals and String objects added using intern() just before garbage collection.
Is there a method implicit to JDK for such operation? How can we inspect the string pool?
EDIT: The comment suggests that there may be a misunderstanding regarding what this "hack" does. It prints the strings that have been interned by (directly or indirectly) calling intern(), as described in the question. It will not print the "whole string pool", as the string pool only resides in the JVM, is filled with symbols and strings that appear during classloading and initialization, and not accessible from Java side.
NeplatnyUdaj mentioned in a comment that it might be possible to define a new java.lang.String class and sneak this into the JVM at startup. I was curious, and tried it out. And what should I say: It works!
1. Create a new project that contains the package java.lang
2. Insert a class like this into this package
package java.lang;
import java.util.LinkedHashSet;
import java.util.Set;
public class StringPool {
private static Set<String> pool = null;
public static synchronized void store(String string)
{
try
{
if (pool == null)
{
pool = new LinkedHashSet<String>();
}
pool.add(string);
}
catch (Exception e)
{
// Ignore
}
}
public static synchronized Set<String> getPool()
{
return new LinkedHashSet<String>(pool);
}
}
3. Copy & Paste the original java.lang.String class into this package. Surprisingly, this works without many problems. It will complain about a single function, namely a call to
h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, 0, value.length);
that can safely be replaced with
h = 0;
4. Change the String#intern() method of the new String class. Originally, this is a native method. It can be replaced with something like
public String intern()
{
StringPool.store(this);
return this;
}
5. Create a .JAR file from this project, and store it, for example, as newString.jar
6. Create another project with a test class that generates/contains/uses some strings. (that should be easy) and compile this class, which may be named NewStringTest
7. Launch the test program with the modified string class:
java -Xbootclasspath:newString.jar;C:\jre\lib\rt.jar NewStringTest
The StringPool#getPool() method can then be used to obtain the pool containing the interned strings.
I just tested this with the following class, which manually creates some strings, and some Swing components (which can be expected to contain some strings):
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class NewStringTest
{
public static void main(String[] args)
{
generateSomeStrings();
System.out.println(StringPool.getPool());
}
private static void generateSomeStrings()
{
String s = "This is some test string";
for (int i=0; i<10; i++)
{
String t = s + i;
t.intern();
}
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run() {
JFrame frame = new JFrame();
JTable table = new JTable();
}
});
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
And the output is
[hashSeed, value, buf, J, D, Z, seed, segmentShift, segmentMask,
segments, state, head, tail, waitStatus, next, Ljava/lang/String;,
I, [C, [J, Ljava/util/Hashtable;, Ljava/security/PermissionCollection;,
Ljava/util/Vector;, Ljava/lang/Class;, main, This is some test string0,
This is some test string1, This is some test string2,
This is some test string3, This is some test string4,
This is some test string5, This is some test string6,
This is some test string7, This is some test string8,
This is some test string9, INSTANCE, es, , ES, sv, SE,
values, Ljava/lang/Object;, [Ljava/awt/Component;,
Ljava/awt/LayoutManager;, Ljava/awt/LightweightDispatcher;,
Ljava/awt/Dimension;, createUI, invoke, VK_F10,
VK_CONTEXT_MENU, VK_SPACE, VK_LEFT, VK_KP_LEFT,
VK_RIGHT, VK_KP_RIGHT, VK_ESCAPE, VK_C, VK_V, VK_X,
VK_COPY, VK_PASTE, VK_CUT, VK_INSERT, VK_DELETE,
VK_DOWN, VK_KP_DOWN, VK_UP, VK_KP_UP, VK_HOME, VK_END,
VK_PAGE_UP, VK_PAGE_DOWN, VK_TAB, VK_ENTER, VK_A,
VK_SLASH, VK_BACK_SLASH, VK_F2, VK_F8]
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize%28%29 , So the GC calls finalize method before clean-up any of the objects.
So the finalize method in String is also getting called. But sadly String is a final class and you cannot override it. (Why is String class declared final in Java?)
But If you really want to get this thing to be worked, then you need to create your own string object named something else, but inner behaviour will keep all the strings functions.
And for a guaranteed GC try this : http://code.google.com/p/jlibs/wiki/GarbageCollection
We have some legacy code with Groovy, and we want to remove Groovy from the application, so, we need to get the java source code generated after using the gmaven plug-in.
Basically, in other words I am dynamically generating new classes (using gmaven Groovy maven plug in) and I would like to be able to obtain the java source code of such generated classes.
I researched a little bit and can see that the only goals for this plug in are
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
I can't see any goal that allows you to obtain the fully implemented java source code, the stub code is not enough for us as we need the final implementation source code in order to get rid of Groovy.
I'm not very familiar with the gmaven plugin, but I assume it compiles the groovy code into byte code. In this case, you can use a byte code decompiler, there is a nice list here. In the past I've used JAD and it was quite nice. The best ones will also try to create meaningful variable names based on class names.
One warning though - Groovy objects are derived from GObject, not java.lang.Object, so you would probably need to keep the groovy jar until the groovy->java porting is done. Also, be prepared that it won't be a very easy to read java...
It may be out of your scope (1 year old) but I fought against the same problem and found a method to retrieve the algorithm (not the java source code) from the decompiled groovy classes.
You may want to take a look : http://michael.laffargue.fr/blog/2013/11/02/decompiling-groovy-made-classes/
The generated stubs will be useless for you. They are just what their names suggests: stubs.
The stubs are only useful when doing joint java/groovy compilation. That's because there are two compilers involved in a java/groovy mixed project.
Parse groovy
Create stubs
Compile java and stubs (using javac)
Continue groovy compilation (using groovyc)
The groovy code will be compiled using groovyc compiler and the result is byte code.
This is an example of a generated stub:
package maba.groovy;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
import groovy.lang.*;
import groovy.util.*;
#groovy.util.logging.Log4j() public class Order
extends java.lang.Object implements
groovy.lang.GroovyObject {
public groovy.lang.MetaClass getMetaClass() { return (groovy.lang.MetaClass)null;}
public void setMetaClass(groovy.lang.MetaClass mc) { }
public java.lang.Object invokeMethod(java.lang.String method, java.lang.Object arguments) { return null;}
public java.lang.Object getProperty(java.lang.String property) { return null;}
public void setProperty(java.lang.String property, java.lang.Object value) { }
public int getPrice() { return (int)0;}
public void setPrice(int value) { }
public int getQuantity() { return (int)0;}
public void setQuantity(int value) { }
#java.lang.Override() public java.lang.String toString() { return (java.lang.String)null;}
}
As you can see there is nothing useful. And you will still depend on some groovy libraries.
This question has been on the mailing-list some time ago [0]. To summarize: Groovy to Java is hard to achieve since there are language constructs and APIs (if you do want to totally remove the Groovy dependency) that are not available in Java.
Especially with the introduction of call-site caching and other performance optimizing techniques the generated Java code would look a lot like this (for the matter of simplicity I just threw some script into JD-GUI [1]):
public class script1351632333660 extends Script
{
public script1351632333660()
{
script1351632333660 this;
CallSite[] arrayOfCallSite = $getCallSiteArray();
}
public script1351632333660(Binding arg1)
{
Binding context;
CallSite[] arrayOfCallSite = $getCallSiteArray();
ScriptBytecodeAdapter.invokeMethodOnSuperN($get$$class$groovy$lang$Script(), this, "setBinding", new Object[] { context });
}
public Object run()
{
CallSite[] arrayOfCallSite = $getCallSiteArray(); Object items = ScriptBytecodeAdapter.createList(new Object[0]);
Object[] item = (Object[])ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.createList(new Object[] { "Fluff", arrayOfCallSite[1].callConstructor($get$$class$java$util$Date()), (Integer)DefaultTypeTransformation.box(11235813) }), $get$array$$class$java$lang$Object());
arrayOfCallSite[2].call(items, item);
arrayOfCallSite[3].callCurrent(this, items);
ValueRecorder localValueRecorder = new ValueRecorder();
try
{
Object tmp102_101 = items; localValueRecorder.record(tmp102_101, 8);
Object tmp126_121 = arrayOfCallSite[4].call(tmp102_101, new script1351632333660._run_closure1(this)); localValueRecorder.record(tmp126_121, 14); if (DefaultTypeTransformation.booleanUnbox(tmp126_121)) localValueRecorder.clear(); else ScriptBytecodeAdapter.assertFailed(AssertionRenderer.render("assert items.findAll { it }", localValueRecorder), null); } finally {
localValueRecorder.clear(); throw finally; } return null; return null; }
static { __$swapInit();
Long localLong1 = (Long)DefaultTypeTransformation.box(0L);
__timeStamp__239_neverHappen1351632333665 = localLong1.longValue();
Long localLong2 = (Long)DefaultTypeTransformation.box(1351632333665L);
__timeStamp = localLong2.longValue(); }
class _run_closure1 extends Closure implements GeneratedClosure { public _run_closure1(Object _thisObject) { super(_thisObject); }
public Object doCall(Object it) { CallSite[] arrayOfCallSite = $getCallSiteArray(); return it; return null;
}
// ...
[0] http://groovy.329449.n5.nabble.com/Java-lt-gt-Groovy-converters-td337442.html
[1] http://java.decompiler.free.fr