Skip to content

Commit ce807af

Browse files
committed
PARQUET-777: Move dynamic support classes, add tests.
1 parent 57245b6 commit ce807af

File tree

8 files changed

+864
-41
lines changed

8 files changed

+864
-41
lines changed

parquet-cli/src/main/java/org/apache/parquet/cli/commands/CheckParquet251Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.apache.parquet.Version;
3131
import org.apache.parquet.VersionParser;
3232
import org.apache.parquet.bytes.BytesInput;
33-
import org.apache.parquet.cli.util.DynConstructors;
33+
import org.apache.parquet.util.DynConstructors;
3434
import org.apache.parquet.column.ColumnDescriptor;
3535
import org.apache.parquet.column.ColumnReader;
3636
import org.apache.parquet.column.page.DataPage;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.parquet;
21+
22+
public class Exceptions {
23+
/**
24+
* If the given throwable is an instance of E, throw it as an E.
25+
*/
26+
public static <E extends Exception> void throwIfInstance(Throwable t,
27+
Class<E> excClass)
28+
throws E {
29+
if (excClass.isAssignableFrom(t.getClass())) {
30+
// the throwable is already an exception, so return it
31+
throw excClass.cast(t);
32+
}
33+
}
34+
}

parquet-cli/src/main/java/org/apache/parquet/cli/util/DynConstructors.java renamed to parquet-common/src/main/java/org/apache/parquet/util/DynConstructors.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
* under the License.
1818
*/
1919

20-
package org.apache.parquet.cli.util;
20+
package org.apache.parquet.util;
2121

22-
import com.google.common.base.Objects;
23-
import com.google.common.base.Preconditions;
24-
import com.google.common.base.Throwables;
25-
import com.google.common.collect.Maps;
22+
import org.apache.parquet.Preconditions;
2623
import java.lang.reflect.Constructor;
2724
import java.lang.reflect.InvocationTargetException;
2825
import java.security.AccessController;
2926
import java.security.PrivilegedAction;
30-
import java.util.Arrays;
27+
import java.util.HashMap;
3128
import java.util.Map;
3229

