Aside from recompiling rt.jar is there any way I can replace the currentTimeMillis() call with one of my own?
1# The right way to do it is use a Clock object and abstract time.
I know it but we'll be running code developed by an endless number of developers that have not implemented Clock or have made an implementation of their own.
2# Use a mock tool like JMockit to mock that class.
Even though that only works with Hotspot disabled -Xint and we have success using the code bellow it does not "persist" on external libraries. Meaning that you'd have to Mock it everywhere which, as the code is out of our control, is not feasible. All code under main() does return 0 milis (as from the example) but a new DateTime() will return the actual system millis.
#MockClass(realClass = System.class)
public class SystemMock extends MockUp<System> {
// returns 1970-01-01
#Mock public static long currentTimeMillis() { return 0; }
}
3# Re-declare System on start up by using -Xbootclasspath/p (edited)
While possible, and though you can create/alter methods, the one in question is declared as public static native long currentTimeMillis();. You cannot change it's declaration without digging into Sun's proprietary and native code which would make this an exercise of reverse engineering and hardly a stable approach.
All recent SUN JVM crash with the following error:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000, pid=4668, tid=5736
4# Use a custom ClassLoader (new test as suggested on the comments)
While trivial to replace the system CL using -Djava.system.class.loader JVM actually loads up the custom classLoader resorting to the default classLoader and System is not even pushed trough the custom CL.
public class SimpleClassLoader extends ClassLoader {
public SimpleClassLoader(ClassLoader classLoader) {
super(classLoader);
}
#Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
}
We can see that java.lang.System is loaded from rt.jar using java -verbose:class
Line 15: [Loaded java.lang.System from C:\jdk1.7.0_25\jre\lib\rt.jar]
I'm running out of options.
Is there some approach I'm missing?
You could use an AspectJ compiler/weaver to compile/weave the problematic user code, replacing the calls to java.lang.System.currentTimeMillis() with your own code. The following aspect will just do that:
public aspect CurrentTimeInMillisMethodCallChanger {
long around():
call(public static native long java.lang.System.currentTimeMillis())
&& within(user.code.base.pckg.*) {
return 0; //provide your own implementation returning a long
}
}
I'm not 100% sure if I oversee something here, but you can create your own System class like this:
public static class System {
static PrintStream err = System.err;
static InputStream in = System.in;
static PrintStream out = System.out;
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) {
System.arraycopy(src, srcPos, dest, destPos, length);
}
// ... and so on with all methods (currently 26) except `currentTimeMillis()`
static long currentTimeMillis() {
return 4711L; // Your application specific clock value
}
}
than import your own System class in every java file. Reorganize imports in Eclipse should do the trick.
And than all java files should use your applicatikon specific System class.
As I said, not a nice solution because you will need to maintain your System class whenever Java changes the original one. Also you must make sure, that always your class is used.
As discussed in the comments, it is possible that option #3 in the original question has actually worked, successfully replacing the default System class.
If that is true, then application code which calls currentTimeMillis() will be calling the replacement, as expected.
Perhaps unexpectedly, core classes like java.util.Timer would also get the replacement!
If all of the above are true, then the root cause of the crash could be the successful replacement of the System class.
To test, you could instead replace System with a copy that is functionally identical to the original to see if the crashes disappear.
Unfortunately, if this answer turns out to be correct, it would seem that we have a new question. :) It might go like this:
"How do you provide an altered System.currentTimeMillis() to application classes, but leave the default implementation in place for core classes?"
i've tried using javassist to remove the native currentTimeMills, add a pure java one and load it using bootclasspath/p, but i got the same exception access violation as you did. i believe that's probably because of the native method registerNatives that's called in the static block but it's really too much to disassemble the native library.
so, instead of changing the System.currentTimeMills, how about changing the user code? if the user code already compiled (you don't have source code), we can use tools like findbugs to identify the use of currentTimeMillis and reject the code (maybe we can even replace the call to currentTimeMills with your own implementation).
I am using Haxe for a game and compiling for the C++ target using HXCPP. I am trying to get the built-in profiler to work (cpp.vm.Profiler), but I cannot get it to create a dump file. My code is as simple as that :
if(Input.check(Key.P))
cpp.vm.Profiler.start("profiler.txt");
if(Input.check(Key.M))
cpp.vm.Profiler.stop();
I use HaxePunk for the input, and I assert that the profiler calls are indeed being executed (I made sure using a couple trace calls). I use defines HXCPP_STACK_TRACE and HXCPP_PROFILER for the compilation.
Am I doing anything wrong, or missing anything ?
EDIT : here is some code that when compiled using haxe -D HXCPP_PROFILER -D HXCPP_STACK_TRACE -main Main -cpp test, doesn't actually create any noticeable "profiler.txt" file :
class Main
{
static public function main()
{
var bleh = haxe.Timer.stamp();
cpp.vm.Profiler.start("profiler.txt");
while(haxe.Timer.stamp() - bleh < 5.)
{
// Do something I guess
Math.cos(haxe.Timer.stamp());
}
cpp.vm.Profiler.stop();
}
}
Relevant bug report to hxcpp: #580.
Apparently this was fixed on 17 May 2017 in this commit. The fix should be in the next hxcpp version after 3.4.64.
I need to apply a lot of functions to the same piece of data in arbitrary order. Different people add different functions. I have created a system, that, after simplification, looks like that:
abstract_filter.h
class AbstractFilter {
void filter(data) = 0;
}
blue_filter.h
class BlueFilter: public AbstractFilter ...
red_filter.h ...
green_filter.h ...
parser.cpp
#include "blue_filter.h"
#include "red_filter.h" //so on
void Parse(const Data data) {
RedFilter redFilter();
redFilter.filter(data);
BlueFilter blueFilter();
blueFilter.filter(data);
....
}
I have hundreds of filters and people always forget to add them to the list or configure. Is it possible to write something like "take all classes from that group/folder and instantiate and put in array...."? I can't make them static or register filters in their constructors because several filtering stacks can be active in the same time.
All I want is to not have to manually enumerate all filters. Would be great to put them in place by just adding them to the project.
Write a python script which parses the filter directory entries an adds them to a generic factory or processing class. If the script runs on every build process the filters will always be taken care of.
What is the most common use sturdy nugetpackage/library/framework for C++ that does pretty much the same as the C# library by Microsoft called Unity: http://unity.codeplex.com/ ?
The end result would look something like this:
class IProjectRepository
{
public:
vector<Project> GetProjects() = 0;
}
class XMLProjectRepository : IProjectRepository
{
public:
vector<Project> GetProjects()
{
return // implementation
}
}
class Application
{
public:
Application( shared_ptr<IProjectRepository> projectRepository )
// Or however this would look...
// projectRepository would be an instance of whatever i have registered (see below)
{
auto projects = this.projectRepository.GetProjects();
}
}
And in some startup area i would bind like this:
BinderFramework::Register<IProjectRepository, XMLProjectRepository>();What is the most common use sturdy nuget package/library for C++ that does pretty much the same as the C# library by Microsoft called Unity: http://unity.codeplex.com/
I have been looking for a while now, but can't quite find a nice complete and simple solution.
The idea is to use a well known framework rather than write my own flaky code.
I might be a bit late but you should give a look at Hypodermic.
It's header only, it comes with a pretty cool Dsl plus it's been used for years in production environments.
I think this would increase the quality of life when devving, but google came up with nothing and I couldn't find anything specific inside inside Netbeans either.
What I want is to start with this header:
class bla
{
public:
static void gfg(somearg asd);
};
Then I open the blank bla.cpp and pressed 'autoimplement'. After that, it would look like this:
#include "bla.h"
static void bla::gfg(somearg asd)
{
//TODO: implement
throw unimplemented("void bla::gfg(somearg) is unimplemented");
}
Anyone know of a tool like this?
I found http://www.radwin.org/michael/projects/stubgen/
"stubgen is a C++ development tool that keeps code files in sync with their associated headers. When it finds a member function declaration in a header file that doesn't have a corresponding implementation, it creates an empty skeleton with descriptive comment headers."
This looks like it does exactly what you want it to do.
Some time has passed and in the meantime the requested feature seems to have been implemented in netbeans. Refer to https://netbeans.org/bugzilla/show_bug.cgi?id=213811 , which also gives a description on how to use it:
Note:
Implemented CTRL+SPACE.
IDE suggest implementing of class method if CTRL+SPACE was pressed:
- inside file that already has at least one method definition
- between method declarations