|
51 | 51 | import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels; |
52 | 52 | import org.eclipse.jdt.internal.corext.dom.ASTNodes; |
53 | 53 | import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; |
| 54 | +import org.eclipse.jdt.internal.corext.fix.AddUnimplementedMethodsOperation; |
| 55 | +import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; |
| 56 | +import org.eclipse.jdt.internal.corext.fix.IProposableFix; |
| 57 | +import org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore; |
54 | 58 | import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateFileChange; |
55 | 59 | import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
56 | 60 | import org.eclipse.jdt.internal.corext.util.Messages; |
@@ -389,6 +393,31 @@ private CompilationUnitChange constructNewCUChange(ICompilationUnit cu) throws C |
389 | 393 | String typeStub = constructTypeStub(cu, fTypeNameWithParameters, Flags.AccPublic, lineDelimiter); |
390 | 394 | String cuContent = constructCUContent(cu, typeStub, lineDelimiter); |
391 | 395 | CompilationUnitChange cuChange = new CompilationUnitChange("", cu); |
| 396 | + |
| 397 | + String[] permittedNames = fCompilationUnit.findPrimaryType().getPermittedSubtypeNames(); |
| 398 | + boolean isPermitted = Arrays.asList(permittedNames).contains(fTypeNameWithParameters); |
| 399 | + |
| 400 | + if (isPermitted && fTypeKind != K_INTERFACE) { |
| 401 | + cu.becomeWorkingCopy(null); |
| 402 | + cu.getBuffer().setContents(cuContent); |
| 403 | + |
| 404 | + ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); |
| 405 | + parser.setSource(cu); |
| 406 | + parser.setResolveBindings(true); |
| 407 | + CompilationUnit cuNode = (CompilationUnit) parser.createAST(null); |
| 408 | + |
| 409 | + if (!cuNode.types().isEmpty()) { |
| 410 | + AddUnimplementedMethodsOperation operation = new AddUnimplementedMethodsOperation((ASTNode) cuNode.types().get(0), null); |
| 411 | + if (operation.getMethodsToImplement().length > 0) { |
| 412 | + IProposableFix fix = new UnimplementedCodeFixCore(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, cuNode, new CompilationUnitRewriteOperation[] { operation }); |
| 413 | + CompilationUnitChange addUnimplementedChange = fix.createChange(null); |
| 414 | + cuContent = addUnimplementedChange.getPreviewContent(null); |
| 415 | + } |
| 416 | + } |
| 417 | + |
| 418 | + cu.discardWorkingCopy(); |
| 419 | + } |
| 420 | + |
392 | 421 | cuChange.setEdit(new InsertEdit(0, cuContent)); |
393 | 422 | return cuChange; |
394 | 423 | } |
@@ -438,7 +467,11 @@ private String constructTypeStub(ICompilationUnit parentCU, String name, int mod |
438 | 467 | boolean isInterface = cuType != null ? cuType.isInterface() : false; |
439 | 468 | boolean isPermitted = Arrays.asList(permittedNames).stream().anyMatch(p -> name.equals(p)); |
440 | 469 | if (isPermitted) { |
441 | | - buf.append("final "); |
| 470 | + if (fTypeKind == K_INTERFACE) { |
| 471 | + buf.append("non-sealed "); |
| 472 | + } else { |
| 473 | + buf.append("final "); |
| 474 | + } |
442 | 475 | } |
443 | 476 |
|
444 | 477 | String type = ""; //$NON-NLS-1$ |
@@ -466,16 +499,20 @@ private String constructTypeStub(ICompilationUnit parentCU, String name, int mod |
466 | 499 | case K_RECORD: |
467 | 500 | type = "record "; //$NON-NLS-1$ |
468 | 501 | templateID = CodeGeneration.RECORD_BODY_TEMPLATE_ID; |
| 502 | + superType = isInterface ? "implements " : "extends "; |
469 | 503 | break; |
470 | 504 | } |
471 | 505 | buf.append(type); |
472 | 506 | buf.append(name); |
| 507 | + |
| 508 | + if (fTypeKind == K_RECORD) { |
| 509 | + buf.append("()"); |
| 510 | + } |
| 511 | + |
473 | 512 | if (isPermitted) { |
474 | 513 | buf.append(' '); |
475 | 514 | buf.append(superType); |
476 | 515 | buf.append(cuType.getElementName()); |
477 | | - } else if (fTypeKind == K_RECORD) { |
478 | | - buf.append("() "); |
479 | 516 | } |
480 | 517 |
|
481 | 518 | buf.append(" {").append(lineDelimiter); //$NON-NLS-1$ |
|
0 commit comments