Meta classes in java

Java implementation – Meta classes

The way I understand it, Java object model is 3 levels, each level describes the level beneath it, therefore there is one Meta class shared by all Classes (which are themselves objects?).

My question is – how are constructors implemented in Java? (or any other class methods) my logic says that constructors should appear in the Meta classes, but since there is only one Meta class, it doesn’t make any sense that it keeps all possible constructors, or is my understanding of this is all wrong..

Best Solution

In Java there’s a single metaclass: the instances of the class Class are used to represent the types of classes and interfaces. The constructors are defined at the class level, not at the metaclass level.

Java – How to test a class that has private methods, fields or inner classes

Update:

Some 10 years later perhaps the best way to test a private method, or any inaccessible member, is via @Jailbreak from the Manifold framework.

@Jailbreak Foo foo = new Foo(); // Direct, *type-safe* access to *all* foo's members foo.privateMethod(x, y, z); foo.privateField = value; 

This way your code remains type-safe and readable. No design compromises, no overexposing methods and fields for the sake of tests.

If you have somewhat of a legacy Java application, and you’re not allowed to change the visibility of your methods, the best way to test private methods is to use reflection.

Читайте также:  Python process shared queue

Internally we’re using helpers to get/set private and private static variables as well as invoke private and private static methods. The following patterns will let you do pretty much anything related to the private methods and fields. Of course, you can’t change private static final variables through reflection.

Method method = TargetClass.getDeclaredMethod(methodName, argClasses); method.setAccessible(true); return method.invoke(targetObject, argObjects); 
Field field = TargetClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, value); 

Notes:
1. TargetClass.getDeclaredMethod(methodName, argClasses) lets you look into private methods. The same thing applies for getDeclaredField .
2. The setAccessible(true) is required to play around with privates.

Java – What are the differences between a HashMap and a Hashtable in Java

There are several differences between HashMap and Hashtable in Java:

  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  3. One of HashMap’s subclasses is LinkedHashMap , so in the event that you’d want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap . This wouldn’t be as easy if you were using Hashtable .

Since synchronization is not an issue for you, I’d recommend HashMap . If synchronization becomes an issue, you may also look at ConcurrentHashMap .

Related Question

Источник

Meta classes in java

A MetaClass describes a real Class with the purpose of providing to an IDE class level information, and delaying the loading of that class to the last possible moment: when an instance of the class is required.

Читайте также:  Css для аим бота

A MetaClass binds the Class object from its class name using the appropriate class loader. Once the class is bound, new instances can be created using the newInstance() method.

Constructor Summary
MetaClass (java.lang.ClassLoader classLoader, java.lang.String className)
Construct a meta class for the specified class name.
Method Summary
boolean equals (java.lang.Object object)
java.lang.ClassLoader getClassLoader ()
Get the classloader for this meta class.
java.lang.String getClassName ()
Get the fully qualified class name.
int hashCode ()
T newInstance ()
Creates a new instance of the class represented by this MetaClass object.
java.lang.Class toClass ()
Build the Class object from the class name.
java.lang.String toString ()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait

MetaClass

public MetaClass(java.lang.ClassLoader classLoader, java.lang.String className)

Parameters: classLoader — the class loader to load the class from. Must not be null. className — the fully qualified name of the class. Must not be null.

Method Detail

getClassName

public java.lang.String getClassName()

Returns: The fully qualified class name.

getClassLoader

public java.lang.ClassLoader getClassLoader()

Returns: the class loader in which this meta class must be loaded.

toClass

public java.lang.Class toClass() throws java.lang.ClassNotFoundException

Returns: The real Class object. Throws: java.lang.ClassNotFoundException — if the class was not found.

newInstance

public T newInstance() throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.ClassNotFoundException

Returns: A newly created instance. Throws: java.lang.IllegalAccessException — if the Class or its initializer is not accessible. java.lang.InstantiationException — if the Class is an abstract class, an interface, an array class, a primitive type, or void; or if the instantiation fails for some other reason. java.lang.ClassNotFoundException — if the Class is not found using the MetaClass name. java.lang.ClassCastException — if the Class is of the wrong type.

Читайте также:  Абстрактные классы и методы питон

toString

public java.lang.String toString()

hashCode


equals

public boolean equals(java.lang.Object object)

Overrides: equals in class java.lang.Object

Overview Package Class Use Tree Deprecated Index Help
Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1)

E13403-04
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Copyright © 1997, 2010, Oracle. All rights reserved.

Источник

Оцените статью