1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.awt.Color;
  24 import java.awt.Component;
  25 import java.util.Enumeration;
  26 import javax.swing.JComponent;
  27 import javax.swing.SwingUtilities;
  28 import javax.swing.text.AttributeSet;
  29 import javax.swing.text.Document;
  30 import javax.swing.text.Element;
  31 import javax.swing.text.html.HTML;
  32 import javax.swing.text.html.ObjectView;
  33 /*
  34  * @test
  35  * @bug 8080972 8169887
  36  * @summary Audit Core Reflection in module java.desktop for places that will
  37  *          require changes to work with modules
  38  * @author Alexander Scherbatiy
  39  */
  40 
  41 public class TestObjectView {
  42 
  43     public static void main(String[] args) throws Exception {
  44         SwingUtilities.invokeAndWait(TestObjectView::testObjectView);
  45         System.setSecurityManager(new SecurityManager());
  46         SwingUtilities.invokeAndWait(TestObjectView::testObjectView);
  47     }
  48 
  49     private static void testObjectView() {
  50 
  51         UserObjectView objectView = new UserObjectView(new UserElement());
  52 
  53         Component comp = objectView.createComponent();
  54 
  55         if (!(comp instanceof UserJComponent)) {
  56             throw new RuntimeException("Component is not UserJComponent!");
  57         }
  58     }
  59     public static class UserJComponent extends JComponent {
  60 
  61         public static final Color USER_COLOR = new Color(10, 20, 30);
  62         public static final Color TEST_COLOR = new Color(15, 25, 35);
  63 
  64         Color color = USER_COLOR;
  65 
  66         public UserJComponent() {
  67 
  68         }
  69 
  70         public Color getUserColor() {
  71             System.out.println("[user component] get user color");
  72             return color;
  73         }
  74 
  75         public void setUserColor(Color color) {
  76             System.out.println("[user component] set user color");
  77             this.color = color;
  78         }
  79 
  80     }
  81 
  82     public static class UserObjectView extends ObjectView {
  83 
  84         public UserObjectView(Element elem) {
  85             super(elem);
  86         }
  87 
  88         @Override
  89         public Component createComponent() {
  90             return super.createComponent();
  91         }
  92     }
  93 
  94     public static class UserElement implements Element {
  95 
  96         @Override
  97         public Document getDocument() {
  98             throw new UnsupportedOperationException("Not supported yet.");
  99         }
 100 
 101         @Override
 102         public Element getParentElement() {
 103             throw new UnsupportedOperationException("Not supported yet.");
 104         }
 105 
 106         @Override
 107         public String getName() {
 108             throw new UnsupportedOperationException("Not supported yet.");
 109         }
 110 
 111         @Override
 112         public AttributeSet getAttributes() {
 113             return new AttributeSet() {
 114 
 115                 @Override
 116                 public int getAttributeCount() {
 117                     throw new UnsupportedOperationException("Not supported yet.");
 118                 }
 119 
 120                 @Override
 121                 public boolean isDefined(Object attrName) {
 122                     throw new UnsupportedOperationException("Not supported yet.");
 123                 }
 124 
 125                 @Override
 126                 public boolean isEqual(AttributeSet attr) {
 127                     throw new UnsupportedOperationException("Not supported yet.");
 128                 }
 129 
 130                 @Override
 131                 public AttributeSet copyAttributes() {
 132                     throw new UnsupportedOperationException("Not supported yet.");
 133                 }
 134 
 135                 @Override
 136                 public Object getAttribute(Object key) {
 137                     if (key.equals(HTML.Attribute.CLASSID)) {
 138                         return UserJComponent.class.getName();
 139                     }
 140 
 141                     return null;
 142                 }
 143 
 144                 @Override
 145                 public Enumeration<?> getAttributeNames() {
 146                     throw new UnsupportedOperationException("Not supported yet.");
 147                 }
 148 
 149                 @Override
 150                 public boolean containsAttribute(Object name, Object value) {
 151                     throw new UnsupportedOperationException("Not supported yet.");
 152                 }
 153 
 154                 @Override
 155                 public boolean containsAttributes(AttributeSet attributes) {
 156                     throw new UnsupportedOperationException("Not supported yet.");
 157                 }
 158 
 159                 @Override
 160                 public AttributeSet getResolveParent() {
 161                     throw new UnsupportedOperationException("Not supported yet.");
 162                 }
 163             };
 164         }
 165 
 166         @Override
 167         public int getStartOffset() {
 168             throw new UnsupportedOperationException("Not supported yet.");
 169         }
 170 
 171         @Override
 172         public int getEndOffset() {
 173             throw new UnsupportedOperationException("Not supported yet.");
 174         }
 175 
 176         @Override
 177         public int getElementIndex(int offset) {
 178             throw new UnsupportedOperationException("Not supported yet.");
 179         }
 180 
 181         @Override
 182         public int getElementCount() {
 183             throw new UnsupportedOperationException("Not supported yet.");
 184         }
 185 
 186         @Override
 187         public Element getElement(int index) {
 188             throw new UnsupportedOperationException("Not supported yet.");
 189         }
 190 
 191         @Override
 192         public boolean isLeaf() {
 193             throw new UnsupportedOperationException("Not supported yet.");
 194         }
 195     }
 196 }