Compiler Class In Java


A native code is a coding formation that can be run in a Java Virtual Machine. The compiler class, provides a support as well as give us a space to convert a Java code to a native code. This a public package which embedded in java.lang.Compiler.command() package in a Java environment. Here is an example fo a compiler class in Java −

class of the NewClass$CompilerClass Name
Value Is: null value
Is the compilation successful here? : false value
Is the compilation successful using str here? : false value

Algorithm to Operate With a Compiler Class in Java

In this possible algorithm, we are going to show you how to perform the compiler class which provides the support and services from a Java code to Native code. By using this algorithm, we will build some Java syntax to explain the process in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Inport and declare some Java packages to run the process.

  • Step 3 − Declare a public class.

  • Step 4 − Mention a string argument.

  • Step 5 − Mention and call a compiler class function class.

  • Step 6 − Use the enable() method.

  • Step 7 − Declare a class for compiler method.

  • Step 8 − Use of print command.

  • Step 9 − Check the subclass is present or not.

  • Step 10 − Check the boolean value.

  • Step 11 − Get the print value.

  • Step 13 − Use desable to stop the method.

  • Step 14 − Declare a private class in the form of compiler class.

  • Step 15 − Terminate the process.

Syntax to Operate With a Compiler Class in Java

protected void assertRun(String mainClass) {
   try {
      List<String> args = new ArrayList<>();
      args.add(jvm);
      args.add("-cp");
      args.add(".:" + System.getProperty("java.class.path"));
      args.add(mainClass);
      log.debug("command: " + StringUtils.join(args.toArray(new String[]{}), "
      "));
      Compiler Class In Java
      ProcessBuilder pb = new ProcessBuilder(args);
      pb.redirectErrorStream(true);
      pb.directory(getClassOutputDir());
      Process p = pb.start();
      jvmOutput = IOUtils.toString(p.getInputStream());
      int exitCode = p.waitFor();
      log.debug(jvm + " exit code: " + exitCode);
      log.debug(jvm + " console output: " + jvmOutput);
      assertEquals(jvmOutput, 0, exitCode);
   } catch (IOException ex) {
      throw new RuntimeException(ex);
   } catch (InterruptedException ex) {
      throw new Error(ex);
   }
}

In this possible syntax above, we have tried to show you how to declare and create a compiler class method in a particular process. By using this particular syntax, we are heading towards some possible approaches related to the problem statement in an efficient manner.

Approaches to Follow

  • Approach 1 − Java Program illustrating the use of Compiler class Methods

  • Approach 2 − Java program to illustrate compiler class by using various Long class methods and bit count

Approach 1: Use of Compiler Class Methods

Use of Compiler Extends Object Method

In this possible approach, we are going to apply the Java compiler class method which will return a true value if the compilation become successful. But it can also throw a NullPointerException.

public final class Compiler extends Object
public static boolean command(Object argument)
public static boolean compileClass(Class c)
public static void enable()
public static void disable()
public static boolean compileClasses(String string)

Example

//Java Program illustrating the use of Compiler class Methods.
import java.lang.*;
public class ARBRDD {
   public static void main(String[] args){
      CompilerClass geek = new CompilerClass();
      Compiler.enable();
      Class c = geek.getClass();
      System.out.println(c);
      Object g = Compiler.command("javac CompilerClass Has Been Declared");
      System.out.println("Value Of The Process Is : " + g);
      boolean check = Compiler.compileClass(c);
      System.out.println( "Is the particular compilation successful here? : " + check);
      String str = "CompilerClass";
      boolean check1 = Compiler.compileClasses(str);
      System.out.println( "Is the particular compilation successful using str here ? : " + check1);
      Compiler.disable();
   }
   private static class CompilerClass {
      public CompilerClass() {}
   }
}

Output

class ARBRDD$CompilerClass
Value Of The Process Is : null
Is the particular compilation successful here? : false
Is the particular compilation successful using str here ? : false

Approach 2: Use of Long Class and Bit Count Methods

Use of the Long Class Method

In this possible approach,we are going to apply the Long object to represent the value which is associated with the system property. It can return a null value if the value does not exist in the process.

public Long(long b)
public Long(String s)throws NumberFormatException
public String toString(long b)
public byte byteValue()
public static int bitCount(long i)
public static int numberofLeadingZeroes(long i)

Example

//Java program to illustrate compiler class by using various Long class methods
public class Long_test{
   public static void main(String args[]){
      long b = 55;
      String bb = "45";
      Long x = new Long(b);
      Long y = new Long(bb);
      System.out.println("toString(b) = " + Long.toString(b));
      System.out.println("toHexString(b) =" + Long.toHexString(b));
      System.out.println("toOctalString(b) =" + Long.toOctalString(b));
      System.out.println("toBinaryString(b) =" + Long.toBinaryString(b));
      Long z = Long.valueOf(b);
      System.out.println("valueOf(b) = " + z);
      z = Long.valueOf(bb);
      System.out.println("ValueOf(bb) = " + z);
      z = Long.valueOf(bb, 6);
      System.out.println("ValueOf(bb,6) = " + z);
      long zz = Long.parseLong(bb);
      System.out.println("parseLong(bb) = " + zz);
      zz = Long.parseLong(bb, 6);
      System.out.println("parseLong(bb,6) = " + zz);
      long prop = Long.getLong("sun.arch.data.model");
      System.out.println("getLong(sun.arch.data.model) = " + prop);
      System.out.println("getLong(abcd) =" + Long.getLong("abcd"));
      System.out.println("getLong(abcd,10) =" + Long.getLong("abcd", 10));
      String decimal = "45";
      String octal = "005";
      String hex = "0x0f";
      Long dec = Long.decode(decimal);
      System.out.println("decode(45) = " + dec);
      dec = Long.decode(octal);
      System.out.println("decode(005) = " + dec);
      dec = Long.decode(hex);
      System.out.println("decode(0x0f) = " + dec);
      long valrot = 2;
      System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Long.rotateLeft(valrot, 2));
      System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Long.rotateRight(valrot, 3));
   }
}

