|
package com.cogvio.util.nio; |
|
|
|
import sun.nio.fs.AbstractFileTypeDetector; |
|
import sun.nio.fs.GnomeFileTypeDetector; |
|
|
|
import java.lang.reflect.Field; |
|
import java.nio.file.Path; |
|
import java.nio.file.spi.FileTypeDetector; |
|
import java.util.*; |
|
|
|
public class MimeTest |
|
{ |
|
|
|
public static void main(String[] arg) throws Exception |
|
{ |
|
Class<?> ftdClass = Class.forName("java.nio.file.Files$FileTypeDetectors", true, MimeTest.class.getClassLoader()); |
|
|
|
List<FileTypeDetector> installeDetectors = readStaticPrivateField(ftdClass, "installeDetectors"); |
|
System.out.println("installeDetectors:"); |
|
dumpFileTypeDetector(installeDetectors); |
|
|
|
FileTypeDetector fileTypeDetector = readStaticPrivateField(ftdClass, "defaultFileTypeDetector"); |
|
System.out.println("fileTypeDetector:"); |
|
dumpFileTypeDetector(fileTypeDetector); |
|
} |
|
|
|
private static void dumpFileTypeDetector(final List<FileTypeDetector> detectorList) throws Exception |
|
{ |
|
for (FileTypeDetector typeDetector : detectorList) { |
|
dumpFileTypeDetector(typeDetector); |
|
} |
|
} |
|
|
|
private static void dumpFileTypeDetector(final FileTypeDetector fileTypeDetector) throws Exception |
|
{ |
|
Class<? extends FileTypeDetector> ftdClass = fileTypeDetector.getClass(); |
|
if (Class.forName("sun.nio.fs.AbstractFileTypeDetector").isAssignableFrom(ftdClass) && ftdClass.isAnonymousClass()) { |
|
for (Field declaredField : ftdClass.getDeclaredFields()) { |
|
declaredField.setAccessible(true); |
|
Object fieldValue = declaredField.get(fileTypeDetector); |
|
|
|
if (fieldValue instanceof AbstractFileTypeDetector[]) { |
|
for (AbstractFileTypeDetector innerAftd : (AbstractFileTypeDetector[]) fieldValue) { |
|
dumpFileTypeDetector(innerAftd); |
|
} |
|
return; |
|
} |
|
} |
|
|
|
throw new RuntimeException("fuck"); |
|
} |
|
|
|
System.out.println(ftdClass.getName()); |
|
|
|
if (fileTypeDetector instanceof GnomeFileTypeDetector) { |
|
printPrivateProperty(ftdClass, fileTypeDetector, "gioAvailable", Boolean.class); |
|
printPrivateProperty(ftdClass, fileTypeDetector, "gnomeVfsAvailable", Boolean.class); |
|
} |
|
|
|
if (Class.forName("sun.nio.fs.MimeTypesFileTypeDetector").isAssignableFrom(ftdClass)) { |
|
printPrivateProperty(ftdClass, fileTypeDetector, "mimeTypesFile", Path.class); |
|
printPrivateProperty(ftdClass, fileTypeDetector, "loaded", Boolean.class); |
|
} |
|
|
|
if (Class.forName("sun.nio.fs.MagicFileTypeDetector").isAssignableFrom(ftdClass)) { |
|
printPrivateProperty(ftdClass, fileTypeDetector, "libmagicAvailable", Boolean.class); |
|
} |
|
} |
|
|
|
private static <T> void printStaticPrivateProperty(final Class<?> aClass, final String fieldName, final Class<T> fieldClass) throws Exception |
|
{ |
|
T value = readStaticPrivateField(aClass, fieldName); |
|
System.out.println(String.format("%s#%s: %s", aClass.getName(), fieldName, formatValue(value, fieldClass))); |
|
} |
|
|
|
private static <T> void printPrivateProperty(final Class<?> aClass, final Object instance, final String fieldName, final Class<T> fieldClass) throws Exception |
|
{ |
|
T value = readPrivateField(aClass, instance, fieldName); |
|
System.out.println(String.format("%s#%s: %s", aClass.getName(), fieldName, formatValue(value, fieldClass))); |
|
} |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
private static <T> T readStaticPrivateField(final Class<?> aClass, final String fieldName) throws Exception |
|
{ |
|
Field field = aClass.getDeclaredField(fieldName); |
|
field.setAccessible(true); |
|
return (T) field.get(null); |
|
} |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
private static <T> T readPrivateField(final Class<?> aClass, final Object instance, final String fieldName) throws Exception |
|
{ |
|
Field field = aClass.getDeclaredField(fieldName); |
|
field.setAccessible(true); |
|
return (T) field.get(instance); |
|
} |
|
|
|
private static <T> String formatValue(final T value, final Class<T> fieldClass) |
|
{ |
|
if (Boolean.class.isAssignableFrom(fieldClass)) { |
|
return ((Boolean) value).booleanValue() ? "true" : "false"; |
|
} |
|
|
|
if (Path.class.isAssignableFrom(fieldClass)) { |
|
return ((Path) value).toString(); |
|
} |
|
|
|
throw new RuntimeException(String.format("unhandled type %s", fieldClass)); |
|
} |
|
|
|
} |