net.java.html.lib.Objs Maven / Gradle / Ivy
package net.java.html.lib;
import java.lang.reflect.Type;
/*
* #%L
* ES6 Core Library - a library from the DukeScript project.
* Visit http://dukescript.com for support and commercial license.
* %%
* Copyright (C) 2015 - 2016 Dukehoff GmbH
* %%
* 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.
* #L%
*/
public class Objs extends java.lang.Object {
private final java.lang.Object js;
private static final Constructor $AS = new Constructor(Objs.class) {
@Override
protected Objs create(Object any) {
return $as(any);
}
};
/** Constructor for auto-generated subclasses. From client code
* use {@link #$as(java.lang.Object)} or {@link #$as(java.lang.Class, java.lang.Object)}.
*
* @param constructor constructor {@link Constructor#Constructor(java.lang.Class) associated with this type}
* @param js any raw object to be wrapped by this instance
*/
protected Objs(Constructor> constructor, java.lang.Object js) {
this(constructor, js, (Type[]) null);
}
/**
* Constructor for auto-generated subclasses with type parameter specification.
* From client code use {@link #$as(java.lang.Object)} or
* {@link #$as(java.lang.Class, java.lang.Object)}.
*
* @param constructor constructor
* {@link Constructor#Constructor(java.lang.Class) associated with this type}
* @param js any raw object to be wrapped by this instance
* @param typeParameters {@code null} or suggested type parameters
* @since 0.20
*/
protected Objs(Constructor> constructor, java.lang.Object js, Type... typeParameters) {
assert constructor.clazz.isInstance(this);
this.js = $js(js);
}
Objs(Constructor> constructor) {
assert constructor.clazz.isInstance(this);
this.js = createJS();
}
/** Casts given object to this class.
*
* @param obj any object
* @return a view of the provided obj
object
*/
public static Objs $as(java.lang.Object obj) {
return new Objs($AS, obj);
}
/** Casts given object to requested class.
*
* @param the type of requested object
* @param clazz identification of the type
* @param obj object to be converted
* @return a view of the provided obj
object
* @throws ClassCastException if the obj
cannot be cast to
* the requested class
*/
public static T $as(java.lang.Class clazz, java.lang.Object obj) {
Class extends T> castTo = clazz;
if (clazz == java.lang.Object.class) {
if (
obj instanceof java.lang.Number ||
obj instanceof java.lang.String ||
obj instanceof java.lang.Character ||
obj instanceof java.lang.Boolean
) {
return clazz.cast(obj);
} else {
castTo = Objs.class.asSubclass(clazz);
}
}
return $asRaw(castTo, obj);
}
static T $asRaw(java.lang.Class extends T> castTo, java.lang.Object obj) {
if (castTo.isInstance(obj)) {
return castTo.cast(obj);
}
obj = $js(obj);
Constructor> c = Constructor.find(castTo);
if (c != null) {
obj = c.create(obj);
}
return castTo.cast(obj);
}
static java.lang.Object $as(String clazz, java.lang.Object obj) {
try {
return $as(Class.forName(clazz), obj);
} catch (ClassNotFoundException ex) {
obj = $js(obj);
Constructor> c = Constructor.find(clazz);
if (c != null) {
obj = c.create(obj);
}
return obj;
}
}
/**
* Unwraps the object into plain JavaScript one.
*
* @param obj an object to unwrap - either subclass of Objs
or
* plain JavaScript object
* @return the raw JavaScript object hidden behind the obj
one
*/
public static java.lang.Object $js(java.lang.Object obj) {
if (obj instanceof Objs) {
Objs objs = (Objs) obj;
return objs.js;
}
if(obj instanceof Object[]) {
Object[] orig = (Object[]) obj;
Object[] copy = null;
for (int i = 0; i < orig.length; i++) {
Object at = $js(orig[i]);
if (at != orig[i]) {
if (copy == null) {
copy = orig.clone();
}
copy[i] = at;
}
}
return copy == null ? orig : copy;
}
return net.java.html.lib.Function.specialJs(obj);
}
/** "Casts" this object to some other one. Creates
* a wrapper for the JavaScript object represented by this
* instance, so one can view it as different object.
*
* @param type one requests
* @param clazz requested class
* @return instance of the requested type
* @throws ClassCastException if cast cannot be done
*/
public T $cast(Class clazz) {
return $as(clazz, this);
}
public java.lang.Object $get(java.lang.String n) {
return getRaw($js(this), /* AnyKeyword*/$js(n));
}
@net.java.html.js.JavaScriptBody(args={"self", "n"}, body=
"return self[n];\n"
)
static native java.lang.Object getRaw(java.lang.Object self, java.lang.Object n);
/**
* Sets a raw property on this JavaScript object. The $set
* method returns this
which can be useful for chaining
* multiple calls:
*
* Objs js = new Objs().
* $set("firstName", "John").
* $set("lastName", "Cleese");
*
*
* @param k the name of the property
* @param n the value of the property
* @return this object
*/
public Objs $set(java.lang.String k,java.lang.Object n) {
setRaw($js(this), $js(k), $js(n));
return this;
}
@net.java.html.js.JavaScriptBody(args={"self", "k", "n"}, wait4js = false, body=
"self[k] = n;\n"
)
static native void setRaw(java.lang.Object self, java.lang.Object k, java.lang.Object n);
/** Removes a property for this JavaScript object.
*
* @param k the name of property to remove
*/
public Objs $delete(java.lang.String k) {
deleteRaw($js(this), k);
return this;
}
@net.java.html.js.JavaScriptBody(args={"self", "k"}, wait4js = false, body=
"delete self[k];\n"
)
private static native void deleteRaw(java.lang.Object self, java.lang.Object k);
/**
* Determines whether an object has a property with the specified name.
* @param v A property name.
*/
public java.lang.Boolean hasOwnProperty(java.lang.String v) {
return CoreTypes.hasOwnProperty$487($js(this), v);
}
/**
* Determines whether an object exists in another object's prototype chain.
* @param v Another object whose prototype chain is to be checked.
*/
public java.lang.Boolean isPrototypeOf(java.lang.Object v) {
return CoreTypes.isPrototypeOf$488($js(this), /* FirstTypeNode*/$js(v));
}
/**
* Determines whether a specified property is enumerable.
* @param v A property name.
*/
public java.lang.Boolean propertyIsEnumerable(java.lang.String v) {
return CoreTypes.propertyIsEnumerable$489($js(this), v);
}
/** Returns a date converted to a string using the current locale. */
public java.lang.String toLocaleString() {
return CoreTypes.toLocaleString$490($js(this));
}
/** Returns a string representation of an object. */
public java.lang.String toString() {
final Object objs = $js(this);
try {
return CoreTypes.toString$491(objs);
} catch (IllegalStateException ex) {
return getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(objs));
}
}
/** Returns the primitive value of the specified object. */
public java.lang.Object valueOf() {
return (java.lang.Object)CoreTypes.valueOf$492($js(this));
}
// constructor ObjectConstructor
/** A reference to the prototype for a class of objects. */
public static java.lang.Object prototype;
/**
* Returns the prototype of an object.
* @param o The object that references the prototype.
*/
public static java.lang.Object getPrototypeOf(java.lang.Object o) {
return CoreTypes.getPrototypeOf$493(/* AnyKeyword*/$js(o));
}
/**
* Gets the own property descriptor of the specified object.
* An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype.
* @param o Object that contains the property.
* @param p Name of the property.
*/
public static PropertyDescriptor getOwnPropertyDescriptor(java.lang.Object o, java.lang.String p) {
return PropertyDescriptor.$as(CoreTypes.getOwnPropertyDescriptor$494(/* AnyKeyword*/$js(o), p));
}
/**
* Returns the names of the own properties of an object. The own properties of an object are those that are defined directly
* on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions.
* @param o Object that contains the own properties.
*/
public static java.lang.String[] getOwnPropertyNames(java.lang.Object o) {
Object[] arr = CoreTypes.getOwnPropertyNames$495(/* AnyKeyword*/$js(o));
String[] res = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i].toString();
}
return res;
}
/**
* Creates an object that has the specified prototype, and that optionally contains specified properties.
* @param o Object to use as a prototype. May be null
* @param properties JavaScript object that contains one or more property descriptors.
*/
public static java.lang.Object create(java.lang.Object o, PropertyDescriptorMap properties) {
return CoreTypes.create$496(/* AnyKeyword*/$js(o), /* FirstTypeNode*/$js(properties));
}
/**
* Creates an object that has the specified prototype, and that optionally contains specified properties.
* @param o Object to use as a prototype. May be null
* @param properties JavaScript object that contains one or more property descriptors.
*/
public static java.lang.Object create(java.lang.Object o) {
return CoreTypes.create$497(/* AnyKeyword*/$js(o));
}
/**
* Adds a property to an object, or modifies attributes of an existing property.
* @param o Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.
* @param p The property name.
* @param attributes Descriptor for the property. It can be for a data property or an accessor property.
*/
public static java.lang.Object defineProperty(java.lang.Object o, java.lang.String p, PropertyDescriptor attributes) {
return CoreTypes.defineProperty$498(/* AnyKeyword*/$js(o), p, /* FirstTypeNode*/$js(attributes));
}
/**
* Adds one or more properties to an object, and/or modifies attributes of existing properties.
* @param o Object on which to add or modify the properties. This can be a native JavaScript object or a DOM object.
* @param properties JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property.
*/
public static java.lang.Object defineProperties(java.lang.Object o, PropertyDescriptorMap properties) {
return CoreTypes.defineProperties$499(/* AnyKeyword*/$js(o), /* FirstTypeNode*/$js(properties));
}
/**
* Prevents the modification of attributes of existing properties, and prevents the addition of new properties.
* @param o Object on which to lock the attributes.
*/
public static T seal(T o) {
return (T)CoreTypes.seal$500(/* FirstTypeNode*/$js(o));
}
/**
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
* @param o Object on which to lock the attributes.
*/
public static T freeze(T o) {
return (T)CoreTypes.freeze$501(/* FirstTypeNode*/$js(o));
}
/**
* Prevents the addition of new properties to an object.
* @param o Object to make non-extensible.
*/
public static T preventExtensions(T o) {
return (T)CoreTypes.preventExtensions$502(/* FirstTypeNode*/$js(o));
}
/**
* Returns true if existing property attributes cannot be modified in an object and new properties cannot be added to the object.
* @param o Object to test.
*/
public static java.lang.Boolean isSealed(java.lang.Object o) {
return CoreTypes.isSealed$503(/* AnyKeyword*/$js(o));
}
/**
* Returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.
* @param o Object to test.
*/
public static java.lang.Boolean isFrozen(java.lang.Object o) {
return CoreTypes.isFrozen$504(/* AnyKeyword*/$js(o));
}
/**
* Returns a value that indicates whether new properties can be added to an object.
* @param o Object to test.
*/
public static java.lang.Boolean isExtensible(java.lang.Object o) {
return CoreTypes.isExtensible$505(/* AnyKeyword*/$js(o));
}
/**
* Returns the names of the enumerable properties and methods of an object.
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
public static Array keys(java.lang.Object o) {
return (Array)Array.$as(CoreTypes.keys$506(/* AnyKeyword*/$js(o)));
}
public Objs(java.lang.Object value) {
this($AS, CoreTypes.new$507(/* AnyKeyword*/$js(value)));
}
public Objs() {
this($AS, CoreTypes.new$508());
}
public static java.lang.Object newObject() {
return CoreTypes.newObject$509();
}
public static java.lang.Object newObject(java.lang.Object value) {
return CoreTypes.newObject$510(/* AnyKeyword*/$js(value));
}
Object createJS() {
throw new IllegalStateException();
}
/** Factory for one subclass of {@link Objs} wrapper. It is used
* from {@link Objs#$as(java.lang.Class, java.lang.Object)} method.
*
* @param type this constructor produces
*/
protected static abstract class Constructor {
private static Constructor> head;
final Class clazz;
final Constructor> next;
/** Registers the instance to the system.
*
* @param clazz subclass of {@link Objs} this constructor can handle
*/
protected Constructor(Class clazz) {
this.clazz = clazz;
this.next = head;
synchronized (Constructor.class) {
head = this;
}
}
/** Wraps any object into instance of associated {@link Objs} type.
*
* @param any object to be wrapped
* @return new instance of a wrapper
*/
protected abstract T create(Object any);
/** Wraps any object into instance of associated {@link Objs} type
* including suggested type parameters. Classes supporting type parameters
* are encouraged to implement this method. The default implementation
* just calls {@link #create(java.lang.Object)}.
*
* @param any object to be wrapped
* @param typeParameters optional type parameters or {@code null}
* @return new instance of a wrapper
* @since 0.20
*/
protected T create(Object any, Type... typeParameters) {
return create(any);
}
static Constructor> find(Class> clazz) {
if (String.class == clazz || clazz.isPrimitive()) {
return null;
}
if (Union.class.isAssignableFrom(clazz)) {
clazz = Union.class;
} else if (Function.A5.class.isAssignableFrom(clazz)) {
clazz = Function.class;
}
for (int i = 0; i <= 2; i++) {
Constructor> c = head;
for (;;) {
if (c == null) {
break;
}
if (c.clazz == clazz) {
return c;
}
c = c.next;
}
initializeClass(clazz);
}
return null;
}
static Constructor> find(String clazz) {
if (clazz.startsWith(Union.class.getName())) {
return find(Union.class);
} else if (clazz.startsWith(Function.class.getName())) {
return find(Function.class);
}
Constructor> c = head;
for (;;) {
if (c == null) {
break;
}
if (c.clazz.getName().equals(clazz)) {
return c;
}
c = c.next;
}
return null;
}
private static void initializeClass(Class> c) {
try {
// classical way to initialze a class
Class.forName(c.getName(), true, c.getClassLoader());
} catch (Throwable ex) {
try {
// most forceful way
c.newInstance();
} catch (Exception ex1) {
// We can't do more
}
}
}
}
/** Represents a property of an {@link Objs} object.
*
* @param the type of the property
*/
public static final class Property {
private final Objs js;
private final Class type;
private final String property;
private /* Constructor> | this */ Object constructor;
private Property(Objs objs, Class type, java.lang.String property) {
this.js = objs;
this.type = type;
this.property = property;
}
/** Creates new property for the object.
*
* @param type of the property
* @param obj the object the property belongs to
* @param propertyType type of the property
* @param property name of the property
* @return property instances {@link #get() to use} later
*/
public static Property create(Objs obj, Class extends Object> propertyType, java.lang.String property) {
return new Property(obj, propertyType, property);
}
/** Get value of the property.
*
* @return the value
*/
public T get() {
Object raw = getRaw(Objs.$js(js), property);
raw = applyConstructor(raw);
return type.cast(raw);
}
/** Sets value of the property.
*
* @param value the value to set
*/
public void set(T value) {
Objs.setRaw(Objs.$js(js), property, Objs.$js(value));
}
private Object applyConstructor(Object raw) {
if (raw == null || constructor == this) {
return raw;
}
Constructor> c;
if (constructor == null) {
c = Constructor.find(type);
if (c == null) {
// no constructor found
constructor = this;
return raw;
}
constructor = c;
} else {
c = (Constructor>) constructor;
}
return c.create(raw);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy