List of usage examples for org.eclipse.jdt.core.dom TypeLiteral getAST
public final AST getAST()
From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java
License:Open Source License
/** * Ensures that given {@link TypeDeclaration} interface with given name. * /*from ww w .ja v a 2 s. c om*/ * @return <code>false</code> if class already implements given interface and <code>true</code> in * other case. */ public boolean ensureInterfaceImplementation(TypeDeclaration type, String interfaceClassName) throws Exception { // find last implement interface and check, may be required interface is already implemented Type lastInterface = null; for (Type interfaceType : DomGenerics.superInterfaces(type)) { String implementedInterface = AstNodeUtils.getFullyQualifiedName(interfaceType, true); if (implementedInterface.equals(interfaceClassName)) { return false; } lastInterface = interfaceType; } // prepare position int pos; String codePrefix; if (lastInterface == null) { ASTNode typeName = type.getName(); if (type.getSuperclassType() != null) { typeName = type.getSuperclassType(); } pos = typeName.getStartPosition() + typeName.getLength(); codePrefix = " implements "; } else { pos = lastInterface.getStartPosition() + lastInterface.getLength(); codePrefix = ", "; } // prepare new interfaceType node SimpleType interfaceType; { TypeLiteral typeLiteral = (TypeLiteral) m_parser.parseExpression(type.getStartPosition(), interfaceClassName + ".class"); AstNodeUtils.moveNode(typeLiteral, pos + codePrefix.length()); interfaceType = (SimpleType) typeLiteral.getType(); typeLiteral.setType(typeLiteral.getAST().newPrimitiveType(PrimitiveType.BOOLEAN)); } // update source/AST replaceSubstring(pos, 0, codePrefix + interfaceClassName); DomGenerics.superInterfaces(type).add(interfaceType); // update ITypeBinding { ITypeBinding interfaceBinding = AstNodeUtils.getTypeBinding(interfaceType); DesignerTypeBinding typeBinding = getDesignerTypeBinding(type); typeBinding.addInterface(interfaceBinding); } // finalize resolveImports(interfaceType); return true; }
From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java
License:Open Source License
/** * Ensures that {@link MethodDeclaration} declares as thrown exception with given name, or its * superclass.//from w w w . jav a 2s. c om */ public void ensureThrownException(MethodDeclaration method, String requiredException) throws Exception { ITypeHierarchy requiredHierarchy; { IType requiredExceptionType = getJavaProject().findType(requiredException); Assert.isNotNull2(requiredExceptionType, "No such exception type: {0}", requiredException); requiredHierarchy = requiredExceptionType.newSupertypeHierarchy(null); } // find last exception and check, may be required exception is already declared Name lastName = null; for (Name declaredTypeName : DomGenerics.thrownExceptions(method)) { String declaredName = AstNodeUtils.getFullyQualifiedName(declaredTypeName, false); IType declaredType = getJavaProject().findType(declaredName); if (requiredHierarchy.contains(declaredType)) { return; } lastName = declaredTypeName; } // prepare position int pos; String codePrefix; if (lastName == null) { ASTNode name = method.getName(); pos = AstNodeUtils.getSourceEnd(name); pos = indexOf(")", pos) + 1; codePrefix = " throws "; } else { pos = AstNodeUtils.getSourceEnd(lastName); codePrefix = ", "; } // prepare new exception nodes SimpleType newExceptionType; Name newExceptionTypeName; { TypeLiteral typeLiteral = (TypeLiteral) m_parser.parseExpression(pos, requiredException + ".class"); AstNodeUtils.moveNode(typeLiteral, pos + codePrefix.length()); newExceptionType = (SimpleType) typeLiteral.getType(); newExceptionTypeName = newExceptionType.getName(); newExceptionType.setName(typeLiteral.getAST().newSimpleName("filler")); } // update source/AST replaceSubstring(pos, 0, codePrefix + requiredException); DomGenerics.thrownExceptions(method).add(newExceptionTypeName); // update ITypeBinding { ITypeBinding newExceptionBinding = AstNodeUtils.getTypeBinding(newExceptionType); DesignerMethodBinding methodBinding = getDesignerMethodBinding(method); methodBinding.addExceptionType(newExceptionBinding); } // finalize resolveImports(method); }
From source file:org.wloka.reflectify.internal.assist.ClassAccessProposalCreator.java
License:Open Source License
public IJavaCompletionProposal createProposal(ASTNode node, IInvocationContext context) { if (node == null || node.getNodeType() != ASTNode.TYPE_LITERAL) { throw new IllegalArgumentException(ReflectifyMessages.NoOrIllegalNodeError); }/*from ww w . jav a 2 s . c o m*/ TypeLiteral litNode = (TypeLiteral) node; ITypeBinding typeBind = litNode.getType().resolveBinding(); if (typeBind == null) { throw new IllegalArgumentException(ReflectifyMessages.BindingResolutionError); } AST ast = litNode.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); MethodInvocation methInv = createClassForName(typeBind, ast); rewrite.replace(litNode, methInv, null); // no edit group return new ASTRewriteCorrectionProposal(ReflectifyMessages.ReflectifyAssistProcessor_class, context.getCompilationUnit(), rewrite, getRelevance(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL)); }