如何确定字段的未删除类型?

时间:2016-04-08 16:27:25

标签: java reflection

我有一个通用的响应包装类:

private async Task InitializeCameraAsync()
        {

            if (_mediaCapture == null)
            {

                // Get available devices for capturing pictures
                var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                // Get the desired camera by panel
                DeviceInformation cameraDevice =
                    allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null &&
                    x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);

                // If there is no camera on the specified panel, get any camera
                cameraDevice = cameraDevice ?? allVideoDevices.FirstOrDefault();

                if (cameraDevice == null)
                {
                    //ShowMessageToUser("No camera device found.");
                    Debug.WriteLine("No camera device found.");
                    return;
                }

                // Create MediaCapture and its settings
                _mediaCapture = new MediaCapture();

                // Register for a notification when video recording has reached the maximum time and when something goes wrong
                //_mediaCapture.RecordLimitationExceeded += MediaCapture_RecordLimitationExceeded;

                var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };

                // Initialize MediaCapture
                try
                {
                    await _mediaCapture.InitializeAsync(mediaInitSettings);
                    _isInitialized = true;
                }
                catch (UnauthorizedAccessException)
                {
                    //ShowMessageToUser("The app was denied access to the camera");
                    Debug.WriteLine("The app was denied access to the camera");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception when initializing MediaCapture with {0}: {1}", cameraDevice.Id, ex.ToString());
                }

                // If initialization succeeded, start the preview
                if (_isInitialized)
                {
                    // Figure out where the camera is located
                    if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
                    {
                        // No information on the location of the camera, assume it's an external camera, not integrated on the device
                        _externalCamera = true;
                    }
                    else
                    {
                        // Camera is fixed on the device
                        _externalCamera = false;

                        // Only mirror the preview if the camera is on the front panel
                        //_mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
                    }

                    //await StartPreviewAsync();

                    //UpdateCaptureControls();
                }
            }
        }

和要包装的不相关的类:

public class Response <T> {
    T response;
}

当我发出服务请求时,我得到的JSON响应类似于:

public class ServiceResponse {
    String someField;
}

现在,我所有的服务响应都有相同的外包装,即它们都有:

{ "code":200, "response":{"someField":"some text"} }

但是响应字段的实际格式/类型对于每个服务请求是不同的。当我反序列化响应时,我需要知道{ "code":200, "timestamp":"....", "response":... } 字段的类型,以便我可以创建适当的实例,如果反序列化是在response内完成的,我可以使用:

Response

但是,我在一个由反射驱动的库中完成所有这些操作,因此我通常使用以下代码反序列化整个树:

response = new T(jsonParser);

但是,此时我的parseObject方法无法正确确定T的类型。

我可以使用类似的东西:

wrapper = deserializer.parseObject(Response<ServiceResponse>.class)

然后告诉我Response<ServiceResponse> response = new Response<>(); Field field = response.getClass().getDeclaredField("response"); Type type = field.getGenericType(); 的类型为response,但我实际需要的是T

this SO question我尝试将其转换为ServiceResponse,但实际上这似乎适用于ParameterizedType类型的字段,而不是其中的实际字段(并且因为{{1}而失败}无法转换为Response<ServiceResponse>

有没有办法确定(在运行时)原始类型type

最终,我可能最终需要创建一个注释,提供有关如何反序列化字段的更多详细信息,可能通过提供执行此功能的功能,但更喜欢更透明的方法。

另一种可能性是在初始化时将实际的T实例分配给响应然后我可以从中获取实际类型...

1 个答案:

答案 0 :(得分:-1)

看看这篇文章: http://mydailyjava.blogspot.com/2013/06/advanced-java-generics-retreiving.html

它实际上完全你正在寻找的东西。

根据这一点,您只需要扩展您的Response类,然后查询其超类的泛型类型。