1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IStatus; 20 21 import org.eclipse.core.expressions.EvaluationContext; 22 import org.eclipse.core.expressions.EvaluationResult; 23 import org.eclipse.core.expressions.Expression; 24 import org.eclipse.core.expressions.ExpressionConverter; 25 import org.eclipse.core.expressions.ExpressionTagNames; 26 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.IJavaModelMarker; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.JavaCore; 31 32 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 33 34 import org.eclipse.jdt.internal.ui.JavaPlugin; 35 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 36 37 public final class ContributedProcessorDescriptor { 38 39 private final IConfigurationElement fConfigurationElement; 40 private Object fProcessorInstance; 41 private Boolean fStatus; 42 private boolean fLastResult; 43 private String fRequiredSourceLevel; 44 private final Set fHandledMarkerTypes; 45 46 private static final String ID= "id"; private static final String CLASS= "class"; 49 private static final String REQUIRED_SOURCE_LEVEL= "requiredSourceLevel"; 51 private static final String HANDLED_MARKER_TYPES= "handledMarkerTypes"; private static final String MARKER_TYPE= "markerType"; 54 public ContributedProcessorDescriptor(IConfigurationElement element, boolean testMarkerTypes) { 55 fConfigurationElement= element; 56 fProcessorInstance= null; 57 fStatus= null; if (fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT).length == 0) { 59 fStatus= Boolean.TRUE; 60 } 61 fRequiredSourceLevel= element.getAttribute(REQUIRED_SOURCE_LEVEL); 62 fHandledMarkerTypes= testMarkerTypes ? getHandledMarkerTypes(element) : null; 63 } 64 65 private Set getHandledMarkerTypes(IConfigurationElement element) { 66 HashSet map= new HashSet (7); 67 IConfigurationElement[] children= element.getChildren(HANDLED_MARKER_TYPES); 68 for (int i= 0; i < children.length; i++) { 69 IConfigurationElement[] types= children[i].getChildren(MARKER_TYPE); 70 for (int k= 0; k < types.length; k++) { 71 String attribute= types[k].getAttribute(ID); 72 if (attribute != null) { 73 map.add(attribute); 74 } 75 } 76 } 77 if (map.isEmpty()) { 78 map.add(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER); 79 map.add(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER); 80 map.add(IJavaModelMarker.TASK_MARKER); 81 } 82 return map; 83 } 84 85 public IStatus checkSyntax() { 86 IConfigurationElement[] children= fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT); 87 if (children.length > 1) { 88 String id= fConfigurationElement.getAttribute(ID); 89 return new StatusInfo(IStatus.ERROR, "Only one < enablement > element allowed. Disabling " + id); } 91 return new StatusInfo(IStatus.OK, "Syntactically correct quick assist/fix processor"); } 93 94 private boolean matches(ICompilationUnit cunit) { 95 if (fRequiredSourceLevel != null) { 96 String current= cunit.getJavaProject().getOption(JavaCore.COMPILER_SOURCE, true); 97 if (JavaModelUtil.isVersionLessThan(current, fRequiredSourceLevel)) { 98 return false; 99 } 100 } 101 102 if (fStatus != null) { 103 return fStatus.booleanValue(); 104 } 105 106 IConfigurationElement[] children= fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT); 107 if (children.length == 1) { 108 try { 109 ExpressionConverter parser= ExpressionConverter.getDefault(); 110 Expression expression= parser.perform(children[0]); 111 EvaluationContext evalContext= new EvaluationContext(null, cunit); 112 evalContext.addVariable("compilationUnit", cunit); IJavaProject javaProject= cunit.getJavaProject(); 114 String [] natures= javaProject.getProject().getDescription().getNatureIds(); 115 evalContext.addVariable("projectNatures", Arrays.asList(natures)); evalContext.addVariable("sourceLevel", javaProject.getOption(JavaCore.COMPILER_SOURCE, true)); fLastResult= !(expression.evaluate(evalContext) != EvaluationResult.TRUE); 118 return fLastResult; 119 } catch (CoreException e) { 120 JavaPlugin.log(e); 121 } 122 } 123 fStatus= Boolean.FALSE; 124 return false; 125 } 126 127 public Object getProcessor(ICompilationUnit cunit) throws CoreException { 128 if (matches(cunit)) { 129 if (fProcessorInstance == null) { 130 fProcessorInstance= fConfigurationElement.createExecutableExtension(CLASS); 131 } 132 return fProcessorInstance; 133 } 134 return null; 135 } 136 137 public boolean canHandleMarkerType(String markerType) { 138 return fHandledMarkerTypes == null || fHandledMarkerTypes.contains(markerType); 139 } 140 141 } 142 | Popular Tags |