スキップしてメイン コンテンツに移動

投稿

ラベル(Reflection)が付いた投稿を表示しています

Java: Get All Static Fields Defined in Class by Reflection

If you want to get all static fields in a class, use code snippet below. The key part is "Modifier.isStatic", "getDeclaredFields" methods package com.dukesoftware.reflection; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ReflectionUtils { public static List<Field> getStaticFields(Class<?> clazz) { Field[] declaredFields = clazz.getDeclaredFields(); return Arrays.stream(declaredFields) .filter(field -> Modifier.isStatic(field.getModifiers())) .collect(Collectors.toList()) ; } }

Java Reflection: Getter and Setter Method

Reflective Getter and Setter Method In this post, I will show you Java code for getting getter and setter method from given object. The code is below: public final static Method getSetterMethod(Object o, String propertyName, Class<?> paramterType) throws SecurityException, NoSuchMethodException{ return o.getClass().getMethod("set"+toUpperFirstChar(propertyName), paramterType); } public final static Method getGetterMethod(Object o, String propertyName) throws SecurityException, NoSuchMethodException{ return o.getClass().getMethod("get"+toUpperFirstChar(propertyName)); } public final static String toUpperFirstChar(String str){ if(str.isEmpty()) return ""; return str.substring(0, 1).toUpperCase()+str.substring(1, str.length()); } Where to Use Reflective Getter and Setter? You may have a question - "Where should we use reflective getter and setter method?" If you already know where to use them, you can skip the fo

C#: Reflection Tips

C# is a very powerful language. However for this "powerful" perspective, there are various way to achieve something and you might hover among which way to take (at least for me :D). In this post I will focus on "Reflection" and introduce some small code spinets. First we assume this trivial class is defined in Utility assembly. namespace Utility.Sample { public class Target { public string wrapByDoubleQuote(string text) { return "\"" + text + "\""; } } } Get Type from String and Instantiate by Activator // type from reflection Type type = Type.GetType("Utility.Sample.Target"); // instantiate object from Type Target target = Activator.CreateInstance(type) as Target; // invoke method normally Console.WriteLine(target.wrapByDoubleQuote("contents")); Instantiate Object from ConstructorInfo and Invoke Method by Reflection // type from reflectio

ActionScript3.0: Reflection Example

I introduce some tips for reflection (not reflection of image but "programatic") of ActionScript 3.0. getDefinitionByName If you would like to Class object getDefinitionByName function should help. var c:Class = getDefinitionByName("flash.display.Sprite") as Class; If you would like to know details information of Class you can use describeType , which returns class information as Xml format. Reflective Class Instantiation import flash.utils.getDefinitionByName; public class Instantiator { private var classRef:Class; public function Instantiator(className:String) { this.classRef = getDefinitionByName(className) as Class; } public function newInstance(...args):Object { if(args.length == 0){ return new classRef(); } else { return new classRef(args); } } } Example usage: import flash.display.Sprite; import seedion.io.XMLExporter; // class will be instantiated at line with (*) import utils.tool.Instantiat

Java: Reflection Example: Get All Static Field Names in Class

This post is just as I wrote in the title - "get all static field names in class". I think you can easily imagine how to get other similar information by reflection... public final static Collection<String> getStaticFieldNames(Class<?> classObj){ ArrayList<String> list = new ArrayList<String>(); Field[] fields = classObj.getFields(); for(Field field: fields){ if(Modifier.isStatic(field.getModifiers())){ list.add(field.getName()); } } return list; }