医助是什么工作| 念珠菌感染用什么药效果好| 全职是什么意思| 梦见别人流血是什么预兆| 为什么心率过快| apc是什么| 丑时属什么生肖| 什么是七杀命格| 过敏性皮肤用什么护肤品比较好| 风化是什么意思| 久坐脚肿是什么原因| 脂肪滴是什么意思| 白泽是什么神兽| 尿胆原阴性是什么意思| dan是什么单位| 阿司匹林是什么| 淋巴细胞数偏高是什么意思| 陈惠敏和陈慧琳什么关系| 口是心非是什么动物| 尿粘液丝高是什么原因| 骨髓移植是什么意思| 眼睛出现重影是什么原因| 属牛的和什么属相最配| 缗什么意思| 淋巴结肿大用什么药| 结膜出血用什么眼药水| 梦到捡到钱是什么预兆| 21三体高风险是什么原因造成的| 云的五行属性是什么| 内分泌失调吃什么药| 清明是什么意思| 喜欢趴着睡是什么原因| 九月二十号是什么星座| 小指麻木是什么原因| 红斑狼疮是什么| 晚上总是做梦是什么原因引起的| 单宁是什么意思| 1964年是什么年| 嘴唇发红是什么原因| 撒丫子是什么意思| 3月17日是什么星座| 绿鼻涕是什么原因| babyface是什么意思| 尿蛋白是什么原因| o型血的孩子父母是什么血型| 头自动摇摆是什么原因| 老年人头晕挂什么科| 李登辉是什么人| 什么是浅表性胃炎| IA是什么| 儿童急性肠胃炎吃什么药| 男人吃洋葱有什么好处| 精华液是干什么用的| 南无阿弥陀佛什么意思| 西铁城是什么档次的表| 孔雀蓝配什么颜色好看| 气节是什么意思| 黄瓜为什么叫黄瓜| 隆字五行属什么| 后颈长痘痘是什么原因| 异性缘是什么意思| 什么叫同工同酬| 武警和特警有什么区别| 困惑什么意思| 肠道有息肉有什么症状| 嫖娼什么意思| 闷葫芦是什么意思| 胃火旺喝什么茶| 10月11是什么星座| 上眼皮肿是什么原因| 什么的去路| 打仗是什么意思| 4月23是什么星座| 什么时候教师节| 管型偏高说明什么问题| pos什么意思| 降调针什么时候打| 腿疼是什么原因| 对应是什么意思| 白细胞满视野是什么意思| hpv通过什么传播| usd什么意思| 肺部挂什么科| 睡觉腿麻是什么原因引起| 有缘无分什么意思| 吃芒果有什么好处| 早上起床腰疼是什么原因| 脚拇指发麻是什么原因| 胃消化不良吃什么药| 1月17日是什么星座| 5点至7点是什么时辰| elite是什么意思| 择日什么意思| 法西斯是什么意思| DDP是什么| hairy什么意思| 天秤座男生喜欢什么样的女生| art是什么| 安踏属于什么档次| 什么叫三叉神经痛| 掉头发多是什么原因| 宝宝有口臭是什么原因引起的| 犄角旮旯是什么意思| 喝什么养胃| opt是什么| 吐痰带血丝是什么原因| 春暖花开是什么生肖| 腰痛应该挂什么科| 亲子鉴定需要什么样本| 年柱将星是什么意思| 强迫症吃什么药| 周文王叫什么| 巧克力有什么功效与作用| 清影是什么意思| 脂蛋白a高是什么意思| 鞭尸什么意思| 榴莲树长什么样子图片| 来月经量少吃什么可以增加月经量| 水痘要注意什么| 虫字旁与什么有关| 脸部过敏红痒抹什么药| 什么叫尿潴留| 紫颠是什么病怎样治| 缺钾吃什么食物| 锐减是什么意思| 保鲜卡是什么原理纸片| 血糖高适合喝什么酒| 后背长痘是什么原因| 成都有什么区| 为什么会血压高| 抽烟为什么会上瘾| 69年属什么生肖| 男大三后面一句是什么| 一什么帽子| 副鼻窦炎症是什么意思| 处理是什么意思| 苹果什么季节成熟| na是什么| 抽筋吃什么药| 办港澳通行证需要带什么| 北极熊为什么不怕冷| 发膜什么牌子效果最好| gmp什么意思| 雪燕有什么功效| 项羽姓什么| 鲁迅字什么| 脾胃不和吃什么药| 网是什么结构的字| 老年人心跳过快是什么原因| vivo是什么牌子的手机| 什么是低血糖| 贫血严重会得什么病| 镜花缘是什么意思| 妊娠反应什么时候开始| 朝阳是什么意思| od值是什么意思| 内角是什么意思| 橄榄是什么| 间质性肺炎是什么意思| 勃艮第红是什么颜色| 隙是什么意思| 喉咙扁桃体发炎吃什么药| 肌酸激酶偏低是什么原因| 什么东西清肺止咳| 心率过缓有什么危害| 什么是直径| 手机流量是什么| tommyhilfiger什么牌子| 嘴巴里面起泡是什么原因引起的| 婆婆是什么意思| 飞黄腾达是什么生肖| 相得益彰什么意思| 为什么要穿内裤| 心包积液吃什么药| 等闲识得东风面下一句是什么| 耳鸣什么原因| 好男儿志在四方是什么生肖| 带蜜蜡有什么好处| 什么生肖晚上不睡觉| 出现血尿是什么原因| 2026属什么生肖| 咽炎吃什么药最管用| ivu是什么检查| 8.1是什么星座| 梦见系鞋带是什么意思| 在吗是什么意思| 寓言故事有什么特点| 十一月八号是什么星座| 为什么会气虚| 焦糖色是什么| 白头翁是什么生肖| 取环需要做什么检查| 什么是十字花科蔬菜| 孽债是什么意思| 甲亢是什么| 尿液检查白细胞高是什么原因| 墨绿色是什么颜色| 7月11是什么星座| 精血亏虚吃什么中成药| 鸡胸是什么| 松塔有什么用| 耳鸣吃什么药好| obsidian什么意思| 小狗肚子里有虫子吃什么药| 头晕挂什么科室| 中元节出什么生肖| 怀孕的最佳时间是什么时候| 羁什么意思| 软装是什么| 三尖瓣反流是什么意思| co2cp在医学上是什么| 为什么没有西京| 丑角是什么意思| 成家是什么意思| 脚背疼是什么原因| 鼻子有臭味是什么原因| 双子座上升星座是什么| o型阴性血是什么意思| 什么茶减肥效果最好| 丸美属于什么档次| 不举是什么原因造成的| 吃什么容易拉肚子| 石钟乳是什么意思| 红领巾的含义是什么| 女人梦见好多蛇是什么预兆| 乙肝两对半挂什么科| 只出不进什么意思| 腔隙性脑梗死吃什么药| 农历七月十五是什么节| 元帅相当于现在什么官| 腹泻期间宜吃什么食物| 蜜枣是什么枣做的| 胡萝卜与什么食物相克| 什么是对偶句| 苏打和小苏打有什么区别| 菩提什么意思| 第一次坐飞机注意什么| 下眼睑肿胀是什么原因| 喝茶叶有什么好处| 率性是什么意思| 什么是rpa| 悬脉是什么意思| 什么样的伤口需要缝针| 私生是什么意思| 睾酮高有什么影响| 梨子什么时候成熟| 石楠花什么味道| 沉香对人体有什么好处| 牙疼吃什么药止疼最快| 受用是什么意思| 喝酒眼睛红是什么原因| 跳蚤喜欢咬什么样的人| 2043年是什么年| 势如破竹什么意思| 脸上长白斑是什么原因引起的| c是什么| 性格内敛是什么意思| 感冒吃什么菜比较好| 明天是什么日子| 散粉是干什么用的| 得不到的永远在骚动什么意思| 弱碱性水是什么水| 秋葵有什么好处| 什么是双| 百度

