Class ClassDescriptor

java.lang.Object
org.htmlunit.corejs.javascript.ClassDescriptor

public class ClassDescriptor extends Object
A descriptor for a constructor and its prototype methods which can then be used to build a real constructor function and prototype during standard object initialisation. This should be built by creating a ClassDescriptor.Builder and using the methods there to declare the required methods. This should normally be done once during static initialisation of a class and then utilised when initialising within a scope.

For example:

 final class NativeBoolean extends ScriptableObject {
   private static final String CLASS_NAME = "Boolean";

   private static final ClassDescriptor DESCRIPTOR;

   static {
       DESCRIPTOR =
               new ClassDescriptor.Builder(
                               CLASS_NAME,
                               1,
                               NativeBoolean::js_constructorFunc,
                               NativeBoolean::js_constructor)
                       .withMethod(PROTO, "toString", 0, NativeBoolean::js_toString)
                       .withMethod(PROTO, "toSource", 0, NativeBoolean::js_toSource)
                       .withMethod(PROTO, "valueOf", 0, NativeBoolean::js_valueOf)
                       .build();

   static void init(VarScope scope, boolean sealed) {
       // Boolean is an unusual object in that the prototype is itself a Boolean
       var constructor = DESCRIPTOR.buildConstructor(scope, new NativeBoolean(false), sealed);
   }
 
Properties can be defined using the ClassDescriptor.Builder.withProp(Destination, String, LambdaGetterFunction, LambdaSetterFunction, int) and the others with similar names. These either take lambda getter and setter functions which will be converted into function objects at the point the context is initialised, or take ClassDescriptor.ValueCreators which can populate a value descriptor.

Convenience methods are provided to create aliases of properties (ClassDescriptor.Builder.aliss(String)) and simple values which are truly independent of the context (ClassDescriptor.Builder.value(Object)), along with variants that also accept attribute flags. More complex cases can be handled by methods similar to NativeArray.makeUnscopables(Context, VarScope, ScriptableObject).