1 11 12 package org.eclipse.jdt.internal.core.manipulation; 13 14 import java.util.regex.Pattern ; 15 16 import org.eclipse.core.runtime.CoreException; 17 18 import org.eclipse.core.expressions.PropertyTester; 19 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaProject; 22 import org.eclipse.jdt.core.IPackageFragmentRoot; 23 import org.eclipse.jdt.core.JavaModelException; 24 25 31 public class JavaElementPropertyTester extends PropertyTester { 32 33 36 public static final String NAME = "name"; 38 41 public static final String IS_IN_JAVA_PROJECT = "isInJavaProject"; 43 46 public static final String IS_IN_JAVA_PROJECT_WITH_NATURE = "isInJavaProjectWithNature"; 48 51 public static final String IS_ON_CLASSPATH = "isOnClasspath"; 53 56 public static final String HAS_TYPE_ON_CLASSPATH = "hasTypeOnClasspath"; 58 62 public static final String IN_SOURCE_FOLDER = "inSourceFolder"; 64 68 public static final String IN_ARCHIVE = "inArchive"; 70 74 public static final String IN_EXTERNAL_ARCHIVE = "inExternalArchive"; 76 84 public static final String PROJECT_OPTION = "projectOption"; 86 87 90 public boolean test(Object receiver, String method, Object [] args, Object expectedValue) { 91 if (!(receiver instanceof IJavaElement)) { 92 return false; 93 } 94 IJavaElement res = (IJavaElement) receiver; 95 if (method.equals(NAME)) { 96 return Pattern.matches(toString(expectedValue), res.getElementName()); 97 } else if (method.equals(IS_IN_JAVA_PROJECT)) { 98 IJavaProject javaProject= res.getJavaProject(); 99 return javaProject != null && javaProject.exists() && javaProject.getProject().isOpen(); 100 } else if (method.equals(IS_IN_JAVA_PROJECT_WITH_NATURE)) { 101 IJavaProject javaProject= res.getJavaProject(); 102 if (javaProject != null && javaProject.exists() && javaProject.getProject().isOpen() ) { 103 if (expectedValue != null) { 104 try { 105 return javaProject.getProject().hasNature(toString(expectedValue)); 106 } catch (CoreException e) { 107 return false; 108 } 109 } 110 } 111 return false; 112 } else if (method.equals(IS_ON_CLASSPATH)) { 113 IJavaProject javaProject= res.getJavaProject(); 114 if (javaProject != null && javaProject.exists()) { 115 return javaProject.isOnClasspath(res); 116 } 117 return false; 118 } else if (method.equals(IN_SOURCE_FOLDER)) { 119 IJavaElement root= res.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 120 if (root != null) { 121 try { 122 return ((IPackageFragmentRoot) root).getKind() == IPackageFragmentRoot.K_SOURCE; 123 } catch (JavaModelException e) { 124 } 126 } 127 return false; 128 } else if (method.equals(IN_ARCHIVE)) { 129 IJavaElement root= res.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 130 if (root != null) { 131 return ((IPackageFragmentRoot) root).isArchive(); 132 } 133 return false; 134 } else if (method.equals(IN_EXTERNAL_ARCHIVE)) { 135 IJavaElement root= res.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 136 if (root != null) { 137 return ((IPackageFragmentRoot) root).isExternal(); 138 } 139 return false; 140 } else if (method.equals(PROJECT_OPTION)) { 141 IJavaProject project= res.getJavaProject(); 142 if (project != null) { 143 if (args.length == 2) { 144 String current= project.getOption(toString(args[0]), true); 145 return current != null && current.equals(args[1]); 146 } else if (args.length == 1) { 147 return project.getOption(toString(args[0]), false) != null; 148 } 149 } 150 return false; 151 } else if (method.equals(HAS_TYPE_ON_CLASSPATH)) { 152 IJavaProject javaProject= res.getJavaProject(); 153 if (javaProject != null && javaProject.exists()) { 154 try { 155 return javaProject.findType(toString(expectedValue)) != null; 156 } catch (JavaModelException e) { 157 return false; 158 } 159 } 160 } 161 return false; 162 } 163 164 private String toString(Object expectedValue) { 165 return expectedValue == null ? "" : expectedValue.toString(); } 167 } 168 | Popular Tags |