Docker??????Rancher??????????????????????

百度 ”对于党的各级领导干部来说,治省、治市、治县乃至治镇、治村,都应当有这种精神,不懈怠、不马虎,夙夜在公、勤勉工作。

In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.[1]

Historical background

edit

The earliest computers were programmed in their native assembly languages, which were inherently reflective, as these original architectures could be programmed by defining instructions as data and using self-modifying code. As the bulk of programming moved to higher-level compiled languages such as ALGOL, COBOL, Fortran, Pascal, and C, this reflective ability largely disappeared until new programming languages with reflection built into their type systems appeared.[citation needed]

Brian Cantwell Smith's 1982 doctoral dissertation introduced the notion of computational reflection in procedural programming languages and the notion of the meta-circular interpreter as a component of 3-Lisp.[2][3]

Uses

edit

Reflection helps programmers make generic software libraries to display data, process different formats of data, perform serialization and deserialization of data for communication, or do bundling and unbundling of data for containers or bursts of communication.

Effective use of reflection almost always requires a plan: A design framework, encoding description, object library, a map of a database or entity relations.

Reflection makes a language more suited to network-oriented code. For example, it assists languages such as Java to operate well in networks by enabling libraries for serialization, bundling and varying data formats. Languages without reflection such as C are required to use auxiliary compilers for tasks like Abstract Syntax Notation to produce code for serialization and bundling.

