Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ jobs:
if: ${{ matrix.java == '21' }}
run: .github/retry.sh ant $OPTS -f platform/masterfs test

- name: platform/o.n.jdk.fallback
run: ant $OPTS -f platform/o.n.jdk.fallback test

- name: Commit Validation tests
run: .github/retry.sh ant $OPTS -Dcluster.config=$CLUSTER_CONFIG commit-validation

Expand Down
1 change: 1 addition & 0 deletions nbbuild/cluster.properties
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ nb.cluster.platform=\
o.n.html.ko4j,\
o.n.html.presenters.spi,\
o.n.html.xhr4j,\
o.n.jdk.fallback,\
o.n.swing.laf.dark,\
o.n.swing.laf.flatlaf,\
o.n.swing.outline,\
Expand Down
25 changes: 25 additions & 0 deletions platform/o.n.jdk.fallback/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<project basedir="." default="build" name="platform/o.n.jdk.fallback">
<description>Builds, tests, and runs the project org.netbeans.jdk.fallback</description>
<import file="../../nbbuild/templates/projectized.xml"/>
</project>
6 changes: 6 additions & 0 deletions platform/o.n.jdk.fallback/manifest.mf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Manifest-Version: 1.0
AutoUpdate-Show-In-Client: false
AutoUpdate-Essential-Module: true
OpenIDE-Module: o.n.jdk.fallback/0
OpenIDE-Module-Localizing-Bundle: org/netbeans/jdk/fallback/Bundle.properties
OpenIDE-Module-Specification-Version: 0.1
23 changes: 23 additions & 0 deletions platform/o.n.jdk.fallback/nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

javac.compilerargs=-Xlint
javac.release=17

# javadoc.arch=${basedir}/arch.xml
# javadoc.apichanges=${basedir}/apichanges.xml
# javadoc.arch=${basedir}/arch.xml
48 changes: 48 additions & 0 deletions platform/o.n.jdk.fallback/nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.apisupport.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
<code-name-base>o.n.jdk.fallback</code-name-base>
<module-dependencies/>
<test-dependencies>
<test-type>
<name>unit</name>
<test-dependency>
<code-name-base>org.netbeans.libs.junit4</code-name-base>
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.modules.nbjunit</code-name-base>
<compile-dependency/>
</test-dependency>
</test-type>
</test-dependencies>
<!-- internal API-->
<friend-packages>
<friend>org.netbeans.modules.maven.indexer</friend>
<package>org.netbeans.jdk.fallback.lang</package>
</friend-packages>
</data>
</configuration>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

