Field 1 Reflection



  1. Field 1 Reflections
  2. Field 1 Reflection 1
  3. Field 1 Reflection Meaning
  4. Field 1 Reflection Worksheets

Java Reflection provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions. In the fields portion of these results, enum constants are listed. While these are technically fields, it might be useful to distinguish them from other fields. This example could be modified to use java.lang.reflect.Field.isEnumConstant for this purpose. Hofer 1 My Field Experience Reflection: Stephanie Hofer Introduction to Teaching 111 Professor Eastman. Hofer 2 My Field Experience Reflection After concluding my field experience at Shawnee Middle School, I feel more enthusiastic about my decision to become an educator. While observing students in the classroom setting, I. Remarks The field information is obtained from metadata.FieldInfo does not have a public constructor.FieldInfo objects are obtained by calling either the Type.GetFields or Type.GetField method of a Type object. Fields are variables defined in the class.FieldInfo provides access to the metadata for a field within a class and provides dynamic set and get functionality for the field.

Field 1 Reflections


License : Copyright Emeric Nasi, some rights reserved
This work is licensed under a Creative Commons Attribution 4.0 International License.

I. Reflection and Java security

For most Java developers, Java security comes from the use of keywords such as 'private, protected, or final'.
For example a field declared :
private static String state;
The field called 'state' is a class variable, with the given keywords, it should only be accessible by other instance objects of the same class.

Another example :
private final String name='MyClass';

In this example the field 'name' can only be accessed by another code in the same object, and it has the 'final' keyword so it cannot be modified once it is set (a real Java constant has both the keywords 'static' and 'final').

These examples shows that Java data access security is guaranteed by the language keywords, however this statement is not true because of Java 'reflection'.
Reflection is a Java feature that allows a code to examine itself dynamically and modify its references and properties at runtime.
Reflection is a direct part of the Java language. The reflection package is java.lang.reflect. This package provides objects that can be used to list any fields inside a class, invoke any class methods or access and modify any fields, and when I say any I mean all of them, even private ones.

II. Use reflection to modify any class/object field.

II.1 Our scope

The java.lang.reflect classes allows you do do a lot more things than just access to class fields. This is just a little part of the enormous possibilities of reflection, the goal here is to show that reflection can 'break' classic keyword security.
The only include that is needed for this article codes is :

II.2 Access and modify any class variables

A class variable is a variable that is common to all instances of this class, it is defined with the 'static' keyword.
The code below can be used to access any class variable :

In this code, the essential line is 'field.setAccessible(true);'. The setAccessible() method disables usual languages security checks and allows the field to be accessed from any other class.
Note also the getDeclaredField() method that is really useful and allows you to access via reflection to a Field object representing any field that is declared inside a Class.
We saw we could grab any field value, but can we also modify them? the answer is yes. For that you can use the next code :

Here, the Field.get() method is used to grab the object contained in the field. The set() method overrides this object with another Object (newValue) we passed in parameters.

II.3 Access and modify any instance variables

In the previous section we saw that we could access and modify any static field. We can do the same thing for non-static fields that is, instance variables. The example codes works basically like the previous ones except that instead of manipulating a Class, we are going to manipulate an Object, instance of the Class.

III. Prevent reflection

Reflection is a very powerful feature and a lot of Java frameworks use it. However, an 'evil' code can also use it to break the security of your application and access and modify any field and a lot more things (invoke any methods, list all class content, etc).
The only way to prevent reflection is to use a securitymanager. By default code using reflection needs particular security permissions.
In our example, both the getDeclaredField() method and the setAccessible() method contain an inner security check that will throw a SecurityException if called by a code that is not authorized by the securitymanager.
If you want to look at some example of securitymanager that authorizes reflection for particular jars you should read the article : Spring and Hibernate Tomcat security manager.

IV. Example : Disable Java security

The best example to show that reflection is a dangerous feature is to show that all the security mechanisms of Java, relying on the security manager, can be disabled using a single call to the setStaticValue() method previously described in this article.
The entire language security of Java relies on security checks using a static value inside the java.lang.System class.
The usual security checks maid by sensible functions are :

  • Test if a call to System.getSecurityManager() returns null.
  • If returned value is null, the function considers security is deactivated.
  • If the return value is not null, the SecurityManager element returned by the function is used to know what is authorized and what is not.

