Created
January 20, 2025 14:23
-
-
Save asdf913/37fdafeb7e6189727f8497ab6323c796 to your computer and use it in GitHub Desktop.
Print the "java.awt.datatransfer.DataFlavor" constants which are "java.awt.datatransfer.DataFlavor". Also list the result of "java.awt.datatransfer.Transferable.isDataFlavorSupported(java.awt.datatransfer.DataFlavor)" against all "java.awt.datatransfer.DataFlavor" constants which are "java.awt.datatransfer.DataFlavor"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.GraphicsEnvironment; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.datatransfer.DataFlavor; | |
import java.awt.datatransfer.Transferable; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Member; | |
import java.lang.reflect.Modifier; | |
import java.lang.reflect.Proxy; | |
import java.util.Arrays; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Objects; | |
import java.util.OptionalInt; | |
import java.util.Set; | |
import java.util.Spliterator; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.function.ToIntFunction; | |
import java.util.stream.Collector; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
import java.util.stream.StreamSupport; | |
import org.apache.commons.collections4.IterableUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.commons.lang3.function.FailableFunction; | |
import org.apache.commons.lang3.stream.Streams.FailableStream; | |
import io.github.toolfactory.narcissus.Narcissus; | |
public class Main { | |
public static void main(String[] args) { | |
// | |
final FailableStream<Field> fs = testAndApply(Objects::nonNull, | |
filter(testAndApply(Objects::nonNull, getDeclaredFields(DataFlavor.class), Arrays::stream, null), | |
f -> Boolean.logicalAnd(Objects.equals(DataFlavor.class, getType(f)), isStatic(f))), | |
FailableStream::new, null); | |
// | |
final Iterable<Entry<String, DataFlavor>> entrySet = entrySet(collect(stream(fs), Collectors.toMap( | |
Main::getName, | |
f -> testAndApply(Main::isStatic, f, x -> cast(DataFlavor.class, Narcissus.getStaticField(x)), null)))); | |
// | |
Entry<String, DataFlavor> entry = null; | |
// | |
DataFlavor df = null; | |
// | |
final int maxLength1 = orElse(max(mapToInt( | |
map(testAndApply(Objects::nonNull, spliterator(entrySet), x -> StreamSupport.stream(x, false), null), | |
Main::getKey), | |
StringUtils::length)), 0); | |
// | |
final int maxLength2 = orElse( | |
max(mapToInt(map(testAndApply(Objects::nonNull, spliterator(entrySet), | |
x -> StreamSupport.stream(x, false), null), x -> toString(getValue(x))), StringUtils::length)), | |
0); | |
// | |
final Transferable transferable = getContents(testAndApply(x -> !GraphicsEnvironment.isHeadless(), | |
Toolkit.getDefaultToolkit(), x -> getSystemClipboard(x), null), null); | |
// | |
for (int i = 0; i < IterableUtils.size(entrySet); i++) { | |
// | |
System.out.println(StringUtils.joinWith("\t", | |
StringUtils.rightPad(getKey(entry = IterableUtils.get(entrySet, i)), maxLength1), | |
StringUtils.rightPad(toString(df = getValue(entry)), maxLength2), | |
isDataFlavorSupported(transferable, df))); | |
// | |
} // for | |
// | |
} | |
private static boolean isDataFlavorSupported(final Transferable instance, final DataFlavor flavor) { | |
return instance != null && instance.isDataFlavorSupported(flavor); | |
} | |
private static Transferable getContents(final Clipboard instance, final Object requestor) { | |
return instance != null ? instance.getContents(requestor) : null; | |
} | |
private static Clipboard getSystemClipboard(final Toolkit instance) { | |
return instance != null ? instance.getSystemClipboard() : null; | |
} | |
private static String toString(final Object instance) { | |
return instance != null ? instance.toString() : null; | |
} | |
private static <T> T cast(final Class<T> clz, final Object value) { | |
return clz != null && clz.isInstance(value) ? clz.cast(value) : null; | |
} | |
private static <O> Stream<O> stream(final FailableStream<O> instance) { | |
return instance != null ? instance.stream() : null; | |
} | |
private static OptionalInt max(final IntStream instance) { | |
return instance != null ? instance.max() : null; | |
} | |
private static int orElse(final OptionalInt instance, final int other) { | |
return instance != null ? instance.orElse(other) : other; | |
} | |
private static <T> IntStream mapToInt(final Stream<T> instance, final ToIntFunction<? super T> mapper) { | |
// | |
return instance != null && (Proxy.isProxyClass(getClass(instance)) || mapper != null) | |
? instance.mapToInt(mapper) | |
: null; | |
// | |
} | |
private static <K> K getKey(final Entry<K, ?> instance) { | |
return instance != null ? instance.getKey() : null; | |
} | |
private static <V> V getValue(final Entry<?, V> instance) { | |
return instance != null ? instance.getValue() : null; | |
} | |
private static <T> Spliterator<T> spliterator(final Iterable<T> instance) { | |
return instance != null ? instance.spliterator() : null; | |
} | |
private static <T, R, A> R collect(final Stream<T> instance, final Collector<? super T, A, R> collector) { | |
// | |
return instance != null && (collector != null || Proxy.isProxyClass(getClass(instance))) | |
? instance.collect(collector) | |
: null; | |
// | |
} | |
private static String getName(final Member instance) { | |
return instance != null ? instance.getName() : null; | |
} | |
private static <K, V> Set<Entry<K, V>> entrySet(final Map<K, V> instance) { | |
return instance != null ? instance.entrySet() : null; | |
} | |
private static <T, R> Stream<R> map(final Stream<T> instance, final Function<? super T, ? extends R> mapper) { | |
// | |
return instance != null && (Proxy.isProxyClass(getClass(instance)) || mapper != null) ? instance.map(mapper) | |
: null; | |
// | |
} | |
private static boolean isStatic(final Member instance) { | |
return instance != null && Modifier.isStatic(instance.getModifiers()); | |
} | |
private static Class<?> getType(final Field instance) { | |
return instance != null ? instance.getType() : null; | |
} | |
private static <T> Stream<T> filter(final Stream<T> instance, final Predicate<? super T> predicate) { | |
// | |
return instance != null && (predicate != null || Proxy.isProxyClass(getClass(instance))) | |
? instance.filter(predicate) | |
: null; | |
// | |
} | |
private static Class<?> getClass(final Object instance) { | |
return instance != null ? instance.getClass() : null; | |
} | |
private static <T, R, E extends Throwable> R testAndApply(final Predicate<T> predicate, final T value, | |
final FailableFunction<T, R, E> functionTrue, final FailableFunction<T, R, E> functionFalse) throws E { | |
return test(predicate, value) ? apply(functionTrue, value) : apply(functionFalse, value); | |
} | |
private static <T, R, E extends Throwable> R apply(final FailableFunction<T, R, E> instance, final T value) | |
throws E { | |
return instance != null ? instance.apply(value) : null; | |
} | |
private static final <T> boolean test(final Predicate<T> instance, final T value) { | |
return instance != null && instance.test(value); | |
} | |
private static Field[] getDeclaredFields(final Class<?> instance) { | |
return instance != null ? instance.getDeclaredFields() : null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
<version>3.17.0</version> | |
</dependency> | |
<!--https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-collections4</artifactId> | |
<version>4.4</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/io.github.toolfactory/narcissus --> | |
<dependency> | |
<groupId>io.github.toolfactory</groupId> | |
<artifactId>narcissus</artifactId> | |
<version>1.0.7</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output