Output

toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseLong(bb) = 45
parseLong(bb,6) = 29
getLong(sun.arch.data.model) value = 64
getLong(abcd) = null
getLong(abcd,10) =10
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =4611686018427387904

Use of the bit Count Method

In this possible approach,we are going to apply highestOneBit() and LowestOneBit() for the single one bit value for a compiler class. It also returns a higher and lower value with a one bit at last. As an example −

If the value of a number is 0000 0000 0000 1111, then the return will be from the process is 0000 0000 0000 0001.

Example

//Java program to illustrate compiler class by using various Long class methods by using bit count
public class ARBRDD{
   public static void main(String args[]){
      long b = 55;
      String bb = "45";
      Long x = new Long(b);
      Long y = new Long(bb);
      System.out.println("bytevalue(x) = " + x.byteValue());
      System.out.println("shortvalue(x) = " + x.shortValue());
      System.out.println("intvalue(x) = " + x.intValue());
      System.out.println("longvalue(x) = " + x.longValue());
      System.out.println("doublevalue(x) = " + x.doubleValue());
      System.out.println("floatvalue(x) = " + x.floatValue());
      long value = 45;
      System.out.println("Long.bitcount(value)=" + Long.bitCount(value));
      System.out.println("Long.numberOfTrailingZeros(value)=" +
      Long.numberOfTrailingZeros(value));
      System.out.println("Long.numberOfLeadingZeros(value)=" +
      Long.numberOfLeadingZeros(value));
      System.out.println("Long.highestOneBit(value)=" +
      Long.highestOneBit(value));
      System.out.println("Long.lowestOneBit(value)=" +
      Long.lowestOneBit(value));
      System.out.println("Long.reverse(value)=" + Long.reverse(value));
      System.out.println("Long.reverseBytes(value)=" +
      Long.reverseBytes(value));
      System.out.println("Long.signum(value)=" + Long.signum(value));
      int hash = x.hashCode();
      System.out.println("hashcode(x) = " + hash);
      boolean eq = x.equals(y);
      System.out.println("x.equals(y) = " + eq);
      int e = Long.compare(x, y);
      System.out.println("compare(x,y) = " + e);
      int f = x.compareTo(y);
      System.out.println("x.compareTo(y) = " + f);
   }
}

Output

ytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
Long.bitcount(value)=4
Long.numberOfTrailingZeros(value)=0
Long.numberOfLeadingZeros(value)=58
Long.highestOneBit(value)=32
Long.lowestOneBit(value)=1
Long.reverse(value)=-5476377146882523136
Long.reverseBytes(value)=3242591731706757120
Long.signum(value)=1
hashcode(x) = 55
x.equals(y) = false value
compare(x,y) = 1
x.compareTo(y) = 1

Conclusion

Java compile class is a back code container which stores the byte code in a Java Virtual Machine. The javac method turns the source code into a compiled class method and rename the source files with a particular class extension. Today in this article we have learned about the working process of a compiler class in a Java environment. With the above mentioned syntax and algorithm, we have built some Java codes to solve the problem statement in an efficient manner.

Updated on: 29-Dec-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements