Skip to content

ImFl0wow/unsafe

This branch is up to date with sniffy/unsafe:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ac72c33 · Mar 22, 2023
Mar 14, 2023
Feb 27, 2023
Dec 6, 2022
Mar 22, 2023
Mar 6, 2023
Mar 22, 2023
Mar 7, 2023
Mar 4, 2023
Jan 15, 2023
Jan 19, 2023
Jan 15, 2023
Jan 19, 2023
Jan 15, 2023
Jan 19, 2023
Jan 20, 2023
Jan 19, 2023
Jan 19, 2023
Jan 13, 2023
Jan 19, 2023
Dec 6, 2022
Jan 19, 2023
Jan 2, 2023
Dec 1, 2022
Jan 31, 2023
Feb 27, 2023
Feb 28, 2023
Feb 27, 2023
Feb 27, 2023
Mar 14, 2023

Repository files navigation

unsafe

Vision

  • many apps rely on Unsafe to deal with final or private fields via reflection in a ninja style
  • This is wrong and must be addressed properly (raising incididents for librarty maintainers to open their API)
  • When not possible it must be dealt with using instrumentation
  • unsafe tools provide API which is firendly to static analysis
  • unsafe tools provide a way forward to migrate to new JDK APIS (code evolution)
  • unsafe tools provide an extremely compatible API (HotSport 5-21, J9, Android, GraalVM native images)
  • unsafe tools provide a helper to run instrumentation

Links

org.mockito.internal.util.reflection.InstrumentationMemberAccessor

TODO

Duck Typing and mimicking get parameter names

GraalVM

https://docs.oracle.com/en/graalvm/enterprise/20/docs/reference-manual/native-image/Reflection/

Structure

Naming conventions

tools.unsafe.reflection.method.voidresult.oneparam.resolved.ResolvedVoidDynamicOneParamMethodRef<C, P1> implements GenericVoidDynamicOneParamMethodRef<C, P1>

voidresult.oneparam.resolved.ResolvedVoidDynamicOneParamMethodRef voidresult - result (voidresult / typedresult / genericresult) oneparam - arguments (oneparam, twoparams, threeparams, fourparams, multipleparams) resolved - resolved / unresolved

ResolvedVoidDynamicOneParamMethodRef resolved - resolved / unresolved void - void / typedresult / genericresult

ResolvedInstanceVoidOneParamMethodRef resolved - resolved / unresolved instance - instance / static / dynamic void - void / types / generic oneparam - oneparam, twoparams, threeparams, fourparams, multipleparams

Class names

Instance* - non-static members (fields and methods) with a reference to particular object Static* - static members (fields and methods) NonStatic* - non-static members (fields and methods) without a reference to particular object; require passing an object when working with them

Unresolved* - unresolved references which might (or might not) throw an UnresolvedRefException when working with them in case say class or methog were not found

Fluent API

function $ does the magic depending on context - i.e. creates ClassRef or ObjectRef instances for given Class, class name or arbitrary object, or creates a new ObjectRef instance for the given object and field name.

It is not the most stongly types API but might be

class Bar {
    private final int baz;

    private Bar(int baz) {
        this.baz = baz;
    }
}

class Foo {
    private final static Bar STATIC_BAR_REFERENCE = new Bar(7);
    private final Bar bar;

    private Foo(int baz) {
        bar = new Bar(baz);
    }
}

    Foo foo = new Foo(42);

    $(foo).$("bar").$("baz").compareAndSet(42,13);
        $(Foo.class).field("STATIC_BAR_REFERENCE").set(3);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 97.0%
  • Kotlin 2.5%
  • C# 0.5%