Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8339603: Seal the class hierarchy of Node, Camera, LightBase, Shape, Shape3D #1556

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.javafx.scene;

import javafx.scene.Node;

/**
* This class only exists so that JavaFX code can extend the {@code Node} class across module
* boundaries, as classes in other modules cannot be permitted subclasses.
*/
public abstract non-sealed class AbstractNode extends Node {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -57,7 +57,7 @@ public static void initHelper(Camera camera) {

@Override
protected NGNode createPeerImpl(Node node) {
throw new UnsupportedOperationException("Applications should not extend the Camera class directly.");
throw new AssertionError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the right error type?
why change UnsupportedOperationException?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Camera is sealed, this method cannot be called by user code. UnsupportedOperationException would be okay if it could reasonably be called, but this is now an invariant that would signal a bug in JavaFX.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe an explanatory comment should be added?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically use InternalError for the case where there should be no possible reason for a particular case, but also use AssertionError in some cases. Either is OK with me. I agree that a comment to the affect of "Should not ever get here" wouldn't be a bad idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment to CameraHelper and LightBaseHelper.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -55,7 +55,7 @@ public static void initHelper(LightBase lightBase) {

@Override
protected NGNode createPeerImpl(Node node) {
throw new UnsupportedOperationException("Applications should not extend the LightBase class directly.");
throw new AssertionError();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.SubScene;
import javafx.scene.shape.Shape;
import javafx.scene.shape.Shape3D;
import javafx.scene.text.Font;

/**
Expand All @@ -65,23 +63,7 @@ protected NodeHelper() {
}

protected static NodeHelper getHelper(Node node) {

NodeHelper helper = nodeAccessor.getHelper(node);
if (helper == null) {
String nodeType;
if (node instanceof Shape) {
nodeType = "Shape";
} else if (node instanceof Shape3D) {
nodeType = "Shape3D";
} else {
nodeType = "Node";
}

throw new UnsupportedOperationException(
"Applications should not extend the "
+ nodeType + " class directly.");
}
return helper;
return nodeAccessor.getHelper(node);
}

protected static void setHelper(Node node, NodeHelper nodeHelper) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.javafx.scene.shape;

import javafx.scene.shape.Shape;

/**
* This class only exists so that JavaFX code can extend the {@code Shape} class across module
* boundaries, as classes in other modules cannot be permitted subclasses.
*/
public abstract non-sealed class AbstractShape extends Shape {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -41,7 +41,7 @@
*
* @since JavaFX 8.0
*/
public class AmbientLight extends LightBase {
public non-sealed class AmbientLight extends LightBase {
static {
AmbientLightHelper.setAmbientLightAccessor(new AmbientLightHelper.AmbientLightAccessor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -101,14 +101,9 @@
* coordinate space. Hence the conversion formula mentioned above is not used.
* </p>
*
* <p>
* An application should not extend the Camera class directly. Doing so may lead to
* an UnsupportedOperationException being thrown.
* </p>
*
* @since JavaFX 2.0
*/
public abstract class Camera extends Node {
public abstract sealed class Camera extends Node permits ParallelCamera, PerspectiveCamera {
static {
// This is used by classes in different packages to get access to
// private and package private methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -48,7 +48,7 @@
* @since 18
* @see PhongMaterial
*/
public class DirectionalLight extends LightBase {
public non-sealed class DirectionalLight extends LightBase {
static {
DirectionalLightHelper.setDirectionalLightAccessor(new DirectionalLightHelper.DirectionalLightAccessor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -97,9 +97,6 @@
* * Supports spotlight attenuation factors as described in its class docs.
*
* <p>
* An application cannot add its own light types. Extending {@code LightBase} directly may lead to an
* {@code UnsupportedOperationException} being thrown.
* <p>
* All light types are not affected by scaling and shearing transforms.
*
* <h2><a id="Color">Color</a></h2>
Expand Down Expand Up @@ -166,7 +163,7 @@
*
* @since JavaFX 8.0
*/
public abstract class LightBase extends Node {
public abstract sealed class LightBase extends Node permits AmbientLight, DirectionalLight, PointLight {
static {
// This is used by classes in different packages to get access to
// private and package private methods.
Expand Down
13 changes: 7 additions & 6 deletions modules/javafx.graphics/src/main/java/javafx/scene/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@
import javafx.geometry.Point2D;
import javafx.geometry.Point3D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.Effect;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.DragEvent;
Expand All @@ -98,6 +100,7 @@
import javafx.scene.input.TouchEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.input.ZoomEvent;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Transform;
Expand Down Expand Up @@ -147,6 +150,7 @@
import com.sun.javafx.geom.transform.GeneralTransform3D;
import com.sun.javafx.geom.transform.NoninvertibleTransformException;
import com.sun.javafx.perf.PerformanceTracker;
import com.sun.javafx.scene.AbstractNode;
import com.sun.javafx.scene.BoundsAccessor;
import com.sun.javafx.scene.CameraHelper;
import com.sun.javafx.scene.CssFlags;
Expand Down Expand Up @@ -227,11 +231,6 @@
* the {@link Platform#startup(Runnable)} method for more information.
* </p>
*
* <p>
* An application should not extend the Node class directly. Doing so may lead to
* an UnsupportedOperationException being thrown.
* </p>
*
* <h2><a id="StringID">String ID</a></h2>
* <p>
* Each node in the scene graph can be given a unique {@link #idProperty id}. This id is
Expand Down Expand Up @@ -412,7 +411,9 @@
* @since JavaFX 2.0
*/
@IDProperty("id")
public abstract class Node implements EventTarget, Styleable {
public abstract sealed class Node
implements EventTarget, Styleable
permits AbstractNode, Camera, LightBase, Parent, SubScene, Canvas, ImageView, Shape, Shape3D {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to use stronger wording in javadoc in regards to extending Nodes. should -> must, etc. (or, rather, 'cannot' and possibly explain why?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the language that you can't extend those classes, as it is now redundant because it is enforced by the compiler. We shouldn't document obvious facts without providing any added value (for example, adding context and reasoning why it's the way it is).


/*
* Store the singleton instance of the NodeHelper subclass corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -47,7 +47,7 @@
*
* @since JavaFX 2.0
*/
public class ParallelCamera extends Camera {
public non-sealed class ParallelCamera extends Camera {
static {
ParallelCameraHelper.setParallelCameraAccessor(new ParallelCameraHelper.ParallelCameraAccessor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -76,7 +76,7 @@
*
* @since JavaFX 2.0
*/
public abstract class Parent extends Node {
public abstract non-sealed class Parent extends Node {
// package private for testing
static final int DIRTY_CHILDREN_THRESHOLD = 10;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -86,7 +86,7 @@
*
* @since JavaFX 2.0
*/
public class PerspectiveCamera extends Camera {
public non-sealed class PerspectiveCamera extends Camera {

private boolean fixedEyeAtCameraZero = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -46,7 +46,7 @@
* @since JavaFX 8.0
* @see PhongMaterial
*/
public class PointLight extends LightBase {
public non-sealed class PointLight extends LightBase {
static {
PointLightHelper.setPointLightAccessor(new PointLightHelper.PointLightAccessor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
*
* @since JavaFX 8.0
*/
public class SubScene extends Node {
public non-sealed class SubScene extends Node {
static {
// This is used by classes in different packages to get access to
// private and package private methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -71,7 +71,7 @@
* @see GraphicsContext
* @since JavaFX 2.2
*/
public class Canvas extends Node {
public non-sealed class Canvas extends Node {
static {
CanvasHelper.setCanvasAccessor(new CanvasHelper.CanvasAccessor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -144,7 +144,7 @@
* @since JavaFX 2.0
*/
@DefaultProperty("image")
public class ImageView extends Node {
public non-sealed class ImageView extends Node {
static {
// This is used by classes in different packages to get access to
// private and package private methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -63,7 +63,7 @@
</PRE>
* @since JavaFX 2.0
*/
public class Arc extends Shape {
public non-sealed class Arc extends Shape {
static {
ArcHelper.setArcAccessor(new ArcHelper.ArcAccessor() {
@Override
Expand Down
Loading