package org.eclipse.dali.core.tests.adapters.java; import java.lang.reflect.Array; import java.util.Iterator; import java.util.List; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.IExtendedModifier; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.launching.JavaRuntime; import junit.framework.TestCase; public class ScratchTest extends TestCase { private IProject project; private IJavaProject javaProject; private IPackageFragmentRoot sourceFolder; public ScratchTest() { super(); } public ScratchTest(String name) { super(name); } protected void setUp() throws Exception { super.setUp(); this.project = this.buildProject(); this.addJavaNature(); this.javaProject = this.createJavaProject(); this.sourceFolder = this.createSourceFolder(); this.addSystemLibraryClasspathEntry(); this.addSourceFolderClasspathEntry(); } private IProject buildProject() throws CoreException { IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject("Test Project"); p.create(null); p.open(null); return p; } protected void tearDown() throws Exception { this.project.delete(true, true, null); this.sourceFolder = null; this.javaProject = null; this.project = null; super.tearDown(); } public void addProjectNature(String natureID) throws CoreException { IProjectDescription description = this.project.getDescription(); description.setNatureIds((String[]) this.add(description.getNatureIds(), natureID)); this.project.setDescription(description, null); } public void removeProjectNature(String natureID) throws CoreException { IProjectDescription description = this.project.getDescription(); description.setNatureIds((String[]) this.removeAllOccurrences(description.getNatureIds(), natureID)); this.project.setDescription(description, null); } public Object[] add(Object[] array, Object o) { int len = array.length; Object[] result = (Object[]) Array.newInstance(array.getClass().getComponentType(), len + 1); System.arraycopy(array, 0, result, 0, len); result[len] = o; return result; } public Object[] removeAllOccurrences(Object[] array, Object value) { Object[] result = array; if (value == null) { for (int i = array.length; i-- > 0; ) { if (array[i] == null) { result = this.remove(result, null); } } } else { for (int i = array.length; i-- > 0; ) { if (value.equals(array[i])) { result = this.remove(result, value); } } } return result; } public Object[] remove(Object[] array, Object value) { int index = this.indexOf(array, value); int len = array.length; Object[] result = (Object[]) Array.newInstance(array.getClass().getComponentType(), len - 1); System.arraycopy(array, 0, result, 0, index); System.arraycopy(array, index + 1, result, index, len - index - 1); return result; } public int indexOf(Object[] array, Object o) { int len = array.length; if (o == null) { for (int i = 0; i < len; i++) { if (array[i] == null) { return i; } } } else { for (int i = 0; i < len; i++) { if (o.equals(array[i])) { return i; } } } return -1; } private void addJavaNature() throws CoreException { this.addProjectNature(JavaCore.NATURE_ID); } private IJavaProject createJavaProject() throws CoreException { IJavaProject jp = JavaCore.create(this.project); jp.setRawClasspath(new IClasspathEntry[0], null); jp.setOutputLocation(this.createFolder("bin").getFullPath(), null); jp.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); jp.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); jp.setOption(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); return jp; } private IPackageFragmentRoot createSourceFolder() throws CoreException { return this.javaProject.getPackageFragmentRoot(this.createFolder("src")); } private void addSystemLibraryClasspathEntry() throws JavaModelException { this.addClasspathEntry(JavaRuntime.getDefaultJREContainerEntry()); } private void addSourceFolderClasspathEntry() throws JavaModelException { this.addClasspathEntry(JavaCore.newSourceEntry(this.sourceFolder.getPath())); } private void addClasspathEntry(IClasspathEntry entry) throws JavaModelException { this.javaProject.setRawClasspath((IClasspathEntry[]) this.add(this.javaProject.getRawClasspath(), entry), null); } public IFolder createFolder(String folderName) throws CoreException { return this.createFolder(this.project, new Path(folderName)); } public IFolder createFolder(IContainer container, String folderName) throws CoreException { return this.createFolder(container, new Path(folderName)); } public IFolder createFolder(IContainer container, IPath folderPath) throws CoreException { IFolder folder = container.getFolder(folderPath); if ( ! folder.exists()) { folder.create(false, true, null); } return folder; } protected static final String CR = System.getProperty("line.separator"); protected void buildType(String annotationSource) throws CoreException { this.createType("test", "TestClass.java", this.buildTypeSource(annotationSource)); } public IType createType(String packageName, String compilationUnitName, String source) throws CoreException { return this.createType(this.createPackage(packageName), compilationUnitName, source); } public IPackageFragment createPackage(String packageName) throws CoreException { return this.sourceFolder.createPackageFragment(packageName, false, null); } public IType createType(IPackageFragment packageFragment, String compilationUnitName, String source) throws CoreException { StringBuffer sb = new StringBuffer(source.length() + 200); sb.append("package ").append(packageFragment.getElementName()).append(CR); sb.append(CR); sb.append(source); ICompilationUnit cu = packageFragment.createCompilationUnit(compilationUnitName, sb.toString(), false, null); return cu.findPrimaryType(); } protected String buildTypeSource(String annotationSource) { StringBuffer sb = new StringBuffer(2000); sb.append(CR); sb.append("public class TestClass {").append(CR); sb.append(CR); sb.append(" ").append(annotationSource).append(CR); sb.append(" private int id;").append(CR); sb.append(CR); sb.append(" public int getId() {").append(CR); sb.append(" return this.id;").append(CR); sb.append(" }").append(CR); sb.append(CR); sb.append(" public void setId(int id) {").append(CR); sb.append(" this.id = id;").append(CR); sb.append(" }").append(CR); sb.append(CR); sb.append("}").append(CR); return sb.toString(); } protected IType type() throws JavaModelException { return this.javaProject.findType("test.TestClass"); } protected IField field() throws JavaModelException { return this.type().getField("id"); } public void test1() throws Exception { this.verify("@Foo(value={1, 2})"); } public void test2() throws Exception { this.verify("@Foo({1, 2})"); } private void verify(String annotation) throws Exception { this.buildType(annotation); CompilationUnit astRoot = this.astRoot(); FieldDeclaration fieldDeclaration = this.fieldDeclaration(astRoot); this.assertContainsAnnotation(fieldDeclaration); } private void assertContainsAnnotation(FieldDeclaration fieldDeclaration) { for (Iterator stream = fieldDeclaration.modifiers().iterator(); stream.hasNext(); ) { if (((IExtendedModifier) stream.next()).isAnnotation()) { return; } } fail("missing annotation"); } public CompilationUnit astRoot() throws JavaModelException { ASTParser parser = ASTParser.newParser(AST.JLS3); IField field = this.field(); if (field.isBinary()) { parser.setSource(field.getClassFile()); // the class file must have a source attachment } else { parser.setSource(field.getCompilationUnit()); } return (CompilationUnit) parser.createAST(null); // null = no progress monitor } private FieldDeclaration fieldDeclaration(CompilationUnit astRoot) { FieldDeclaration[] fieldDeclarations = this.typeDeclaration(astRoot).getFields(); for (int i = fieldDeclarations.length; i-- > 0; ) { FieldDeclaration fieldDeclaration = fieldDeclarations[i]; // handle multiple fields declared in a single statement: // private int foo, bar; List fragments = fieldDeclaration.fragments(); for (int j = fragments.size(); j-- > 0; ) { VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(j); if (fragment.getName().getFullyQualifiedName().equals("id")) { return fieldDeclaration; } } } return null; } private TypeDeclaration typeDeclaration(CompilationUnit astRoot) { for (Iterator stream = astRoot.types().iterator(); stream.hasNext(); ) { TypeDeclaration typeDeclaration = (TypeDeclaration) stream.next(); if (typeDeclaration.getName().getFullyQualifiedName().equals("TestClass")) { return typeDeclaration; } } return null; } }