Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public Builder impl(String className) {

try {
this.foundClass = Class.forName(className, true, loader);
} catch (ClassNotFoundException e) {
// not the right implementation
} catch (ClassNotFoundException | NoClassDefFoundError e) {
Comment thread
nastra marked this conversation as resolved.
// cannot load this implementation
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public <C> Ctor<C> build() {
private Class<?> classForName(String className) throws ClassNotFoundException {
try {
return Class.forName(className, true, loader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
Comment thread
nastra marked this conversation as resolved.
if (loader != Thread.currentThread().getContextClassLoader()) {
return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
} else {
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/org/apache/iceberg/common/DynFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ public Builder impl(String className, String fieldName) {
try {
Class<?> targetClass = Class.forName(className, true, loader);
impl(targetClass, fieldName);
} catch (ClassNotFoundException e) {
// not the right implementation
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// cannot load this implementation
candidates.add(className + "." + fieldName);
}
return this;
Expand Down Expand Up @@ -284,8 +284,8 @@ public Builder hiddenImpl(String className, String fieldName) {
try {
Class<?> targetClass = Class.forName(className, true, loader);
hiddenImpl(targetClass, fieldName);
} catch (ClassNotFoundException e) {
// not the right implementation
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// cannot load this implementation
candidates.add(className + "." + fieldName);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ public Builder impl(String className, String methodName, Class<?>... argClasses)
try {
Class<?> targetClass = Class.forName(className, true, loader);
impl(targetClass, methodName, argClasses);
} catch (ClassNotFoundException e) {
// not the right implementation
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// cannot load this implementation
}
return this;
}
Expand Down Expand Up @@ -333,8 +333,8 @@ public Builder hiddenImpl(String className, String methodName, Class<?>... argCl
try {
Class<?> targetClass = Class.forName(className, true, loader);
hiddenImpl(targetClass, methodName, argClasses);
} catch (ClassNotFoundException e) {
// not the right implementation
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// cannot load this implementation
}
return this;
}
Expand Down
88 changes: 88 additions & 0 deletions common/src/test/java/org/apache/iceberg/common/TestDynClasses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.apache.iceberg.common;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

class TestDynClasses {
static class Available {}

@Test
void implWithNoClassDefFoundError() throws ClassNotFoundException {
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if ("org.apache.iceberg.MissingDependencyClass".equals(name)) {
throw new NoClassDefFoundError("some/TransitiveDependency");
}

return super.loadClass(name, resolve);
}
};

assertThatThrownBy(
() ->
DynClasses.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass")
.buildChecked())
.isInstanceOf(ClassNotFoundException.class)
.hasMessage("Cannot find class; alternatives: org.apache.iceberg.MissingDependencyClass");

assertThat(
DynClasses.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass")
.orNull()
.buildChecked())
.isNull();

assertThat(
DynClasses.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass")
.impl("org.apache.iceberg.common.TestDynClasses$Available")
.buildChecked())
.isEqualTo(Available.class);
}

@Test
void implWithExceptionInInitializerError() {
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
throw new ExceptionInInitializerError("static initializer failed");
}
};

assertThatThrownBy(
() ->
DynClasses.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.FailingInitClass")
.buildChecked())
.isInstanceOf(ExceptionInInitializerError.class)
.hasMessage("static initializer failed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,92 @@ public void testLoaderFallback() throws Exception {
assertThat(ctor.newInstance()).isInstanceOf(MyClass.class);
}

@Test
public void implWithNoClassDefFoundError() throws Exception {
Comment thread
nastra marked this conversation as resolved.
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if ("org.apache.iceberg.MissingDependencyClass".equals(name)) {
throw new NoClassDefFoundError("some/TransitiveDependency");
}

return super.loadClass(name, resolve);
}
};

assertThatThrownBy(
() ->
DynConstructors.builder(MyInterface.class)
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass")
.buildChecked())
.isInstanceOf(NoSuchMethodException.class)
.hasMessageStartingWith("Cannot find constructor for interface")
.hasMessageContaining("Missing org.apache.iceberg.MissingDependencyClass");

assertThat(
DynConstructors.builder(MyInterface.class)
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass")
.impl(MyClass.class)
.buildChecked()
.newInstance())
.isInstanceOf(MyClass.class);

assertThat(
DynConstructors.builder(MyInterface.class)
.loader(errorLoader)
.hiddenImpl("org.apache.iceberg.MissingDependencyClass")
.impl(MyClass.class)
.buildChecked()
.newInstance())
.isInstanceOf(MyClass.class);
}

@Test
public void implWithNoClassDefFoundErrorFallsBackToContextClassLoader() throws Exception {
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (MyClass.class.getName().equals(name)) {
throw new NoClassDefFoundError("some/TransitiveDependency");
}

return super.loadClass(name, resolve);
}
};

assertThat(
DynConstructors.builder(MyInterface.class)
.loader(errorLoader)
.impl(MyClass.class.getName())
.buildChecked()
.newInstance())
.isInstanceOf(MyClass.class);
}

@Test
public void implWithExceptionInInitializerError() {
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
throw new ExceptionInInitializerError("static initializer failed");
}
};

assertThatThrownBy(
() ->
DynConstructors.builder(MyInterface.class)
.loader(errorLoader)
.impl("org.apache.iceberg.FailingInitClass")
.buildChecked())
.isInstanceOf(ExceptionInInitializerError.class)
.hasMessage("static initializer failed");
}

public interface MyInterface {}

public static class MyClass implements MyInterface {}
Expand Down
96 changes: 96 additions & 0 deletions common/src/test/java/org/apache/iceberg/common/TestDynFields.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.apache.iceberg.common;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

class TestDynFields {
static class FieldHolder {
public String value = "hello";

@SuppressWarnings("unused")
private String hidden = "secret";
}

@Test
void implWithNoClassDefFoundError() throws NoSuchFieldException {
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if ("org.apache.iceberg.MissingDependencyClass".equals(name)) {
throw new NoClassDefFoundError("some/TransitiveDependency");
}

return super.loadClass(name, resolve);
}
};

assertThatThrownBy(
() ->
DynFields.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass", "value")
.buildChecked())
.isInstanceOf(NoSuchFieldException.class)
.hasMessage(
"Cannot find field from candidates: org.apache.iceberg.MissingDependencyClass.value");

assertThat(
DynFields.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.MissingDependencyClass", "value")
.impl(FieldHolder.class, "value")
.<String>buildChecked()
.get(new FieldHolder()))
.isEqualTo("hello");

assertThat(
DynFields.builder()
.loader(errorLoader)
.hiddenImpl("org.apache.iceberg.MissingDependencyClass", "hidden")
.hiddenImpl(FieldHolder.class, "hidden")
.<String>buildChecked()
.get(new FieldHolder()))
.isEqualTo("secret");
}

@Test
void implWithExceptionInInitializerError() {
ClassLoader errorLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
throw new ExceptionInInitializerError("static initializer failed");
}
};

assertThatThrownBy(
() ->
DynFields.builder()
.loader(errorLoader)
.impl("org.apache.iceberg.FailingInitClass", "value")
.buildChecked())
.isInstanceOf(ExceptionInInitializerError.class)
.hasMessage("static initializer failed");
}
}
Loading