change the content of debug view in eclipse - java

I'm writing a java framework, for a class file, sample.class, it generates a proxy file sample_proxy.class. When sample.testMethod() is called, it excutes sample_proxy.class. I already make an eclipse plugin to make the breakpoint work,
If I start from Main.java, and make a breakpoint in sample.testMethod(), the stack below looks like: Main.main-->sample.proxy_method-->sample_proxy.testMethod.
Is there any way to ingore the proxy to show like: Main.main-->sample.testMethod?

What you want to do is possible but a bit more complicated. First of all there is no way to change the StackTrace of a running program. So Thread.currentThread().getStackTrace() is not the way to go.
I'm writing a java framework, for a class file, sample.class, it
generates a proxy file sample_proxy.class.
When you do that, you have to inline the called method, instead of simply calling it. That is non-trivial technique also being used by ProGuard. You will find that it does different kinds of inlining. Most interested you could be in the functionality of "inlining short methods".
I suggest you copy it from the code there. I good point to start would be http://sourceforge.net/p/proguard/code/ci/default/tree/src/proguard/optimize/Optimizer.java#l156
But be aware that this requires fundamental knowledge about the JVM itself, so there won't be a simple code snippet that does what you want, in the context you expect.
I hope it helps.

Related

Should a python file always include a class?

I have been coding in python for a couple months now, and something has always been on my mind. I know you can have classes in your .py file, but you don't have to. My question is, is it good practice to always have your code in a class, or is it not necessary?
FYI: I have been coding in Java for a few years, so I'm used to always having a "main" class and a main method that runs everything.
It depends on what your file is. In theory everything (saying this with some hesitation) can be written as a class. But it is a bit overkill to do that just for the sake of being "correct" and will probably make your code look strange rather than clear. In general i would make the following distinctions between cases
If it is the source for a big project which makes sense to be organized in an object oriented fashion, then you would have a class which defines exactly that. This is great because then you can inherit the class for variants or child projects.
If you are creating a list of utility functions to use for all your projects, such as array manipulations or little tools that are always handy, then a function-only file is the way to go
If you are writing a script which is designed in order to execute a specific task in the way a script would, then i would define task-specific source in a .py file and include the code related to the execution under the statement
if name == 'main':

Test if java-code does NOT compile

It might sound a little bit weird, but I am looking for a possibility to test if some statements in the code are rejected by the typechecker (which means that the code should NOT compile).
Be explain my intend: I am running a controlled experiment on type-systems where my subjects have to write some methods in java for me. The functionality of the methods written by the subjects can be easily tested using unit-tests, but I also want the methods to be well-typed (which means that some methodcalls should not be allowed).
One way I could imagine to achieve that would be writing the statements which should break the build into a seperate file, add it to the classpath and run javac to see if any error occurs during the build. Although this might work, it does not feel very sophisticated, so my question is: Is there any better way to (automatically) test if some statements are refected by the typechecker?
This is a variation of the Halting Problem, which isn't solvable in the general case. To do this, you have to run (or in this case compile) the code. Therefore, the solution you've already proposed is the best solution.
Have you looked at the Checker Framework? It can be used to static code analysis and more. It might be a good fit for what's you are doing. Here is the link on my answer with an example of the annotation type processor.
Also you may find the Java Compiler API quite helpful. It allows to execute javac programmatically in a single java machine. So you could use it as a part of your tests.

Plugin for the Android Project (Eclipse)