30+
import static org.apache.parquet.Exceptions.throwIfInstance;
31+
3332
public class DynConstructors {
3433
public static class Ctor<C> extends DynMethods.UnboundMethod {
3534
private final Constructor<C> ctor;
@@ -53,18 +52,18 @@ public C newInstanceChecked(Object... args) throws Exception {
5352
} catch (IllegalAccessException e) {
5453
throw e;
5554
} catch (InvocationTargetException e) {
56-
// rethrow the cause is an exception
57-
Throwables.propagateIfPossible(e.getCause(), Exception.class);
58-
// otherwise, propagate the throwable
59-
throw Throwables.propagate(e.getCause());
55+
throwIfInstance(e.getCause(), Exception.class);
56+
throwIfInstance(e.getCause(), RuntimeException.class);
57+
throw new RuntimeException(e.getCause());
6058
}
6159
}
6260

6361
public C newInstance(Object... args) {
6462
try {
6563
return newInstanceChecked(args);
6664
} catch (Exception e) {
67-
throw Throwables.propagate(e);
65+
throwIfInstance(e, RuntimeException.class);
66+
throw new RuntimeException(e);
6867
}
6968
}
7069

@@ -73,7 +72,7 @@ public C newInstance(Object... args) {
7372
public <R> R invoke(Object target, Object... args) {
7473
Preconditions.checkArgument(target == null,
7574
"Invalid call to constructor: target must be null");
76-
return (R) newInstance(target, args);
75+
return (R) newInstance(args);
7776
}
7877

7978
@Override
@@ -84,25 +83,28 @@ public <R> R invokeChecked(Object target, Object... args) throws Exception {
8483
return (R) newInstanceChecked(args);
8584
}
8685

86+
@Override
87+
public DynMethods.BoundMethod bind(Object receiver) {
88+
throw new IllegalStateException("Cannot bind constructors");
89+
}
90+
8791
@Override
8892
public boolean isStatic() {
8993
return true;
9094
}
9195

9296
@Override
9397
public String toString() {
94-
return Objects.toStringHelper(this)
95-
.add("constructor", ctor)
96-
.add("class", constructed)
97-
.toString();
98+
return getClass().getSimpleName() +
99+
"(constructor=" + ctor + ", class=" + constructed + ")";
98100
}
99101
}
100102

101103
public static class Builder {
102104
private final Class<?> baseClass;
103105
private ClassLoader loader = Thread.currentThread().getContextClassLoader();
104106
private Ctor ctor = null;
105-
private Map<String, Throwable> problems = Maps.newHashMap();
107+
private Map<String, Throwable> problems = new HashMap<String, Throwable>();
106108

107109
public Builder(Class<?> baseClass) {
108110
this.baseClass = baseClass;
@@ -125,11 +127,6 @@ public Builder loader(ClassLoader loader) {
125127
return this;
126128
}
127129

128-
public Builder impl(Class<?>... types) {
129-
impl(baseClass, types);
130-
return this;
131-
}
132-
133130
public Builder impl(String className, Class<?>... types) {
134131
// don't do any work if an implementation has been found
135132
if (ctor != null) {

parquet-cli/src/main/java/org/apache/parquet/cli/util/DynMethods.java renamed to parquet-common/src/main/java/org/apache/parquet/util/DynMethods.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
* under the License.
1818
*/
1919

20-
package org.apache.parquet.cli.util;
20+
package org.apache.parquet.util;
2121

22-
import com.google.common.base.Objects;
23-
import com.google.common.base.Preconditions;
24-
import com.google.common.base.Throwables;
22+
import org.apache.parquet.Preconditions;
2523
import java.lang.reflect.InvocationTargetException;
2624
import java.lang.reflect.Method;
2725
import java.lang.reflect.Modifier;
2826
import java.security.AccessController;
2927
import java.security.PrivilegedAction;
30-
import java.security.PrivilegedExceptionAction;
3128
import java.util.Arrays;
3229

30+
import static org.apache.parquet.Exceptions.throwIfInstance;
31+
3332
public class DynMethods {
3433

3534
/**
@@ -61,18 +60,18 @@ public <R> R invokeChecked(Object target, Object... args) throws Exception {
6160
}
6261

6362
} catch (InvocationTargetException e) {
64-
// rethrow the cause is an exception
65-
Throwables.propagateIfPossible(e.getCause(), Exception.class);
66-
// otherwise, propagate the throwable
67-
throw Throwables.propagate(e.getCause());
63+
throwIfInstance(e.getCause(), Exception.class);
64+
throwIfInstance(e.getCause(), RuntimeException.class);
65+
throw new RuntimeException(e.getCause());
6866
}
6967
}
7068

7169
public <R> R invoke(Object target, Object... args) {
7270
try {
7371
return this.<R>invokeChecked(target, args);
7472
} catch (Exception e) {
75-
throw Throwables.propagate(e);
73+
throwIfInstance(e, RuntimeException.class);
74+
throw new RuntimeException(e);
7675
}
7776
}
7877

@@ -121,10 +120,8 @@ public StaticMethod asStatic() {
121120
}
122121

123122
public String toString() {
124-
return Objects.toStringHelper(this)
125-
.add("name", name)
126-
.add("method", method.toGenericString())
127-
.toString();
123+
return "DynMethods.UnboundMethod(name=" + name +" method=" +
124+
method.toGenericString() + ")";
128125
}
129126

130127
/**
@@ -153,9 +150,7 @@ public boolean isStatic() {
153150

154151
@Override
155152
public String toString() {
156-
return Objects.toStringHelper(this)
157-
.add("name", "noop")
158-
.toString();
153+
return "DynMethods.UnboundMethod(NOOP)";
159154
}
160155
};
161156
}
@@ -223,7 +218,7 @@ public Builder loader(ClassLoader loader) {
223218
*
224219
* @return this Builder for method chaining
225220
*/
226-
public Builder defaultNoop() {
221+
public Builder orNoop() {
227222
if (method == null) {
228223
this.method = UnboundMethod.NOOP;
229224
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.parquet;
21+
22+
import org.junit.Assert;
23+
import java.util.concurrent.Callable;
24+
25+
public class TestUtils {
26+
27+
/**
28+
* A convenience method to avoid a large number of @Test(expected=...) tests
29+
* @param message A String message to describe this assertion
30+
* @param expected An Exception class that the Runnable should throw
31+
* @param callable A Callable that is expected to throw the exception
32+
*/
33+
public static void assertThrows(
34+
String message, Class<? extends Exception> expected, Callable callable) {
35+
try {
36+
callable.call();
37+
Assert.fail("No exception was thrown (" + message + "), expected: " +
38+
expected.getName());
39+
} catch (Exception actual) {
40+
try {
41+
Assert.assertEquals(message, expected, actual.getClass());
42+
} catch (AssertionError e) {
43+
e.addSuppressed(actual);
44+
throw e;
45+
}
46+
}
47+
}
48+
49+
/**
50+
* A convenience method to avoid a large number of @Test(expected=...) tests
51+
* @param message A String message to describe this assertion
52+
* @param expected An Exception class that the Runnable should throw
53+
* @param runnable A Runnable that is expected to throw the runtime exception
54+
*/
55+
public static void assertThrows(
56+
String message, Class<? extends Exception> expected, Runnable runnable) {
57+
try {
58+
runnable.run();
59+
Assert.fail("No exception was thrown (" + message + "), expected: " +
60+
expected.getName());
61+
} catch (Exception actual) {
62+
try {
63+
Assert.assertEquals(message, expected, actual.getClass());
64+
} catch (AssertionError e) {
65+
e.addSuppressed(actual);
66+
throw e;
67+
}
68+
}
69+
}
70+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.parquet.util;
21+
22+
/**
23+
* This is a class for testing DynMethods and DynConstructors.
24+
*/
25+
public class Concatenator {
26+
public static class SomeCheckedException extends Exception {
27+
}
28+
29+
private String sep = "";
30+
31+
public Concatenator() {
32+
}
33+
34+
public Concatenator(String sep) {
35+
this.sep = sep;
36+
}
37+
38+
private Concatenator(char sep) {
39+
this.sep = String.valueOf(sep);
40+
}
41+
42+
public Concatenator(Exception e) throws Exception {
43+
throw e;
44+
}
45+
46+
public static Concatenator newConcatenator(String sep) {
47+
return new Concatenator(sep);
48+
}
49+
50+
private void setSeparator(String sep) {
51+
this.sep = sep;
52+
}
53+
54+
public String concat(String left, String right) {
55+
return left + sep + right;
56+
}
57+
58+
public String concat(String left, String middle, String right) {
59+
return left + sep + middle + sep + right;
60+
}
61+
62+
public String concat(Exception e) throws Exception {
63+
throw e;
64+
}
65+
66+
public String concat(String... strings) {
67+
if (strings.length >= 1) {
68+
StringBuilder sb = new StringBuilder();
69+
sb.append(strings[0]);
70+
for (int i = 1; i < strings.length; i += 1) {
71+
sb.append(sep);
72+
sb.append(strings[i]);
73+
}
74+
return sb.toString();
75+
}
76+
return null;
77+
}
78+
79+
public static String cat(String... strings) {
80+
return new Concatenator().concat(strings);
81+
}
82+
}

0 commit comments

Comments
 (0)