`

Get field type and generic type by field name

阅读更多
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

public class FieldSpy<T> {
	public boolean[][] b = { { false, false }, { true, true } };
	public String name = "Alice";
	public List<Integer> list;
	public HashMap<String, Integer> hashmap;
	public T val;

	public static void main(String... args) {
		try {
			Class<?> c = Class.forName("FieldSpy");
			Field f = c.getField("hashmap");
			System.out.format("Type: %s%n", f.getType());
			System.out.format("GenericType: %s%n", f.getGenericType());

			// production code should handle these exceptions more gracefully
		} catch (ClassNotFoundException x) {
			x.printStackTrace();
		} catch (NoSuchFieldException x) {
			x.printStackTrace();
		}
	}
}
 
分享到:
评论

相关推荐

    advanced and generic programming in abap

    ASSIGN LargeField TO &lt;fa&gt; CASTING TYPE (SomeType = cl_abap_typedescr=&gt;describe_by_name('MY_TYPE')). ASSIGN LargeField TO &lt;fa&gt; CASTING LIKE SmallField. ASSIGN LargeField TO &lt;fa&gt; CASTING LIKE &lt;fa&gt;. ```...

    NETCFSERUP

    - &lt;member name="M:Symbol.API.GetPropertyClass(System.Type,System.Type)"&gt; &lt;summary&gt;This method can be used to obtain subclass information for nested API derived objects within an API object.&lt;/summary&gt;...

    myReflect源代码

    ParameterizedType paramType = (ParameterizedType) genericType; Type[] actualTypes = paramType.getActualTypeArguments(); // 处理实际类型参数 } ``` myReflect项目通常还会提供一些实用工具类,封装了...

    acpi控制笔记本风扇转速

    control method attempts to create 2 objects of the same name. This once again returns AE_ALREADY_EXISTS. When this exception occurs, it invokes the mechanism that will dynamically serialize the ...

    Java™ Puzzlers: Traps, Pitfalls, and Corner Cases.chm

    Puzzle 41: Field and Stream Puzzle 42: Thrown for a Loop Puzzle 43: Exceptionally Unsafe Puzzle 44: Cutting Class Puzzle 45: Exhausting Workout Chapter 6. Classy Puzzlers Puzzle 46: The Case of the ...

    kubernetes常用命令表

    $ kubectl get (-f FILENAME | TYPE NAME) [--all-namespaces] [--field-selector=FIELD_SELECTOR] [--label-selector=LABEL_SELECTOR] [--show-labels] [--show-all] [--output=OUTPUT_FORMAT] [--dry-run=bool] .....

    halcon网上学习笔记.doc

    3. open_framegrabber (Name, HorizontalResolution, VerticalResolution, ImageWidth, ImageHeight, StartRow, StartColumn, Field, BitsPerChannel, ColorSpace, Generic, ExternalTrigger, CameraType, Device, ...

    计算机网络第六版答案

    22. Five generic tasks are error control, flow control, segmentation and reassembly, multiplexing, and connection setup. Yes, these tasks can be duplicated at different layers. For example, error ...

    Halcon學習之二:攝像頭獲取圖像和相關參數

    3. Open_Framegrabber ( Name, HorizontalResolution, VerticalResolution, ImageWidth, ImageHeight, StartRow, StartColumn, Field, BitsPerChannel, ColorSpace, Generic, ExternalTrigger, CameraType, Device, ...

    HALCON函数说明

    open_framegrabber(::Name,HorizontalResolution,VerticalResolution,ImageWidth,ImageHeight,StartRow,StartColumn,Field,BitsPerChannel,ColorSpace,Generic,ExternalTrigger,CameraType,Device,Port,LineIn:...

    halcon学习资料

    3. **open_framegrabber(::Name,HorizontalResolution,VerticalResolution,ImageWidth,ImageHeight,StartRow,StartColumn,Field,BitsPerChannel,ColorSpace,Generic,ExternalTrigger,CameraType,Device,Port,LineIn:...

    java反射获取参数.zip

    - 例如,获取List的泛型类型:`Type genericType = field.getGenericType();`如果泛型是List,则可以通过检查其是否为ParameterizedType并获取实际类型参数。 7. Java 8的新特性: - 默认方法:Java 8引入了接口...

    java反射及动态代理

    - `GenericType`:表示带有类型参数的类、接口或方法的类型。 - `TypeVariable`:表示类型变量。 - `WildcardType`:表示通配符类型。 - **返回Type的方法**: - 上述方法同样适用于`java.lang.reflect`包中,...

    C#特性与枚举值遍历

    Dictionary.Add((TKey)field.GetValue(null), field.Name); } } } public class Program { static void Main(string[] args) { EnumDescriptionDictionary&lt;Colors&gt; dict = new EnumDescriptionDictionary...

    C# winform登陆框验证码的实现方法.docx

    using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; namespace BigHorseLib.Security { public class ValidCode { #region ...

    Senfore_DragDrop_v4.1

    * The name and exact version of your operating system (e.g. NT4 SP5). * The exact version of the Internet Explorer installed on your system. If you can provide me with a minimal application which ...

    c#反射机制的一种使用

    public string Field1 { get; set; } public int Field2 { get; set; } // 其他字段... } public void BindDatabaseFieldsToControls(string connectionString) { var sql = "SELECT * FROM YourTable"; using...

    servlet2.4doc

    Called by the server (via the service method) to allow a servlet to handle a GET request. doHead(HttpServletRequest, HttpServletResponse) - Method in class javax.servlet.http.HttpServlet Receives an...

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Name and Address Conversions Section 11.1. Introduction Section 11.2. Domain Name System (DNS) Section 11.3. gethostbyname Function Section 11.4. gethostbyaddr Function Section 11.5. ...

Global site tag (gtag.js) - Google Analytics