I'm a new person in this area (plugin developing) and I want to create some kind of plugin for my app:
I've developed an android application and now I need to make a toolkit for the students for future work on this app. The idea is:
1) to make a manual for that app, so that students can read about classes and structure not in separate .doc file but inside Eclipse IDE, probably with some links to the code.
2) to make a supervise of the app's functions (so that students can check if all features (performance-UI design, connection to the external server/API's, etc) of the app are working properly, in case if they will change something). All these data should be in separate frame (looks like a toolkit). (I found information related to this here http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-jfeature/section5.html, but I'm not sure if it's gonna work for my idea)
I will be glad to get some links of tutorials that are related to my task as well as your suggestions for the set of the toolkit features (but also with links how to make it).
Thank you very much in advance! Hope to get your help :)
The thing for the manual is Javadoc, you can use it in eclipse with java as well as in android.
here are some links, first:
http://en.wikipedia.org/wiki/Javadoc
Then I can quickly explain why javadoc can be useful for you, first of all it allows to create a real manual (java API are created with javadoc) with (in my opinion) the easiest way you can imagine.
You have to use a special comment tag that is:
/**
*
**/
When you put this before any declaration (methods, class, interfaces, fields etc) it will be included in the javadoc.
You have standard things that can be added for example you can specify a description of the method, what it #return what #params it need and many other things, being very careful and precise you can link javadoc with each other, and create very complex and precise documentation.
In eclipse javadoc is useful because eclipse itself allows the user to interact with javadoc by default. For example if you want to know what a method do, just simply hang the mouse over the name of the method and a little yellow dialog will appear. If you also use ctrl-space you can have some tips also in it. Pressing ctrl-space shows for example all avaible public methods, with javadoc for each method you have the yellow description dialog. If you are instantiating a new object you can see how many (and what kind of parameters they have) constructor are definited and so on, I think it is very useful and important.
For the second things if I understand what you want probably something like JUnit (a unit for the testing) can be ok for you.
http://www.junit.org
JUnit is a unit for testing the code, can be fully integrated with eclipse.
In few word, for each part of the code you should write one or more test to check if its behaviour is correct. Once you've written some test you can run them automatically with an user-friendly interface that tell you how many test are failed, how many passed and what kind of error there are.
Why is useful to test each little (stupid?) thing of my code?
Imagine you have a working code (your code).
Imagine you have someone working on it (your students).
How can a user be sure that any change he/she do it's ok with the existing code?
He/she should run the program and check each functionality one by one in order to find an error.
You understand that this is impossible. so JUnit do it for you with just one click (and if you want also in background).
So the student can add the code, and run the tests in order to see if the pre-existing code is still working.
The students can also write his own test to test automatically if all is ok. JUnit in facts allow you to test each part of the code without depending from the other, in this way, you can also test an internet connection without being connected to the net just "mocking" the connection.
I let this part without explanation because It is a long and complicated part. I gave you that "input" to stimulate you to read about testing and XP programming.
Ah, and welcome to stackoverflow! if you like this answer and think that answer correct to your question you can check it with the little check on the left.

How do I catch the read and writes in a java program?

I am trying to create a tool that can capture all the read and writes made by a java program. Also, I would like to know what fields of what object is access/modified.
I currently looked at:-
1) java.lang.instrument
I could not do much with that. I could not understand how to write an agent that can get access to the a running program and create a watch on different objects/fields and anything related. I would appreciated if you have any idea or information on that.
2) jvmti
I looked at jvmti and tried to create a jvmti tool, but I figured out that to get the objects, I would need the JVMTI_EVENT_OBJECT_ALLOC be a potential capability. But, I figured that, it is not. Moreover, I read that this event is not called for new command. Hence, at the moment, even this does not seem applicable.
So, I would like to know if you guys know any way to do what I want to do, either using the above mentioned methods or any other technique/tool that you may be aware of?
NOTE: I do not have access to the source code of the application. All, I have are the class files.
Check these out:
http://download.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
http://jamonapi.sourceforge.net/
http://www.manageengine.com/products/applications_manager/java-runtime-monitoring.html
It's very easy to do with the ASM lib. Create a new Class Loader that instruments all classes before loading them and use it for loading the target classes. Create a new MethodAdapter and override the visitFieldInsn method. Then look for the PUTFIELD, PUTSTATIC, GETFIELD and GETSTATIC opcodes. Although this might look scary (as my explation is most likely gibberish), it's in fact pretty easy. Just download the ASM manual and you'll know how to do it in no time.
Edit: I was forgetting to tell that in order to be able to intercept the reads and writes of done by the JDK code you have to instrument those classes, save them to files and run the JVM with a modified bootstrap classpath, through command line argument -Xbootclasspath (java.* and some other packages; I believe that at least sun.* and javax.* also need this).
This may also be doable with AspectJ... but I'm not sure.

Dynamic class creation in Java

I'm wondering if anybody knows a way to dynamically create a Java class, or more specifically a method in a Java class. I'm trying to do some unit testing, so I have code that I've already written, and I'm always modifying the code by adding extra System.out.println statements and then deleting them when I'm finished (otherwise the code gets too cluttered). What I'm trying to do is write a framework that can take a method, copy its code, add the System.out.println statements automatically, and then run the test on the copied method. This might also save time recompiling an entire Java application when I've only made a minor change to get some extra info while debugging.
Any ideas would be greatly appreciated.
You could just use a logging framework.
Sounds like you need aspects, i.e. AspectJ.
One option is to use cglib (Code Generation Library).
I don't have a specific example, but Aspect Oriented Programming (AOP) is what you want. Take a look at AspectJ.

Categories