protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { // avoid adding imports when inside imports container if (impRewrite != null && fFullyQualifiedTypeName != null) { String replacementString= getReplacementString(); String qualifiedType= fFullyQualifiedTypeName; if (qualifiedType.indexOf('.') != -1 && replacementString.startsWith(qualifiedType) && !replacementString.endsWith(String.valueOf(';'))) { IType[] types= impRewrite.getCompilationUnit().getTypes(); if (types.length > 0 && types[0].getSourceRange().getOffset() <= offset) { // ignore positions above type. setReplacementString(impRewrite.addImport(getReplacementString())); return true; } } } return false; }
private static Integer[] createOffsetArray(IType[] types) throws JavaModelException { List<Integer> result= new ArrayList<>(); for (int i= 0; i < types.length; i++) { IType iType= types[i]; addOffset(result, iType.getNameRange().getOffset()); addOffset(result, iType.getSourceRange().getOffset() + iType.getSourceRange().getLength()); addMemberOffsetList(result, iType.getMethods()); addMemberOffsetList(result, iType.getFields()); addMemberOffsetList(result, iType.getInitializers()); } return result.toArray(new Integer[result.size()]); }
private static Integer[] createOffsetArray(IType[] types) throws JavaModelException { List result= new ArrayList(); for (int i= 0; i < types.length; i++) { IType iType= types[i]; addOffset(result, iType.getNameRange().getOffset()); addOffset(result, iType.getSourceRange().getOffset() + iType.getSourceRange().getLength()); addMemberOffsetList(result, iType.getMethods()); addMemberOffsetList(result, iType.getFields()); addMemberOffsetList(result, iType.getInitializers()); } return (Integer[]) result.toArray(new Integer[result.size()]); }
private static Integer[] createOffsetArray(IType[] types) throws JavaModelException { List<Integer> result= new ArrayList<>(); for (int i= 0; i < types.length; i++) { IType iType= types[i]; addOffset(result, iType.getNameRange().getOffset()); addOffset(result, iType.getSourceRange().getOffset() + iType.getSourceRange().getLength()); addMemberOffsetList(result, iType.getMethods()); addMemberOffsetList(result, iType.getFields()); addMemberOffsetList(result, iType.getInitializers()); } return result.toArray(new Integer[result.size()]); }
private void addDocumentedAnnotation(IType newType, ImportsManager imports, String lineDelimiter) throws JavaModelException { if (fDocumentedSelection.isSelected()) { String typeName= imports.addImport(Documented.class.getName()); int start= newType.getSourceRange().getOffset(); IBuffer buffer= newType.getCompilationUnit().getBuffer(); buffer.replace(start, 0, "@" + typeName + lineDelimiter); //$NON-NLS-1$ } }
private void addDocumentedAnnotation(IType newType, ImportsManager imports, String lineDelimiter) throws JavaModelException { if (fDocumentedSelection.isSelected()) { String typeName= imports.addImport(Documented.class.getName()); int start= newType.getSourceRange().getOffset(); IBuffer buffer= newType.getCompilationUnit().getBuffer(); buffer.replace(start, 0, "@" + typeName + lineDelimiter); //$NON-NLS-1$ } }
/** * Updates the replacement string. * * @param document the document * @param trigger the trigger * @param offset the offset * @param impRewrite the import rewrite * @return <code>true</code> if the cursor position should be updated, <code>false</code> otherwise * @throws BadLocationException if accessing the document fails * @throws CoreException if something else fails */ protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { // avoid adding imports when inside imports container if (impRewrite != null && fFullyQualifiedTypeName != null) { String replacementString= getReplacementString(); String qualifiedType= fFullyQualifiedTypeName; if (qualifiedType.indexOf('.') != -1 && replacementString.startsWith(qualifiedType) && !replacementString.endsWith(String.valueOf(';'))) { IType[] types= impRewrite.getCompilationUnit().getTypes(); if (types.length > 0 && types[0].getSourceRange().getOffset() <= offset) { // ignore positions above type. setReplacementString(impRewrite.addImport(getReplacementString())); return true; } } } return false; }
/** * Updates the replacement string. * * @param document the document * @param trigger the trigger * @param offset the offset * @param impRewrite the import rewrite * @return <code>true</code> if the cursor position should be updated, <code>false</code> otherwise * @throws BadLocationException if accessing the document fails * @throws CoreException if something else fails */ protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { // avoid adding imports when inside imports container if (impRewrite != null && fFullyQualifiedTypeName != null) { String replacementString= getReplacementString(); String qualifiedType= fFullyQualifiedTypeName; if (qualifiedType.indexOf('.') != -1 && replacementString.startsWith(qualifiedType) && !replacementString.endsWith(String.valueOf(';'))) { IType[] types= impRewrite.getCompilationUnit().getTypes(); if (types.length > 0 && types[0].getSourceRange().getOffset() <= offset) { // ignore positions above type. setReplacementString(impRewrite.addImport(getReplacementString())); return true; } } } return false; }
public void addAnnotation(IType newType, ImportsManager imports, String lineDelimiter) throws JavaModelException { if (isEnabled()) { List<E> selectedEnums= availableSelectedEnums(); if (selectedEnums.size() > 0) { String annotation= createAnnotationAndImports(selectedEnums, imports, lineDelimiter); int start= newType.getSourceRange().getOffset(); IBuffer buffer= newType.getCompilationUnit().getBuffer(); buffer.replace(start, 0, annotation); } } }
public void addAnnotation(IType newType, ImportsManager imports, String lineDelimiter) throws JavaModelException { if (isEnabled()) { List<E> selectedEnums= availableSelectedEnums(); if (selectedEnums.size() > 0) { String annotation= createAnnotationAndImports(selectedEnums, imports, lineDelimiter); int start= newType.getSourceRange().getOffset(); IBuffer buffer= newType.getCompilationUnit().getBuffer(); buffer.replace(start, 0, annotation); } } }
protected boolean isInside(int pos, ISourceReference sourceElement) throws CoreException { // XXX: Work in progress for problem decorator being a workbench decorator // IDecoratorManager decoratorMgr= PlatformUI.getWorkbench().getDecoratorManager(); // if (!decoratorMgr.getEnabled("org.eclipse.jdt.ui.problem.decorator")) //$NON-NLS-1$ // return false; if (!(sourceElement instanceof IType) || ((IType)sourceElement).getDeclaringType() != null) return false; ICompilationUnit cu= ((IType)sourceElement).getCompilationUnit(); if (cu == null) return false; IType[] types= cu.getTypes(); if (types.length < 1) return false; int firstTypeStartOffset= -1; ISourceRange range= types[0].getSourceRange(); if (range != null) firstTypeStartOffset= range.getOffset(); int lastTypeEndOffset= -1; range= types[types.length-1].getSourceRange(); if (range != null) lastTypeEndOffset= range.getOffset() + range.getLength() - 1; return pos < firstTypeStartOffset || pos > lastTypeEndOffset || isInside(pos, sourceElement.getSourceRange()); }
@Override protected boolean isInside(int pos, ISourceReference sourceElement) throws CoreException { // XXX: Work in progress for problem decorator being a workbench decorator // IDecoratorManager decoratorMgr= PlatformUI.getWorkbench().getDecoratorManager(); // if (!decoratorMgr.getEnabled("org.eclipse.jdt.ui.problem.decorator")) //$NON-NLS-1$ // return false; if (!(sourceElement instanceof IType) || ((IType)sourceElement).getDeclaringType() != null) return false; ICompilationUnit cu= ((IType)sourceElement).getCompilationUnit(); if (cu == null) return false; IType[] types= cu.getTypes(); if (types.length < 1) return false; int firstTypeStartOffset= -1; ISourceRange range= types[0].getSourceRange(); if (range != null) firstTypeStartOffset= range.getOffset(); int lastTypeEndOffset= -1; range= types[types.length-1].getSourceRange(); if (range != null) lastTypeEndOffset= range.getOffset() + range.getLength() - 1; return pos < firstTypeStartOffset || pos > lastTypeEndOffset || isInside(pos, sourceElement.getSourceRange()); }
@Override protected boolean isInside(int pos, ISourceReference sourceElement) throws CoreException { // XXX: Work in progress for problem decorator being a workbench decorator // IDecoratorManager decoratorMgr= PlatformUI.getWorkbench().getDecoratorManager(); // if (!decoratorMgr.getEnabled("org.eclipse.jdt.ui.problem.decorator")) //$NON-NLS-1$ // return false; if (!(sourceElement instanceof IType) || ((IType)sourceElement).getDeclaringType() != null) return false; ICompilationUnit cu= ((IType)sourceElement).getCompilationUnit(); if (cu == null) return false; IType[] types= cu.getTypes(); if (types.length < 1) return false; int firstTypeStartOffset= -1; ISourceRange range= types[0].getSourceRange(); if (range != null) firstTypeStartOffset= range.getOffset(); int lastTypeEndOffset= -1; range= types[types.length-1].getSourceRange(); if (range != null) lastTypeEndOffset= range.getOffset() + range.getLength() - 1; return pos < firstTypeStartOffset || pos > lastTypeEndOffset || isInside(pos, sourceElement.getSourceRange()); }
@Override public boolean test(IType candidate) { boolean accept = super.test(candidate); if (!accept) { return false; } try { int flags = candidate.getFlags(); if (Flags.isAbstract(flags)) { return false; } if (SourceRange.isAvailable(candidate.getSourceRange())) { // only accept non-abstract types with source available TextProviderServiceDeclaration d = new TextProviderServiceDeclaration(candidate, getOrder(candidate)); result.add(d); } } catch (JavaModelException e) { // this element seems to be corrupt -> ignore SdkLog.warning("Attempt to access source range of type '{}' failed. Type will be skipped.", candidate.getFullyQualifiedName(), e); } return false; } };
private Expression createEnclosingInstanceCreationString(final ASTNode node, final ICompilationUnit cu) throws JavaModelException { Assert.isTrue((node instanceof ClassInstanceCreation) || (node instanceof SuperConstructorInvocation)); Assert.isNotNull(cu); Expression expression= null; if (node instanceof ClassInstanceCreation) expression= ((ClassInstanceCreation) node).getExpression(); else expression= ((SuperConstructorInvocation) node).getExpression(); final AST ast= node.getAST(); if (expression != null) return expression; else if (JdtFlags.isStatic(fType)) return null; else if (isInsideSubclassOfDeclaringType(node)) return ast.newThisExpression(); else if ((node.getStartPosition() >= fType.getSourceRange().getOffset() && ASTNodes.getExclusiveEnd(node) <= fType.getSourceRange().getOffset() + fType.getSourceRange().getLength())) { if (fCodeGenerationSettings.useKeywordThis || fEnclosingInstanceFieldName.equals(fNameForEnclosingInstanceConstructorParameter)) { final FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(ast.newSimpleName(fEnclosingInstanceFieldName)); return access; } else return ast.newSimpleName(fEnclosingInstanceFieldName); } else if (isInsideTypeNestedInDeclaringType(node)) { final ThisExpression qualified= ast.newThisExpression(); qualified.setQualifier(ast.newSimpleName(fType.getDeclaringType().getElementName())); return qualified; } return null; }
private Expression createEnclosingInstanceCreationString(final ASTNode node, final ICompilationUnit cu) throws JavaModelException { Assert.isTrue((node instanceof ClassInstanceCreation) || (node instanceof SuperConstructorInvocation)); Assert.isNotNull(cu); Expression expression= null; if (node instanceof ClassInstanceCreation) expression= ((ClassInstanceCreation) node).getExpression(); else expression= ((SuperConstructorInvocation) node).getExpression(); final AST ast= node.getAST(); if (expression != null) return expression; else if (JdtFlags.isStatic(fType)) return null; else if (isInsideSubclassOfDeclaringType(node)) return ast.newThisExpression(); else if ((node.getStartPosition() >= fType.getSourceRange().getOffset() && ASTNodes.getExclusiveEnd(node) <= fType.getSourceRange().getOffset() + fType.getSourceRange().getLength())) { if (fCodeGenerationSettings.useKeywordThis || fEnclosingInstanceFieldName.equals(fNameForEnclosingInstanceConstructorParameter)) { final FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(ast.newSimpleName(fEnclosingInstanceFieldName)); return access; } else return ast.newSimpleName(fEnclosingInstanceFieldName); } else if (isInsideTypeNestedInDeclaringType(node)) { final ThisExpression qualified= ast.newThisExpression(); qualified.setQualifier(ast.newSimpleName(fType.getDeclaringType().getElementName())); return qualified; } return null; }
private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException { // fields are different because you don't delete the whole declaration but only a fragment of it if (element.getElementType() == IJavaElement.FIELD) { if (JdtFlags.isEnum((IField) element)) return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode)}; else return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode)}; } if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) { IType type= (IType) element; if (type.isAnonymous()) { if (type.getParent().getElementType() == IJavaElement.FIELD) { final ISourceRange range= type.getSourceRange(); if (range != null) { final ASTNode node= ASTNodeSearchUtil.getAstNode(cuNode, range.getOffset(), range.getLength()); if (node instanceof AnonymousClassDeclaration) return new ASTNode[] { node}; } } return new ASTNode[] { ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode)}; } else { ASTNode[] nodes= ASTNodeSearchUtil.getDeclarationNodes(element, cuNode); // we have to delete the TypeDeclarationStatement nodes[0]= nodes[0].getParent(); return nodes; } } return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode); }
private static StubTypeContext createSupertypeStubTypeContext(String typeName, boolean isInterface, IType enclosingType, IPackageFragment packageFragment) { StubTypeContext stubTypeContext; String prolog= "class " + typeName + (isInterface ? " implements " : " extends "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ String epilog= " {} "; //$NON-NLS-1$ if (enclosingType != null) { try { ICompilationUnit cu= enclosingType.getCompilationUnit(); ISourceRange typeSourceRange= enclosingType.getSourceRange(); int focalPosition= typeSourceRange.getOffset() + typeSourceRange.getLength() - 1; // before closing brace ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setSource(cu); parser.setFocalPosition(focalPosition); CompilationUnit compilationUnit= (CompilationUnit) parser.createAST(null); stubTypeContext= createStubTypeContext(cu, compilationUnit, focalPosition); stubTypeContext= new StubTypeContext(stubTypeContext.getCuHandle(), stubTypeContext.getBeforeString() + prolog, epilog + stubTypeContext.getAfterString()); } catch (CoreException e) { JavaPlugin.log(e); stubTypeContext= new StubTypeContext(null, null, null); } } else if (packageFragment != null) { ICompilationUnit cu= packageFragment.getCompilationUnit(JavaTypeCompletionProcessor.DUMMY_CU_NAME); stubTypeContext= new StubTypeContext(cu, "package " + packageFragment.getElementName() + ";" + prolog, epilog); //$NON-NLS-1$//$NON-NLS-2$ } else { stubTypeContext= new StubTypeContext(null, null, null); } return stubTypeContext; }
try { ICompilationUnit cu= enclosingType.getCompilationUnit(); ISourceRange typeSourceRange= enclosingType.getSourceRange(); int focalPosition= typeSourceRange.getOffset() + typeSourceRange.getLength() - 1; // before closing brace
private static StubTypeContext createSupertypeStubTypeContext(String typeName, boolean isInterface, IType enclosingType, IPackageFragment packageFragment) { StubTypeContext stubTypeContext; String prolog= "class " + typeName + (isInterface ? " implements " : " extends "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ String epilog= " {} "; //$NON-NLS-1$ if (enclosingType != null) { try { ICompilationUnit cu= enclosingType.getCompilationUnit(); ISourceRange typeSourceRange= enclosingType.getSourceRange(); int focalPosition= typeSourceRange.getOffset() + typeSourceRange.getLength() - 1; // before closing brace ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setSource(cu); parser.setFocalPosition(focalPosition); CompilationUnit compilationUnit= (CompilationUnit) parser.createAST(null); stubTypeContext= createStubTypeContext(cu, compilationUnit, focalPosition); stubTypeContext= new StubTypeContext(stubTypeContext.getCuHandle(), stubTypeContext.getBeforeString() + prolog, epilog + stubTypeContext.getAfterString()); } catch (CoreException e) { JavaPlugin.log(e); stubTypeContext= new StubTypeContext(null, null, null); } } else if (packageFragment != null) { ICompilationUnit cu= packageFragment.getCompilationUnit(JavaTypeCompletionProcessor.DUMMY_CU_NAME); stubTypeContext= new StubTypeContext(cu, "package " + packageFragment.getElementName() + ";" + prolog, epilog); //$NON-NLS-1$//$NON-NLS-2$ } else { stubTypeContext= new StubTypeContext(null, null, null); } return stubTypeContext; }