OpenIDE-Module-Name=Simple JDK Fallbacks
OpenIDE-Module-Display-Category=Libraries
OpenIDE-Module-Short-Description=Provides internal, non-permanent and possibly incomplete fallbacks for JDK APIs\
which are useful for NetBeans but not covered by the minimal run requirements yet.
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.jdk.fallback.lang;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Delegates to JDK's LazyConstant and provides a fallback implementation if not available.
*
* Internal API, may be removed when no longer needed.
*
* @author mbien
*/
public final class NBLazyConstant {

private static final MethodHandle lazyConstantFactory;

static {
Logger log = Logger.getLogger(NBLazyConstant.class.getName());
MethodHandle mh = null;
try {
if (Boolean.getBoolean("nb.jdk.LazyConstant.usefallback")) {
mh = null;
log.log(Level.INFO, "using fallback");
} else if (Runtime.version().feature() >= 26) {
Class<?> entryPoint = Class.forName("java.lang.LazyConstant");
mh = MethodHandles.lookup().findStatic(entryPoint, "of", MethodType.methodType(entryPoint, Supplier.class))
.asType(MethodType.methodType(Supplier.class, Supplier.class));
} else if (Runtime.version().feature() == 25) {
Class<?> entryPoint = Class.forName("java.lang.StableValue");
mh = MethodHandles.lookup().findStatic(entryPoint, "supplier", MethodType.methodType(Supplier.class, Supplier.class));
}
// dryrun - just to be sure
if (mh != null) {
Supplier<?> probe = () -> true;
((Supplier<?>)mh.invokeExact(probe)).get();
}
} catch (Throwable ex) {
mh = null;
log.log(Level.FINE, "using fallback", ex);
}
lazyConstantFactory = mh;
log.log(Level.FINE, () -> "impl=" + String.valueOf(lazyConstantFactory));
}

private NBLazyConstant() {}

/**
* Create a {@link Supplier} for a lazily initializing constant.
* @param computingFunction Factory to create the constant, only called once.
* @return Returns the constant, never null.
*/
@SuppressWarnings("unchecked")
public static <T> Supplier<T> of(Supplier<? extends T> computingFunction) {
Objects.requireNonNull(computingFunction);
if (computingFunction instanceof DoubleCheckedFallback<? extends T> lc) {
return (Supplier<T>) lc;
}
if (lazyConstantFactory != null) {
try {
return (Supplier<T>) lazyConstantFactory.invokeExact(computingFunction);
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable ex) {
throw new RuntimeException(ex); // shouldn't be reachable under regular circumstances
}
} else {
return new DoubleCheckedFallback<>(computingFunction);
}
}

private static class DoubleCheckedFallback<T> implements Supplier<T> {

private volatile T constant;
private Supplier<? extends T> factory;

private DoubleCheckedFallback(Supplier<? extends T> factory) {
this.factory = factory;
}

@Override
public T get() {
T c = constant;
if (c == null) {
synchronized (this) {
c = constant;
if (c == null) {
c = factory.get();
Objects.requireNonNull(c);
constant = c;
factory = null;
}
}
}
return c;
}

@Override
public String toString() {
return getClass().getSimpleName() + "{factory=" + factory + ", constant=" + constant + '}';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.jdk.fallback.lang;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

public class NBLazyConstantTest {

@Test
public void testFactory() {

Supplier<Boolean> lazy = NBLazyConstant.of(() -> true);
assertNotNull(lazy);
assertTrue(lazy.get());

String impl;
if (Runtime.version().feature() >= 26) {
impl = "LazyConstant";
} else if (Runtime.version().feature() == 25) {
impl = "StableSupplier";
} else {
impl = "DoubleCheckedFallback";
}
assertTrue(impl + " expected but got " + lazy.getClass(), lazy.getClass().getSimpleName().contains(impl));
}

@Test
public void testConstant() {
Supplier<Double> lazy = NBLazyConstant.of(() -> Math.random());
assertNotNull(lazy);
assertEquals(lazy.get(), lazy.get());
assertEquals(lazy.get(), lazy.get());
assertEquals(lazy.get(), lazy.get());
}

@Test
public void testRequireNPEOnNullResult() {

// StableValue allows null, we use the LazyConstant spec
assumeTrue(Runtime.version().feature() != 25);

AtomicReference<Object> value = new AtomicReference<>();

Supplier<Object> lazy = NBLazyConstant.of(() -> value.get());
assertNotNull(lazy);

try {
lazy.get();
fail();
} catch (NullPointerException good) {}

try {
lazy.get();
fail();
} catch (NullPointerException stillGood) {}

value.set("good");

// constant can be computed from now on
assertEquals("good", lazy.get());
assertEquals("good", lazy.get());
}

@Test
public void testExceptionDuringCompute() {

AtomicBoolean fail = new AtomicBoolean(true);

Supplier<Object> lazy = NBLazyConstant.of(() -> {
if (fail.get()) {
throw new RuntimeException("can't compute");
} else {
return "good";
}
});
assertNotNull(lazy);

try {
lazy.get();
fail();
} catch (RuntimeException good) {}

try {
lazy.get();
fail();
} catch (RuntimeException stillGood) {}

fail.set(false);

// constant can be computed from now on
assertEquals("good", lazy.get());
assertEquals("good", lazy.get());
}

}
Loading