View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import java.io.Serializable;
18  
19  import org.htmlunit.corejs.javascript.Delegator;
20  import org.htmlunit.corejs.javascript.Scriptable;
21  
22  /**
23   * Proxy for a {@link HtmlUnitScriptable}.
24   *
25   * @param <T> the type of scriptable object being wrapped
26   * @author Marc Guillemot
27   * @author Daniel Gredler
28   * @author Ronald Brill
29   */
30  public abstract class HtmlUnitScriptableProxy<T extends HtmlUnitScriptable> extends Delegator
31          implements Serializable {
32  
33      /**
34       * {@inheritDoc}
35       */
36      @Override
37      public Object get(final int index, final Scriptable start) {
38          return getDelegee().get(index, unwrap(start));
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public Object get(final String name, final Scriptable start) {
46          return getDelegee().get(name, unwrap(start));
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public boolean has(final int index, final Scriptable start) {
54          return getDelegee().has(index, unwrap(start));
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public boolean has(final String name, final Scriptable start) {
62          return getDelegee().has(name, unwrap(start));
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public boolean hasInstance(final Scriptable instance) {
70          return getDelegee().hasInstance(unwrap(instance));
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public void put(final int index, final Scriptable start, final Object value) {
78          getDelegee().put(index, unwrap(start), value);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void put(final String name, final Scriptable start, final Object value) {
86          getDelegee().put(name, unwrap(start), value);
87      }
88  
89      /**
90       * Delegates call to delegee.
91       * @param hint the type hint
92       * @return the default value
93       *
94       * @see org.htmlunit.corejs.javascript.Scriptable#getDefaultValue
95       */
96      @Override
97      public Object getDefaultValue(final Class<?> hint) {
98          return getDelegee().getDefaultValue(hint);
99      }
100 
101     /**
102      * Unwraps the given scriptable if it is a proxy.
103      *
104      * @param scriptable the scriptable to unwrap
105      * @return the delegee if the given scriptable is a proxy; the scriptable itself otherwise
106      */
107     private static Scriptable unwrap(final Scriptable scriptable) {
108         if (scriptable instanceof HtmlUnitScriptableProxy<?> proxy) {
109             return proxy.getDelegee();
110         }
111         return scriptable;
112     }
113 }