The System.getSecurityManager() method simply returns the value of a static private variable of the System class, security.
So, that means, disabling Java language security is as simple as setting the security variable to null.

Thats all!

Also in this section

8 January 2011 – Enable securitymanager for Spring and Hibernate

10 November 2010 – Implement hash service using JCE

4 Forum posts

  • How can you possibly disable all security by calling

    setStaticValue('java.lang.System', 'security', null);

    when this method itself uses calls to the reflection api which will be prevented by the SecurityManager ( throw SecurityException) before you manage to set it to null?

    • “show that reflection is a dangerous feature” The point was to explain that if reflection API is enabled, the security of Java can be disabled.
      That is why when enabling Java security you must ensure that you do not allow reflection, especially on modules your are not sure you can trust.

  • how to change the order of the private data variable?

Any message or comments?
Next:Wave-guides Up:Electromagnetic radiation Previous:Dielectric constant of a

Reflection at a dielectric boundary

An electromagnetic wave of real (positive) frequency can be written

The wave-vector, , indicates the direction of propagation of thewave, and also its phase-velocity, , via
(1212)

Since the wave is transverse in nature, we must have. Finally, the familiar Maxwellequation
leads us to the following relation between the constant vectors and :
(1214)

Here, is a unit vector pointing in thedirection of wave propagation.

Suppose that the plane forms the boundary between two different dielectricmedia. Let medium 1, of refractive index , occupy the region ,whilst medium 2, of refractive index , occupies the region .Let us investigate what happens when an electromagnetic wave is incidenton this boundary from medium 1.

Consider, first of all, the simple case of incidence normalto the boundary (see Fig. 55). In this case, for theincident and transmitted waves, and for the reflected wave. Without loss of generality, we can assume thatthe incident wave is polarized in the -direction.Hence, using Eq. (1214), the incidentwave can be written

(1215)
(1216)

where is the phase-velocity in medium 1, and . Likewise, the reflected wave takes the form

Finally, the transmitted wave can be writtenReflection
(1219)
(1220)

where is the phase-velocity in medium 2, and .

For the case of normal incidence, the electric and magneticcomponents of all three waves are parallel to the boundary betweenthe two dielectric media. Hence, the appropriate boundary conditionsto apply at are


The latter condition derives from the general boundary condition, and the fact that in both media (which are assumed to be non-magnetic).

Application of the boundary condition yields

(1223)

Likewise, application of the boundary condition (1222) gives
or
(1225)

since .Equations (1223) and (1225) can be solved to give

Thus, we have determined the amplitudes of the reflected and transmittedwaves in terms of the amplitude of the incident wave.

It can be seen, first of all, that if then and .In other words, if the two media have the same indices of refraction thenthere is no reflection at the boundary between them, and the transmittedwave is consequently equal in amplitude to the incident wave. On the otherhand, if then there is some reflection at the boundary. Indeed,the amplitude of the reflected wave is roughly proportional to the difference between and . This has important practical consequences.We can only see a clean pane of glass in a window because some of the light incidentat an air/glass boundary is reflected, due to the different refractive indiciesof air and glass. As is well-known, it is a lot more difficult to see glass when it is submerged in water. This is because the refractive indices of glass and water are quite similar, and so there is very little reflection of lightincident on a water/glass boundary.

According to Eq. (1226), when . The negative sign indicates a phase-shift of the reflected wave, withrespect to the incident wave. We conclude that there is a phase-shift of the reflected wave, relative to the incident wave, on reflection from a boundary with amedium of greater refractive index. Conversely, there is no phase-shifton reflection from a boundary with a medium of lesser refractive index.

The mean electromagnetic energy flux, or intensity, in the -direction is simply

(1228)

The coefficient of reflection, , is defined as the ratioof the intensities of the reflected and incident waves:
Likewise, the coefficient of transmission, , is the ratio ofthe intensities of the transmitted and incident waves:
(1230)

Equations (1226), (1227), (1229), and (1230)yield

Note that . In other words, any wave energy which is not reflectedat the boundary is transmitted, and vice versa.
Figure 56:

Let us now consider the case of incidence oblique to the boundary (see Fig. 56).Suppose that the incident wave subtends an angle with thenormal to the boundary, whereas the reflected and transmittedwaves subtend angles and , respectively.

The incident wave can be written


