Java Code Examples for org.netbeans.spi.editor.hints.Fix#implement()
The following examples show how to use
org.netbeans.spi.editor.hints.Fix#implement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaFixUtilitiesTest.java From netbeans with Apache License 2.0 | 6 votes |
private void performArithmeticTest(String orig, String nue) throws Exception { String code = replace("0"); prepareTest("Test.java", code); ClassTree clazz = (ClassTree) info.getCompilationUnit().getTypeDecls().get(0); VariableTree variable = (VariableTree) clazz.getMembers().get(1); ExpressionTree init = variable.getInitializer(); TreePath tp = new TreePath(new TreePath(new TreePath(new TreePath(info.getCompilationUnit()), clazz), variable), init); Fix fix = JavaFixUtilities.rewriteFix(info, "A", tp, orig, Collections.<String, TreePath>emptyMap(), Collections.<String, Collection<? extends TreePath>>emptyMap(), Collections.<String, String>emptyMap(), Collections.<String, TypeMirror>emptyMap(), Collections.<String, String>emptyMap()); fix.implement(); String golden = replace(nue); String out = doc.getText(0, doc.getLength()); assertEquals(golden, out); LifecycleManager.getDefault().saveAll(); }
Example 2
Source File: JavaFixUtilitiesTest.java From netbeans with Apache License 2.0 | 6 votes |
public void performRemoveFromParentTest(String code, String rule, String golden) throws Exception { prepareTest("test/Test.java", code); HintDescription hd = HintDescriptionFactory.create() .setTrigger(PatternDescription.create(rule, Collections.<String, String>emptyMap())) .setWorker(new HintDescription.Worker() { @Override public Collection<? extends ErrorDescription> createErrors(HintContext ctx) { return Collections.singletonList(ErrorDescriptionFactory.forName(ctx, ctx.getPath(), "", JavaFixUtilities.removeFromParent(ctx, "", ctx.getPath()))); } }).produce(); List<ErrorDescription> computeHints = new HintsInvoker(HintsSettings.getGlobalSettings(), new AtomicBoolean()).computeHints(info, Collections.singleton(hd)); assertEquals(computeHints.toString(), 1, computeHints.size()); Fix fix = computeHints.get(0).getFixes().getFixes().get(0); fix.implement(); assertEquals(golden, doc.getText(0, doc.getLength())); }
Example 3
Source File: HintTest.java From netbeans with Apache License 2.0 | 6 votes |
private void doApplyFix(Fix f) throws Exception { Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class); preferences.putBoolean("importInnerClasses", true); try { if (requiresJavaFix) { assertTrue("The fix must be a JavaFix", f instanceof JavaFixImpl); ModificationResult result1 = runJavaFix(((JavaFixImpl) f).jf); ModificationResult result2 = runJavaFix(((JavaFixImpl) f).jf); //ensure the results are the same: assertEquals("The fix must be repeatable", result1.getModifiedFileObjects(), result2.getModifiedFileObjects()); for (FileObject file : result1.getModifiedFileObjects()) { assertEquals("The fix must be repeatable", result1.getResultingSource(file), result2.getResultingSource(file)); } result1.commit(); } else { f.implement(); } } finally { preferences.remove("importInnerClasses"); } }
Example 4
Source File: WSHintsTestBase.java From netbeans with Apache License 2.0 | 6 votes |
protected final void testRule(Rule<? extends Element> instance, String testFile) throws IOException { System.out.println("Executing " + getName()); System.out.println("Checking rule " + instance.getClass().getSimpleName()); context.setFileObject(dataDir.getFileObject(testFile)); JavaSource testSource = JavaSource.forFileObject(context.getFileObject()); testSource.runUserActionTask(testTask, true); assertFalse("Expected non empty hints list.", ruleEngine.getProblemsFound().isEmpty()); for(ErrorDescription ed:ruleEngine.getProblemsFound()) { for(Fix fix:ed.getFixes().getFixes()) { try { fix.implement(); } catch (Exception ex) { log(ex.getLocalizedMessage()); } } } testSource.runUserActionTask(testTask, true); assertTrue("Expected empty hints list.", ruleEngine.getProblemsFound().isEmpty()); }
Example 5
Source File: HintTestBase.java From netbeans with Apache License 2.0 | 6 votes |
private void doApplyFix(Fix f) throws Exception { Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class); preferences.putBoolean("importInnerClasses", true); try { if (requiresJavaFix) { assertTrue("The fix must be a JavaFix", f instanceof JavaFixImpl); ModificationResult result1 = runJavaFix(((JavaFixImpl) f).jf); ModificationResult result2 = runJavaFix(((JavaFixImpl) f).jf); //ensure the results are the same: assertEquals("The fix must be repeatable", result1.getModifiedFileObjects(), result2.getModifiedFileObjects()); for (FileObject file : result1.getModifiedFileObjects()) { assertEquals("The fix must be repeatable", result1.getResultingSource(file), result2.getResultingSource(file)); } result1.commit(); } else { f.implement(); } } finally { preferences.remove("importInnerClasses"); } }
Example 6
Source File: JavaFixUtilitiesTest.java From netbeans with Apache License 2.0 | 5 votes |
public void performRewriteTest(String code, String rule, String golden, String sourceLevel) throws Exception { prepareTest("test/Test.java", code, sourceLevel); final String[] split = rule.split("=>"); assertEquals(2, split.length); Map<String, TypeMirror> variablesToTypesTM = new HashMap<String, TypeMirror>(); String plainRule = PatternCompilerUtilities.parseOutTypesFromPattern(info, split[0], variablesToTypesTM); Map<String, String> variablesToTypes = new HashMap<String, String>(); for (Entry<String, TypeMirror> e : variablesToTypesTM.entrySet()) { if (e.getValue() == null) continue; variablesToTypes.put(e.getKey(), e.getValue().toString()); } HintDescription hd = HintDescriptionFactory.create() .setTrigger(PatternDescription.create(plainRule, variablesToTypes)) .setWorker(new HintDescription.Worker() { @Override public Collection<? extends ErrorDescription> createErrors(HintContext ctx) { return Collections.singletonList(ErrorDescriptionFactory.forName(ctx, ctx.getPath(), "", JavaFixUtilities.rewriteFix(ctx, "", ctx.getPath(), split[1]))); } }).produce(); List<ErrorDescription> computeHints = new HintsInvoker(HintsSettings.getGlobalSettings(), new AtomicBoolean()).computeHints(info, Collections.singleton(hd)); assertEquals(computeHints.toString(), 1, computeHints.size()); Fix fix = computeHints.get(0).getFixes().getFixes().get(0); fix.implement(); assertEquals(golden, doc.getText(0, doc.getLength())); }
Example 7
Source File: ErrorHintsTestBase.java From netbeans with Apache License 2.0 | 4 votes |
protected void performFixTest(String fileName, String code, int pos, String fixCode, String goldenFileName, String golden) throws Exception { prepareTest(fileName, code); TreePath path; String diagnosticCode; if (pos == (-1)) { Diagnostic<?> d = findPositionForErrors(); pos = (int) ErrorHintsProvider.getPrefferedPosition(info, d); diagnosticCode = d.getCode(); path = info.getTreeUtilities().pathFor(pos + 1); } else { diagnosticCode = null; path = info.getTreeUtilities().pathFor(pos); } List<Fix> fixes = computeFixes(info, diagnosticCode, pos, path); List<String> fixesNames = new LinkedList<String>(); fixes = fixes != null ? fixes : Collections.<Fix>emptyList(); Fix fix = null; for (Fix e : fixes) { String debugString = toDebugString(info, e); fixesNames.add(debugString); if (fixCode.equals(debugString)) fix = e; } assertNotNull(fixesNames.toString(), fix); fix.implement(); FileObject toCheck = sourceRoot.getFileObject(goldenFileName); assertNotNull(toCheck); DataObject toCheckDO = DataObject.find(toCheck); EditorCookie ec = toCheckDO.getLookup().lookup(EditorCookie.class); Document toCheckDocument = ec.openDocument(); String realCode = toCheckDocument.getText(0, toCheckDocument.getLength()); //ignore whitespaces: realCode = realCode.replaceAll("[ \t\n]+", " "); assertEquals(golden, realCode); LifecycleManager.getDefault().saveAll(); }
Example 8
Source File: TestPerformer.java From netbeans with Apache License 2.0 | 3 votes |
private static String getFixResult(FileObject src, Fix fix) throws Exception { String original = getText(src); fix.implement(); String nue = getText(src); copyStringToFile(src, original); return nue; }
Example 9
Source File: HintsInvokerTest.java From netbeans with Apache License 2.0 | 2 votes |
protected String performFixTest(String fileName, String code, int pos, String errorDescriptionToString, String fixDebugString, String goldenFileName, String golden) throws Exception { prepareTest(fileName, code); TreePath path = info.getTreeUtilities().pathFor(pos); List<ErrorDescription> errors = computeErrors(info, path, pos); ErrorDescription toFix = null; for (ErrorDescription d : errors) { if (errorDescriptionToString.equals(d.toString())) { toFix = d; break; } } assertNotNull("Error: \"" + errorDescriptionToString + "\" not found. All ErrorDescriptions: " + errors.toString(), toFix); assertTrue("Must be computed", toFix.getFixes().isComputed()); List<Fix> fixes = toFix.getFixes().getFixes(); List<String> fixNames = new LinkedList<String>(); Fix toApply = null; for (Fix f : fixes) { if (fixDebugString.equals(toDebugString(info, f))) { toApply = f; } fixNames.add(toDebugString(info, f)); } assertNotNull("Cannot find fix to invoke: " + fixNames.toString(), toApply); toApply.implement(); FileObject toCheck = sourceRoot.getFileObject(goldenFileName); assertNotNull(toCheck); DataObject toCheckDO = DataObject.find(toCheck); EditorCookie ec = toCheckDO.getLookup().lookup(EditorCookie.class); Document toCheckDocument = ec.openDocument(); String realCode = toCheckDocument.getText(0, toCheckDocument.getLength()); //ignore whitespaces: realCode = realCode.replaceAll("[ \t\n]+", " "); if (golden != null) { assertEquals("The output code does not match the expected code.", golden, realCode); } LifecycleManager.getDefault().saveAll(); return realCode; }
Example 10
Source File: TreeRuleTestBase.java From netbeans with Apache License 2.0 | 2 votes |
protected String performFixTest(String fileName, String code, int pos, String errorDescriptionToString, String fixDebugString, String goldenFileName, String golden) throws Exception { prepareTest(fileName, code); TreePath path = info.getTreeUtilities().pathFor(pos); List<ErrorDescription> errors = computeErrors(info, path, pos); ErrorDescription toFix = null; for (ErrorDescription d : errors) { if (errorDescriptionToString.equals(d.toString())) { toFix = d; break; } } assertNotNull("Error: \"" + errorDescriptionToString + "\" not found. All ErrorDescriptions: " + errors.toString(), toFix); assertTrue("Must be computed", toFix.getFixes().isComputed()); List<Fix> fixes = toFix.getFixes().getFixes(); List<String> fixNames = new LinkedList<String>(); Fix toApply = null; for (Fix f : fixes) { if (fixDebugString.equals(toDebugString(info, f))) { toApply = f; } fixNames.add(toDebugString(info, f)); } assertNotNull("Cannot find fix to invoke: " + fixNames.toString(), toApply); toApply.implement(); FileObject toCheck = sourceRoot.getFileObject(goldenFileName); assertNotNull(toCheck); DataObject toCheckDO = DataObject.find(toCheck); EditorCookie ec = toCheckDO.getLookup().lookup(EditorCookie.class); Document toCheckDocument = ec.openDocument(); String realCode = toCheckDocument.getText(0, toCheckDocument.getLength()); //ignore whitespaces: realCode = realCode.replaceAll("[ \t\n]+", " "); if (golden != null) { assertEquals("The output code does not match the expected code.", golden, realCode); } LifecycleManager.getDefault().saveAll(); return realCode; }