Pages

Tuesday, October 30, 2012

The Language Toolkit read

http://www.eclipse.org/articles/Article-LTK/ltk.html A subclass of org.eclipse.ltk.core.refactoring.Refactoring must always be created. As is in this quotation, we need to extend Refactoring. Screen Shot 2012 10 30 at 11 27 24 PM As a result of extending Refactoring class, you need to implement four method. Screen Shot 2012 10 30 at 11 28 39 PM

LKT usage hint of eclipse from Use, Disuse, and Misues of Automated Refactorings paper.

Use, Disuse, and Misuse of Automated Refactorings paper has some clues on how to execute the eclipse refactoring in a programmatic way.
  1. It knows the selected string - "selection-text"
  2. The method that contains the selection - "code-snippet"
  3. Source code - "input"
  4. The kind of refactoring (maybe from the menu) - "id"
Screen Shot 2012 10 30 at 10 51 29 PM

simple make example

  • You can use variable by assigning it, and use it with $(), it can be anything, you can even think of it as a macro.
  • You can use $@ to replace the target. @ is a good name to represent the goal(target).
  • $+ means all prerequisites with space separated when multiple is given.
  • %.o:%.c shows the relationship based on extension name. Don't forget it uses % not *.
# 1
# Defining the compiler:
CC=gcc

# Defining the object files:
objects = main.o example.o

# 2
# The default rule - compiling our main program:
all:	sample
		echo all: make complete

# 3
sample: $(objects)
	# If we get here, all the dependencies are now built.
	# Link it:
	$(CC) -o $@ $+

# 4
# Tell make how to build .o files from .c files:
%.o:%.c
	$(CC) -c $+
	
# 5
#Now make sure that make rebuilds files if included headers change:
main.o: main.h defs.h
example.o: example.h defs.h
http://linuxdevcenter.com/pub/a/linux/2002/01/31/make_intro.html?page=2

phony target in make

When you execute make clean, you are using phony target. phony target is a target that doesn't have the dependencies, but only the actions.
# Naming our phony targets
.PHONY: clean install

# Removing the executable and the object files
clean: 
		rm sample main.o example.o
		echo clean: make complete

# Installing the final product
install:
		cp sample /usr/local
		echo install: make complete

How JPF finds the files

site.properties

~/.jpf/site.properties file is the one where all the directory information is located.
# JPF site configuration
user.home = /Users/smcho/
jpf.home = ${user.home}/Dropbox/smcho/workspace/jpf

# can only expand system properties
jpf-core = ${jpf.home}/jpf-core

jpf-symbc = ${jpf.home}/jpf-symbc
extensions+=,${jpf-symbc}

jpf-guided-test = ${jpf.home}/jpf-guided-test
extensions+=,${jpf-guided-test}

jpf-regression = ${jpf.home}/jpf-regression
extensions+=,${jpf-regression}

jpf-porting = ${jpf.home}/jpf-porting
extensions+=,${jpf-porting}
It points to where all the jpf related information are located.

jpf.properties

