SkillAgentSearch skills...

OneLineReflection

Simple reflection utility for string->value (Java)

Install / Use

/learn @aramperes/OneLineReflection
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

OneLineReflection

Simple reflection utility for string->value.

Example

See the code example for a ready-to-run sample.

Usage

In this scenario, you have an initialized object, we'll call it the context.

In the context's class, Human, you have a method, public String getName(), which returns the value of the field you initialized with the object.

Getting the value from getName() using the utility would be:


Human human = new Human("Bob");
String name = (String) new ReflectionProcessor("$.getName()", human).process(); // Returns "Bob"

Note that $ represents the context. You can also use this to access it.


In another scenario, you want to execute a static method. No context is needed here.

In our com.example.foo.MyClass class, we have a static method, public static void doSomething().

Invoking the method using the utility would be:


new ReflectionProcessor("com.example.foo.MyClass.doSomething()").process();


If you want to use multiple variables in your string, this utility supports multi-contextuality.

For example, if you have two Strings:


String a = "Hello";
String b = "World";

And you have a static method in MyClass, public static void hello(String a, String b);

You can pass the parameters as such:


new ReflectionProcessor("com.example.foo.MyClass.hello($1, $2)", a, b).process();

Note that $1 references the first context, and $2 the second. Using $ alone will reference the first context.

Features

  • Object-oriented calls
  • Static calls
  • Fields, methods (with parameters), classes (with package), literals (String, Boolean, Integer, Long)
  • Multi-contextual

Disclaimer

This is for demonstration purposes, some features you could expect might or might not be present.

Features not included:

  • Decimal literals (Double, Float)
  • Short, Byte literals
  • Constructors

Hello.

Related Skills

View on GitHub
GitHub Stars7
CategoryDevelopment
Updated7y ago
Forks2

Languages

Java

Security Score

70/100

Audited on Dec 28, 2018

No findings