Java.lang - SPLessons

Java.lang Compiler Class

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Compiler Class

Java.lang Compiler

shape Description

The compiler is used to compile the source code from bytecode, bytecode is nothing but it will have set of instructions that will be in the encrypted format no one cannot understand the format. As everyone knows the Java language is portable means that user can carry the class file to execute on another platform. This class gives the support native code compilers also. Following is the syntax of this class. [java] public final class Compiler extends Object[/java] This class will inherit all the methods from java.lang.object. The java.lang.Object class is the root for every Java class, this class will have various methods and automatically all methods will be available to all Java classes. In case the class is extending any other class then it becomes the indirect subclass of the object otherwise, it becomes direct subclass of object.

Methods Of java.lang Compiler Class

shape Table

Methods Description
command(Object) It inspects the parameter type and its fields and play out some reported operation.
compileClass(Class) To compile the given class
compileClasses(String) It compiles all classes whose name coordinates the given string.
disable() To stop the operation.
enable() To resume the operation.

shape Example - 1

Following is an example for the above mentioned methods.

By Using java.lang.Compiler.command()

CompilerDemo.java [java] package com.SPlessons; import java.lang.*; public class CompilerDemo { public static void main(String[] args) { CompilerDemo cls = new CompilerDemo(); CompilerDemo subcls = new SubClass1(); // class CompilerDemo Class c = cls.getClass(); System.out.println(c); // sub class SubClass1 Class c1 = subcls.getClass(); System.out.println(c1); /* Let's compile CompilerDemo class using command method */ Object retval = Compiler.command("javac CompilerDemo"); System.out.println("Return Value = " + retval); } } class SubClass1 extends CompilerDemo { // sub class } [/java] Here the program is going to compile by using a command which specified in the code(javac CompilerDemo) that as follows. [java]Object retval = Compiler.command("javac CompilerDemo");[/java] As discussed in the above description java.lang Compiler Class uses the methods of java.lang.Object.getClass(), getClass() is the method of Object class that is used to return the run time class of an object. Output: Now compile the code result will be as follows. [java] class com.SPlessons.CompilerDemo class com.SPlessons.SubClass1 Return Value = null [/java]

By Using java.lang.Compiler.compileClass()

Now instead of the command line code place the boolean retval = Compiler.compileClass(c1);. Output: Following is the result will be displayed. [java] class com.SPlessons.CompilerDemo class com.SPlessons.SubClass1 Return Value = false[/java]

By Using java.lang.Compiler.compileClasses()

Now instead of command line code place the following code, where the developer created a string that is "CompilerDemo". [java] /* returns false if the compilation failed or no compiler is available */ String str = "CompilerDemo"; boolean retval = Compiler.compileClasses(str); [/java] Output: If compilation failed then it returns false as follows. [java] class com.SPlessons.CompilerDemo class com.SPlessons.SubClass1 Return Value = false[/java]

shape Example - 2

Following is an example for Disable and Enable methods.

By Using java.lang.Compiler.enable()

CompilerDemo.java [java] package com.SPlessons; import java.lang.*; public class CompilerDemo { public static void main(String[] args) { // checking if compiler is enabled or not Compiler.enable(); System.out.println("Compiler Enabled..."); Compiler.command("{java.lang.Integer.*}(compile)"); Integer i = new Integer("50"); // returns a hash code value int retval = i.hashCode(); System.out.println("Value = " + retval); } } [/java] Here the developer used java.lang.Integer Class that is used to wrap the int in an object. The enable method is used to resume the operation. A unique number will be generated by JVM for every object is called as hashcode. [java] Compiler.command("{java.lang.Integer.*}(compile)");[/java] Output: Now compile the code result will be as follows. [java] Compiler Enabled... Value = 50 [/java]

By Using java.lang.Compiler.disable()

Just put the following code in the same example at the end. [java] // disable the compiler System.out.println("Disabling Compiler..."); Compiler.disable(); [/java] Output: The functionality of the disable() is to stop the compiler, the result will be as follows. [java] Compiler Enabled... Value = 50 Disabling Compiler...[/java]

Summary

shape Key Points

  • Java.lang Compiler - The java.lang.Object class is the root class for every Java classes.
  • Java.lang Compiler - To return the run time class of any object java.lang.Object.getClass() will be used.
  • The compiler.command() inspects the parameter type and its fields and play out some reported operation.