Now, you can set up your jpf.properties so that you can point to where ever you want.
#--- project specific host VM classpath (used by Java to load classes)
# NOTE: you have to replace the wildcard jar specs with explicit jar pathnames
# if this property file is used from within a build.xml or NetBeans project.xml,
# since both do not support wildcard patterns in classpath specs
jpf-porting.native_classpath =\
   ${jpf-porting}/build/jpf-porting.jar;\
   ${jpf-porting}/lib/*.jar;\
   ${jpf-symbc}/lib/commons-lang-2.4.jar;
   

#--- project specific JPF classpath (used by JPF to load system-under-test classes)
jpf-porting.classpath =\
   ${jpf-porting}/build/examples;

#--- where are the classes for the JPF regression tests (used by host VM and JPF)
jpf-porting.test_classpath =\
   ${jpf-porting}/build/tests
   
#--- project specific JPF sourcepath (used by JPF to locate sources for system-under-test classes)
jpf-porting.sourcepath =\
   ${jpf-porting}/src

#--- other project specific options go here (e.g. 'vm.insn_factory.class' or 'peer_packages')
peer_packages= gov.nasa.jpf.symbc,${peer_packages}
vm.storage.class=nil
search.multiple_errors=true
symbolic.dp=choco

From RunJPF.jar you can specify the file path of properties file.
java -jar /Users/smcho/Dropbox/smcho/workspace/jpf/jpf-core/build/RunJPF.jar \
+shell.port=4242 \
/Users/smcho/Dropbox/smcho/works/tasks/2012/seal/refactoringChecker/tests/case_ini_is_generated_from_ref_finder/TestA/A.jpf -v\
-c jpf.properties
Now, you can modify the properties file to point to the example directory.

JPF Configuration

Screen Shot 2012 10 30 at 5 06 55 PM
java -jar jpf/jpf-core/build/RunJPF.jar +shell.port=4242 A.jpf -v
http://javapathfinder.sourceforge.net/Configuring_JPF_Runtime_Options.html

Java Path Finder tutorial

Screen Shot 2012 10 30 at 4 59 11 PM https://dl.dropbox.com/u/10773282/papers/2012/JPF-lecture-2.pdf http://fm.csl.sri.com/SSFT11/JPF-lecture-2.pdf

Executing eclipse rename refactoring using LTK

With eclipse, one can use Language Tool Kit (LTK). This is an example of how to execute rename refactoring with the LTK API.

Finding a project

IProject is an interface to refer the Project object, and the project can be found using this code.
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(PROJECT_NAME);
project.open(null /* IProgressMonitor */);

Finding class

Then, we can find the class(IType), and from the type we can get CompilationUnit.
IJavaProject javaProject = JavaCore.create(project);
IType itype = javaProject.findType("smcho.NewName");
org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();

Getting contribution and descriptor

We're renaming a class, so we use getRefactoringContribution() to get contribution, and from the contribution we can get the descriptor.
RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_COMPILATION_UNIT);
RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contribution.createDescriptor();
Then, you need to set the name of the project, new class name, and CompilationUnit.
descriptor.setProject(icu.getResource().getProject().getName( ));
descriptor.setNewName(NEW_CLASS_NAME); // new name for a Class
descriptor.setJavaElement(icu);

Executing the refactoring

One starts with the status and monitor. With them check initial and final conditions. And finally create change object and perform with a parameter monitor.
RefactoringStatus status = new RefactoringStatus();
try {
    Refactoring refactoring = descriptor.createRefactoring(status);

    IProgressMonitor monitor = new NullProgressMonitor();
    refactoring.checkInitialConditions(monitor);
    refactoring.checkFinalConditions(monitor);
    Change change = refactoring.createChange(monitor);
    change.perform(monitor);
} catch (CoreException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}    

The renameRefactor() method

Combining all of them, one can have this method to refactor from oldName to newName in the projeceName.
public void renameRefactor(String projectName, String oldName, String newName) throws CoreException
{
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);
    project.open(null /* IProgressMonitor */);
    
    IJavaProject javaProject = JavaCore.create(project);
    IType itype = javaProject.findType(oldName); // you need to include the package name also "smcho.NewName" 
    org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();

    RefactoringContribution contribution =
                RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_COMPILATION_UNIT);
    RenameJavaElementDescriptor descriptor =
                (RenameJavaElementDescriptor) contribution.createDescriptor();
    descriptor.setProject(icu.getResource().getProject().getName( )); // maybe, you can just use the String
    descriptor.setNewName(newName); // new name for a Class
    descriptor.setJavaElement(icu);

    RefactoringStatus status = new RefactoringStatus();
    try {
        Refactoring refactoring = descriptor.createRefactoring(status);

        IProgressMonitor monitor = new NullProgressMonitor();
        refactoring.checkInitialConditions(monitor);
        refactoring.checkFinalConditions(monitor);
        Change change = refactoring.createChange(monitor);
        change.perform(monitor);

    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }        
}