with analogous expressions for the reflected and transmitted waves.Since, in the case of oblique incidence, the electric and magneticcomponents of the wave fields are no longer necessarily parallel to theboundary, the boundary conditions (1221) and (1222) at mustbe supplemented by the additional boundary conditions
(1235)
(1236)

Equation (1235) derives from the generalboundary condition .

It follows from Eqs. (1222) and (1236) that both componentsof the magnetic field are continuous at the boundary. Hence, we can write

at . Given that , , and areconstant vectors, the only way in which the above equation can be satisfied for allvalues of and is if
(1238)

throughout the plane. This, in turn, implies that
and
(1240)
Field 1 reflection 1
It immediately follows that if then .In other words, if the incident wave lies in the - plane then thereflected and transmitted waves also lie in the - plane. Anotherway of putting this is that the incident, reflected, and transmittedwaves all lie in the same plane, know as the planeof incidence. This, of course, is one of the laws of geometric optics.From now on, we shall assume that the plane of incidence is the -plane.

Now, and . Moreover,

with similar expressions for and . Hence, accordingto Eq. (1239),
(1242)

which implies that . Moreover,
Of course, the above expressions correspond to the law of reflectionand Snell's law of refraction, respectively.Field 1 reflection notes

For the case of oblique incidence, we need to consider two independentwave polarizations separately. The first polarizationhas all the wave electric fields perpendicular to the plane of incidence, whilstthe second has all the wave magnetic fields perpendicular to the planeof incidence.

Let us consider the first wave polarization. We can write unit vectorsin the directions of propagation of the incident, reflected, and transmittedwaves likso:

(1244)
(1245)
(1246)

The constant vectors associated with the incident wave are written

where use has been made of Eq. (1214). Likewise, the constantvectors associated with the reflected and transmitted waves are
(1249)
(1250)

and

respectively.

Now, the boundary condition (1221) yields ,or

(1253)

Likewise, the boundary condition (1236) gives ,or
However, using Snell's law, (1243), the above expression reduces to Eq. (1253). Finally, the boundary condition (1222) yields, or
(1255)

It is convenient to define the parameters

and
(1257)

Equations (1253) and (1255) can be solved in termsof these parameters to give

These relations are known as Fresnel equations.

The wave intensity in the -direction is given by

(1260)

Hence, the coefficient of reflection is written
whereas the coefficient of transmission takes the form
(1262)

Note that it is again the case that .

Let us now consider the second wave polarization. In this case, theconstant vectors associated with the incident, reflected, and transmittedwaves are written


and
(1265)
(1266)

and

respectively.The boundary condition (1222) yields , or
(1269)

Likewise, the boundary condition (1221) gives , or
Finally, the boundary condition (1235) yields , or

Field 1 Reflection 1

(1271)

Making use of Snell's law, and the fact that , theabove expression reduces to Eq. (1269).

Solving Eqs. (1239) and (1270), we obtain


The associated coefficients of reflection and transmission take theform
(1274)
(1275)

respectively. As usual, .

Note that at oblique incidence the Fresnel equations, (1258) and(1259), for the wave polarization in which the electricfield is parallel to the boundary are different to the Fresnel equations,(1272) and (1273), for the wave polarizationin which the magnetic field is parallel to the boundary. This implies thatthe coefficients of reflection and transmission for these two wave polarizationsare, in general, different.

Figure 57 shows the coefficients of reflection (solid curves) and transmission(dashed curves) for oblique incidence from air () toglass (). The left-hand panel shows the wave polarizationfor which the electric field is parallel to the boundary, whereas theright-hand panel shows the wave polarization for which themagnetic field is parallel to the boundary. In general, it canbe seen that the coefficient of reflection rises, and the coefficient oftransmission falls, as the angle of incidence increases. Note, however,that for the second wave polarization there is a particular angle of incidence,know as the Brewster angle,at which the reflected intensity is zero. There is no similar behaviour forthe first wave polarization.

It follows from Eq. (1272) that the Brewster angle correspondsto the condition

(1276)

or
where use has been made of Snell's law. The above expressionreduces to
(1278)

or . Hence, the Brewster angle satisfies

Field 1 Reflection Meaning


If unpolarized light is incident on an air/glass (say) boundary at the Brewster anglethen the reflected beam is plane polarized.
Next:Wave-guides Up:Electromagnetic radiation Previous:

Field 1 Reflection Worksheets

Dielectric constant of aRichard Fitzpatrick2006-02-02