Reflection can be used for observing and modifying program execution at runtime. A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal of that enclosure. This is typically accomplished by dynamically assigning program code at runtime.

In object-oriented programming languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.

Reflection is often used as part of software testing, such as for the runtime creation/instantiation of mock objects.

Reflection is also a key strategy for metaprogramming.

In some object-oriented programming languages such as C# and Java, reflection can be used to bypass member accessibility rules. For C#-properties this can be achieved by writing directly onto the (usually invisible) backing field of a non-public property. It is also possible to find non-public methods of classes and types and manually invoke them. This works for project-internal files as well as external libraries such as .NET's assemblies and Java's archives.

Implementation

edit

A language that supports reflection provides a number of features available at runtime that would otherwise be difficult to accomplish in a lower-level language. Some of these features are the abilities to:

  • Discover and modify source-code constructions (such as code blocks, classes, methods, protocols, etc.) as first-class objects at runtime.
  • Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.
  • Evaluate a string as if it were a source-code statement at runtime.
  • Create a new interpreter for the language's bytecode to give a new meaning or purpose for a programming construct.

These features can be implemented in different ways. In MOO, reflection forms a natural part of everyday programming idiom. When verbs (methods) are called, various variables such as verb (the name of the verb being called) and this (the object on which the verb is called) are populated to give the context of the call. Security is typically managed by accessing the caller stack programmatically: Since callers() is a list of the methods by which the current verb was eventually called, performing tests on callers()[0] (the command invoked by the original user) allows the verb to protect itself against unauthorised use.

Compiled languages rely on their runtime system to provide information about the source code. A compiled Objective-C executable, for example, records the names of all methods in a block of the executable, providing a table to correspond these with the underlying methods (or selectors for these methods) compiled into the program. In a compiled language that supports runtime creation of functions, such as Common Lisp, the runtime environment must include a compiler or an interpreter.

Reflection can be implemented for languages without built-in reflection by using a program transformation system to define automated source-code changes.

Security considerations

edit

Reflection may allow a user to create unexpected control flow paths through an application, potentially bypassing security measures. This may be exploited by attackers.[4] Historical vulnerabilities in Java caused by unsafe reflection allowed code retrieved from potentially untrusted remote machines to break out of the Java sandbox security mechanism. A large scale study of 120 Java vulnerabilities in 2013 concluded that unsafe reflection is the most common vulnerability in Java, though not the most exploited.[5]

Examples

edit

The following code snippets create an instance foo of class Foo and invoke its method PrintHello. For each programming language, normal and reflection-based call sequences are shown.

Common Lisp

edit

The following is an example in Common Lisp using the Common Lisp Object System:

(defclass foo () ())
(defmethod print-hello ((f foo)) (format T "Hello from ~S~%" f))

