Java class source codes

Source code for the java library classes

The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. 5 ClassLoader A class loader is an object that is responsible for loading classes.

Java — Library Classes

This tutorial would cover package java.lang , which provides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time.

Here is the list of classes of package java.lang . These classes are very important to know for a Java programmer. Click a class link to know more detail about that class. For a further drill, you can refer standard Java documentation.

SN Methods with Description
1 Boolean
Boolean
2 Byte
The Byte class wraps a value of primitive type byte in an object.
3 Character
The Character class wraps a value of the primitive type char in an object.
4 Class
Instances of the class Class represent classes and interfaces in a running Java application.
5 ClassLoader
A class loader is an object that is responsible for loading classes.
6 Compiler
The Compiler class is provided to support Java-to-native-code compilers and related services.
7 Double
The Double class wraps a value of the primitive type double in an object.
8 Float
The Float class wraps a value of primitive type float in an object.
9 Integer
The Integer class wraps a value of the primitive type int in an object.
10 Long
The Long class wraps a value of the primitive type long in an object.
11 Math
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
12 Number
The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.
13 Object
Class Object is the root of the class hierarchy.
14 Package
Package objects contain version information about the implementation and specification of a Java package.
15 Process
The Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it.
16 Runtime
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running.
17 RuntimePermission
This class is for runtime permissions.
18 SecurityManager
The security manager is a class that allows applications to implement a security policy.
19 Short
The Short class wraps a value of primitive type short in an object.
20 StackTraceElement
An element in a stack trace, as returned by Throwable.getStackTrace().
21 StrictMath
The class StrictMath contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
22 String
The String class represents character strings.
23 StringBuffer
A string buffer implements a mutable sequence of characters.
24 System
The System class contains several useful class fields and methods.
25 Thread
A thread is a thread of execution in a program.
26 ThreadGroup
A thread group represents a set of threads.
27 ThreadLocal
This class provides thread-local variables.
28 Throwable
The Throwable class is the superclass of all errors and exceptions in the Java language.
29 Void
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
Читайте также:  !DOCTYPE

Using a Library in Source Code in Eclipse?, Suggestion: for easier compiling (on Windows at least): just make a .bat file with something like jar Classes. · Another suggestion is to not use

How to see source code of java library classes

This video tells how to see source code of classes in java library.
Duration: 2:21

Viewing source code of Java’s built-in classes with Eclipse

How to Make a Processing (Java) Library Part 1

How to see the source code for java SDK (java.lang, java.util, . ) classes in Eclipse?

In order to see the source codes of JDK in eclipse you should add JDK path instead of JRE to your workspace or for a specific project SDK. I assume you are a developer and you’ve installed JDK on your system.

  1. Right click and select Properties (or press Alt + Enter ) on your project.
  2. From the left tree styled menu select Java Build Path
  3. On the right select the Libraries
  4. Select the JRE System Library

  1. From the right side buttons click on edit, and edit the path from JRE location to points to the JDK location in your file system.

  1. Then click on Finish and the OK buttons of all opened dialogs to save the configurations and you set to go.

Hope this would be helpful.

you have to add Eclipse Plugin from

HELP -> Eclipse Marketplace

Enhanced Class Decompiler

just install it. and have fun.

See if you have a src.zip file in your JDK Home. This would contain the source code for all Java SE libraries. Just unzip and browse the Java files to view the source code.

If you want to debug through the source files then follow the instructions of attaching source folder in Eclipse as mentioned above.

Source code for the Java library classes, For old versions of Java with separate JRE and JDK downloads, download the JDK. The sources for the public classes are in src.zip.

Is there a Java library which generates Java source code to create objects?

Code generation is fun! You can achieve what you need by using reflection, sadly, there is no MagicCode implemented already.

You need to use the introspection to read what kind of object and create it according.

You can use the Eclipse JDT API to create classes.

Generating a compilation unit

The easiest way to programmatically generate a compilation unit is to use IPackageFragment.createCompilationUnit. You specify the name and contents of the compilation unit. The compilation unit is created inside the package and the new ICompilationUnit is returned.

From the docs, there is a example snippet.

So you basically will introspect to see what kind of object is and what is their fields and current values. Then you will use this API do create a corresponding AST. Your example might look like this.

