Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/christoph-jerolimov/java-hardener

Makes java fault tolerant against NPEs!
https://github.com/christoph-jerolimov/java-hardener

Last synced: 28 days ago
JSON representation

Makes java fault tolerant against NPEs!

Awesome Lists containing this project

README

        

Fachhochschule Köln Campus Gummersbach

Fakultät für Informatik und Ingenieurwissenschaften

This was an academic project developed for the
[Compiler and Interpreter](http://www.gm.fh-koeln.de/ehses/compiler/)
elective subject provided by
[Prof. Dr. Ehses](http://www.gm.fh-koeln.de/ehses/).

## The target

The "java-hardener" (all projects need a name, right?) should make
java-bytecode (a little bit more) fault tolerant against `NullPointerExceptions`.

It's inspired by [Objective-C](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/) which ignores signals (method calls) to nil-Objects.

**An example?**

List aList = null;

System.out.println("List size: " + aList.size());
// No NPE! This line will output: "List size: 0"

if (!aList.isEmpty()) {
// No NPE! Will run the code inside the condituion! }

With this project this code runs without an exception! ✔︎

**How it works?**

It's based on the bytecode manipulation framework [ASM 4](http://asm.ow2.org/).
You can run the hardening mechanism over your compiled class-files
(with a small bytecode to bytecode post-processor) or integrate it as `ClassLoader` which modifies your code on-the-fly when it was loaded.

## Academic-project disclaimer (or „this is fun only“)

> Since this is a academic project it's focusud on the possibility of
> byte-code manipulation instead of creating a commercial-prooven software.
> If you start programming and have to many NullPointerExceptions you may be
> intressting in this. But the best way to fix such issues is understanding
> the general problem and improve your code. :-D

Btw: If your opinion this is useful in some situations ... mail me ... :-)

## Getting started

### Download the dependencies

Dependencies and configuration files for your IDE are not part of
the git repository. To download them use the maven plugin of your
IDE or one of these commands.

If your eclipse has no maven plugin you can generate the configuration
files also with maven itself:

mvn eclipse:clean eclipse:eclipse -DdownloadSources

To compile or package the project from the commandline:

mvn compile # download the dependencies and compile the sources
mvn package # compiles, test and package the java-hardener sources

See `pom.xml` for the dependency list/tree (only asm is needed at runtime).

### Testing it

When you opened the project with your IDE or run the maven package process
you can test java-hardener with the `src/test/java/Demo.java` class.

* To debug the result and show java assembler code use `JHPrint`.
* To compile the file (to stdout!!) use `JHCompile`.
* Use `JHRun` to run a programm with **activated NPE production**.

You can run this classes also from the commandline with this small wrapper scripts:

# Debug
./jhprint target/test-classes/Demo.class [or ./jhprint Demo]

# Compile and run (Without ASM or java-hardener dependency!)
./jhcompile target/test-classes/Demo.class > Demo.class
java -cp . Demo

# Run directly with JHClassLoader
./jhrun Demo

## Copyright

The MIT License (MIT)

Copyright (c) 2013 Christoph Jerolimov

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## Links

* [Objective-C programming guide](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/)
* [ASM](http://asm.ow2.org/)
* [ASM 4.0 javadoc](http://asm.ow2.org/asm40/javadoc/user/overview-summary.html)
* [ASM 4.0 Guide by Eric Bruneton](http://download.forge.objectweb.org/asm/asm4-guide.pdf) (pdf)
* [ASM Eclipse Plugin](http://asm.ow2.org/eclipse/index.html) (Doesn't work for me with the previous and the current version of Eclipse)