;; Normal, without reflection
(let ((foo (make-instance 'foo)))
  (print-hello foo))

;; With reflection to look up the class named "foo" and the method
;; named "print-hello" that specializes on "foo".
(let* ((foo-class (find-class (read-from-string "foo")))
       (print-hello-method (find-method (symbol-function (read-from-string "print-hello"))
                                        nil (list foo-class))))
  (funcall (sb-mop:method-generic-function print-hello-method)
           (make-instance foo-class)))

The following is an example in C++.

import std;

using std::meta::info;
using std::views::filter;

consteval bool is_nonstatic_method_fn(info mem) {
    return is_class_member(mem) 
        && !is_static_member(mem)
        && is_function(mem);
}

consteval info find_method(info ty, const char* name) {
    constexpr auto ctx = std::meta::access_context::current();
    for (info mem : members_of(ty, ctx) | filter(is_nonstatic_method_fn))
        if (identifier_of(mem) == name)
            return mem;
    return info{};
}

template <info Ty, auto Name>
constexpr auto create_invoker_impl = []{
    using Type = [: Ty :];
    static constexpr info M = find_method(Ty, Name);
    static_assert(parameters_of(M).size() == 0 && return_type_of(M) == ^^void);
    return [](Type& instance) { instance.[: M :](); };
}();

consteval info create_invoker(info ty, std::string_view name) {
    return substitute(^^create_invoker_impl, {
        std::meta::reflect_constant(ty), 
        std::meta::reflect_constant_string(name)});
}

int main(int argc, char* argv[]) {
    Foo foo;

    // Without reflection
    foo.printHello();

    // With reflection
    auto invokePrint = [: create_invoker(^^Foo, "printHello") :];
    invokePrint(foo);

    return 0;
}

The following is an example in C#:

// Without reflection
var foo = new Foo();
foo.PrintHello();

// With reflection
Object foo = Activator.CreateInstance("complete.classpath.and.Foo");
MethodInfo method = foo.GetType().GetMethod("PrintHello");
method.Invoke(foo, null);

Delphi, Object Pascal

edit

This Delphi and Object Pascal example assumes that a TFoo class has been declared in a unit called Unit1:

uses RTTI, Unit1;

procedure WithoutReflection;
var
  Foo: TFoo;
begin
  Foo := TFoo.Create;
  try
    Foo.Hello;
  finally
    Foo.Free;
  end;
end;

procedure WithReflection;
var
  RttiContext: TRttiContext;
  RttiType: TRttiInstanceType;
  Foo: TObject;
begin
  RttiType := RttiContext.FindType('Unit1.TFoo') as TRttiInstanceType;
  Foo := RttiType.GetMethod('Create').Invoke(RttiType.MetaclassType, []).AsObject;
  try
    RttiType.GetMethod('Hello').Invoke(Foo, []);
  finally
    Foo.Free;
  end;
end;

The following is an example in eC:

// Without reflection
Foo foo { };
foo.hello();

// With reflection
Class fooClass = eSystem_FindClass(__thisModule, "Foo");
Instance foo = eInstance_New(fooClass);
Method m = eClass_FindMethod(fooClass, "hello", fooClass.module);
((void (*)())(void *)m.function)(foo);

The following is an example in Go:

import "reflect"

// Without reflection
f := Foo{}
f.Hello()

// With reflection
fT := reflect.TypeOf(Foo{})
fV := reflect.New(fT)

m := fV.MethodByName("Hello")
if m.IsValid() {
    m.Call(nil)
}

Java

edit

The following is an example in Java:

import java.lang.reflect.Method;

// Without reflection
Foo foo = new Foo();
foo.hello();

// With reflection
try {
    Object foo = Foo.class.getDeclaredConstructor().newInstance();

    Method m = foo.getClass().getDeclaredMethod("hello", new Class<?>[0]);
    m.invoke(foo);
} catch (ReflectiveOperationException ignored) {}

JavaScript/TypeScript

edit

The following is an example in JavaScript:

// Without reflection
const foo = new Foo();
foo.hello();

// With reflection
const foo = Reflect.construct(Foo);
const hello = Reflect.get(foo, 'hello');
Reflect.apply(hello, foo, []);

// With eval
eval('new Foo().hello()');

The following is the same example in TypeScript:

// Without reflection
const foo: Foo = new Foo();
foo.hello();

// With reflection
const foo: Foo = Reflect.construct(Foo);
const hello: (this: Foo) => void = Reflect.get(foo, 'hello') as (this: Foo) => void;
Reflect.apply(hello, foo, []);

// With eval
eval('new Foo().hello()');

Julia

edit

The following is an example in Julia:

julia> struct Point
           x::Int
           y
       end

# Inspection with reflection
julia> fieldnames(Point)
(:x, :y)

julia> fieldtypes(Point)
(Int64, Any)

julia> p = Point(3,4)

# Access with reflection
julia> getfield(p, :x)
3

Objective-C

edit

The following is an example in Objective-C, implying either the OpenStep or Foundation Kit framework is used:

// Foo class.
@interface Foo : NSObject
- (void)hello;
@end

// Sending "hello" to a Foo instance without reflection.
Foo *obj = [[Foo alloc] init];
[obj hello];

// Sending "hello" to a Foo instance with reflection.
id obj = [[NSClassFromString(@"Foo") alloc] init];
[obj performSelector: @selector(hello)];

Perl

edit

The following is an example in Perl:

# Without reflection
my $foo = Foo->new;
$foo->hello;

# or
Foo->new->hello;

# With reflection
my $class = "Foo"
my $constructor = "new";
my $method = "hello";

my $f = $class->$constructor;
$f->$method;

# or
$class->$constructor->$method;

# with eval
eval "new Foo->hello;";

The following is an example in PHP:[6]

// Without reflection
$foo = new Foo();
$foo->hello();

// With reflection, using Reflections API
$reflector = new ReflectionClass("Foo");
$foo = $reflector->newInstance();
$hello = $reflector->getMethod("hello");
$hello->invoke($foo);

Python

edit

The following is an example in Python:

# Without reflection
obj = Foo()
obj.hello()

# With reflection
obj = globals()["Foo"]()
getattr(obj, "hello")()

# With eval
eval("Foo().hello()")

The following is an example in R:

# Without reflection, assuming foo() returns an S3-type object that has method "hello"
obj <- foo()
hello(obj)

# With reflection
class_name <- "foo"
generic_having_foo_method <- "hello"
obj <- do.call(class_name, list())
do.call(generic_having_foo_method, alist(obj))

Ruby

edit

The following is an example in Ruby:

# Without reflection
obj = Foo.new
obj.hello

# With reflection
obj = Object.const_get("Foo").new
obj.send :hello

# With eval
eval "Foo.new.hello"

Xojo

edit

The following is an example using Xojo:

' Without reflection
Dim fooInstance As New Foo
fooInstance.PrintHello

' With reflection
Dim classInfo As Introspection.Typeinfo = GetTypeInfo(Foo)
Dim constructors() As Introspection.ConstructorInfo = classInfo.GetConstructors
Dim fooInstance As Foo = constructors(0).Invoke
Dim methods() As Introspection.MethodInfo = classInfo.GetMethods
For Each m As Introspection.MethodInfo In methods
  If m.Name = "PrintHello" Then
    m.Invoke(fooInstance)
  End If
Next

See also

edit

References

edit

Citations

edit
  1. ^ A Tutorial on Behavioral Reflection and its Implementation by Jacques Malenfant et al. (PDF), unknown, archived from the original (PDF) on 21 August 2017, retrieved 23 June 2019
  2. ^ Brian Cantwell Smith, Procedural Reflection in Programming Languages, Department of Electrical Engineering and Computer Science, Massachusetts Institute of Technology, PhD dissertation, 1982.
  3. ^ Brian C. Smith. Reflection and semantics in a procedural language Archived 2025-08-05 at the Wayback Machine. Technical Report MIT-LCS-TR-272, Massachusetts Institute of Technology, Cambridge, Massachusetts, January 1982.
  4. ^ Barros, Paulo; Just, René; Millstein, Suzanne; Vines, Paul; Dietl, Werner; d'Amorim, Marcelo; Ernst, Michael D. (August 2015). Static Analysis of Implicit Control Flow: Resolving Java Reflection and Android Intents (PDF) (Report). University of Washington. UW-CSE-15-08-01. Retrieved October 7, 2021.
  5. ^ Eauvidoum, Ieu; disk noise (October 5, 2021). "Twenty years of Escaping the Java Sandbox". Phrack. Vol. 10, no. 46. Retrieved October 7, 2021.
  6. ^ "PHP: ReflectionClass - Manual". www.php.net.

Sources

edit

Further reading

edit
  • Ira R. Forman and Nate Forman, Java Reflection in Action (2005), ISBN 1-932394-18-4
  • Ira R. Forman and Scott Danforth, Putting Metaclasses to Work (1999), ISBN 0-201-43305-2
edit
颈椎病用什么药最好 田七煲汤配什么材料 白蜡金命五行缺什么 浑浑噩噩是什么意思 龟公是什么意思
为什么放生泥鳅果报大 脚烧是什么原因 不什么不什么的成语 白细胞高有什么危害 来姨妈喝什么比较好
银屑病用什么药最好 孕妇梦见下雨是什么意思 有什么好看的古装剧 作践自己是什么意思 甲减长期服用优甲乐有什么危害
为什么吃芒果会过敏 什么是高潮 思春是什么意思啊 什么津乐道 早上五点是什么时辰
子宫内膜息肉吃什么药hcv8jop0ns1r.cn 怀孕吃什么水果好对胎儿好hcv9jop5ns4r.cn 秋千为什么叫秋千hcv8jop2ns9r.cn 诺贝尔奖为什么没有数学奖hcv8jop0ns9r.cn 湿疹长什么样子hcv7jop4ns5r.cn
博士点是什么意思hcv7jop4ns8r.cn 什么程度算节食减肥hcv9jop3ns7r.cn 腹部超声检查什么hcv7jop7ns2r.cn 用盐泡脚有什么好处hcv9jop4ns0r.cn 考试前吃什么hcv7jop6ns9r.cn
梦见妯娌是什么意思bfb118.com 支气管炎是什么原因引起的hcv9jop6ns2r.cn 肾脏不好吃什么食物好hcv8jop9ns1r.cn 毕业答辩是什么hcv9jop8ns2r.cn 经常扁桃体发炎是什么原因hcv8jop4ns9r.cn
言外之意是什么意思hcv8jop7ns1r.cn 发票抬头写什么hcv8jop0ns0r.cn lfc是什么意思hcv9jop2ns5r.cn 什么的废墟mmeoe.com 什么叫家hcv7jop9ns0r.cn
百度