public class CodeGenerator < public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException < Foo foobar = new Foo(); Bar bar = new Bar(); bar.setSomeValue(555d); foobar.setBar(bar); foobar.setPrimitiveDouble(23); foobar.setValue("Hello World!"); CodeGenerator codeGenerator = new CodeGenerator(); String generatedCode = codeGenerator.generateCode(foobar); System.out.println(generatedCode); >int counter = 0; private String createVariableName(String clazzName) < return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, clazzName + getCurrentCounter()); >public String generateCode(AST ast, List statements, Object object) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException < String clazzName = object.getClass().getSimpleName(); String variableName = createVariableName(clazzName); VariableDeclarationFragment newVariable = ast.newVariableDeclarationFragment(); newVariable.setName(ast.newSimpleName(variableName)); // Or clazzName.toCamelCase() ClassInstanceCreation newInstance = ast.newClassInstanceCreation(); newInstance.setType(ast.newSimpleType(ast.newSimpleName(clazzName))); newVariable.setInitializer(newInstance); VariableDeclarationStatement newObjectStatement = ast.newVariableDeclarationStatement(newVariable); newObjectStatement.setType(ast.newSimpleType(ast.newSimpleName(clazzName))); statements.add(newObjectStatement); BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass()); for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) < String propertyName = propertyDesc.getName(); if (!shouldIgnore(propertyName)) < MethodInvocation setterInvocation = ast.newMethodInvocation(); SimpleName setterName = ast.newSimpleName(propertyDesc.getWriteMethod().getName()); setterInvocation.setName(setterName); Object invoked = propertyDesc.getReadMethod().invoke(object); if (invoked == null) < continue; >if (Primitives.isWrapperType(invoked.getClass())) < if (Number.class.isAssignableFrom(invoked.getClass())) < setterInvocation.arguments().add(ast.newNumberLiteral(invoked.toString())); >// TODO: Booleans > else < if (invoked instanceof String) < StringLiteral newStringLiteral = ast.newStringLiteral(); newStringLiteral.setLiteralValue(invoked.toString()); setterInvocation.arguments().add(newStringLiteral); >else < String newObjectVariable = generateCode(ast, statements, invoked); SimpleName newSimpleName = ast.newSimpleName(newObjectVariable); setterInvocation.arguments().add(newSimpleName); >> SimpleName newSimpleName = ast.newSimpleName(variableName); setterInvocation.setExpression(newSimpleName); ExpressionStatement setterStatement = ast.newExpressionStatement(setterInvocation); statements.add(setterStatement); > > return variableName; > public String generateCode(Object object) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException < resetCounter(); AST ast = AST.newAST(AST.JLS3); Block block = ast.newBlock(); generateCode(ast, block.statements(), object); return block.toString(); >private int getCurrentCounter() < return counter++; >private void resetCounter() < counter = 0; >private boolean shouldIgnore(String propertyName) < return "class".equals(propertyName); >> 
  org.eclipse.tycho org.eclipse.jdt.core 3.9.1.v20130905-0837  org.eclipse.core runtime 3.9.100-v20131218-1515  org.eclipse.birt.runtime org.eclipse.core.resources 3.8.101.v20130717-0806  com.google.guava guava 18.0  

This is what the output looks like :

 Foo foo0=new Foo(); Bar bar1=new Bar(); bar1.setSomeValue(555.0); foo0.setBar(bar1); foo0.setPrimitiveDouble(23.0); foo0.setValue("Hello World!"); 

Here is the Foo and Bar class declaration:

public class Bar < private double someValue; public double getSomeValue() < return someValue; >public void setSomeValue(double someValue) < this.someValue = someValue; >> public class Foo < private String value; private double primitiveDouble; private Bar bar; public Bar getBar() < return bar; >public double getPrimitiveDouble() < return primitiveDouble; >public String getValue() < return value; >public void setBar(Bar bar) < this.bar = bar; >public void setPrimitiveDouble(double primitiveDouble) < this.primitiveDouble = primitiveDouble; >public void setValue(String value) < this.value = value; >> 

I added this to a github repository as requested.

You don’t need the generated source code for testing. If all of your classes have toString methods that represent the whole relevant internal state of your objects, you can still automatically generate the test cases: they will compare strings with strings.

There are lots of ways to automatically generate good toString methods, for example Intellij Idea offers 9 templates.

If you have a String that contains a source code and wish to compile it at run time, there is the Java Compiler API for this purpose

Packages In Java, First Statement is used to import Vector class from util package which is contained inside java. Second statement imports all the classes from

Источник

Managing Source and Class Files

Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this. The strategy is as follows.

Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java . For example:

//in the Rectangle.java file package graphics; public class Rectangle

Then, put the source file in a directory whose name reflects the name of the package to which the type belongs:

The qualified name of the package member and the path name to the file are parallel, assuming the Microsoft Windows file name separator backslash (for UNIX, use the forward slash).

  • class name – graphics.Rectangle
  • pathname to file – graphics\Rectangle.java

As you should recall, by convention a company uses its reversed Internet domain name for its package names. The Example company, whose Internet domain name is example.com , would precede all its package names with com.example . Each component of the package name corresponds to a subdirectory. So, if the Example company had a com.example.graphics package that contained a Rectangle.java source file, it would be contained in a series of subdirectories like this:

. \com\example\graphics\Rectangle.java

When you compile a source file, the compiler creates a different output file for each type defined in it. The base name of the output file is the name of the type, and its extension is .class . For example, if the source file is like this

//in the Rectangle.java file package com.example.graphics; public class Rectangle < . . . >class Helper

then the compiled files will be located at:

\com\example\graphics\Rectangle.class \com\example\graphics\Helper.class

Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:

\sources\com\example\graphics\Rectangle.java \classes\com\example\graphics\Rectangle.class

By doing this, you can give the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.

The full path to the classes directory, \classes , is called the class path, and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path. For example, if

is your class path, and the package name is

then the compiler and JVM look for .class files in

\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (UNIX). By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in your class path.

Setting the CLASSPATH System Variable

To display the current CLASSPATH variable, use these commands in Windows and UNIX (Bourne shell):

In Windows: C:\> set CLASSPATH In UNIX: % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use these commands:

In Windows: C:\> set CLASSPATH= In UNIX: % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable, use these commands (for example):

In Windows: C:\> set CLASSPATH=C:\users\george\java\classes In UNIX: % CLASSPATH=/home/george/java/classes; export CLASSPATH

Источник

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