下面列出了org.eclipse.jdt.core.dom.ClassInstanceCreation#setAnonymousClassDeclaration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ASTNode createNewClassInstanceCreation(CompilationUnitRewrite rewrite, ITypeBinding[] parameters) {
AST ast= fAnonymousInnerClassNode.getAST();
ClassInstanceCreation newClassCreation= ast.newClassInstanceCreation();
newClassCreation.setAnonymousClassDeclaration(null);
Type type= null;
SimpleName newNameNode= ast.newSimpleName(fClassName);
if (parameters.length > 0) {
final ParameterizedType parameterized= ast.newParameterizedType(ast.newSimpleType(newNameNode));
for (int index= 0; index < parameters.length; index++)
parameterized.typeArguments().add(ast.newSimpleType(ast.newSimpleName(parameters[index].getName())));
type= parameterized;
} else
type= ast.newSimpleType(newNameNode);
newClassCreation.setType(type);
copyArguments(rewrite, newClassCreation);
addArgumentsForLocalsUsedInInnerClass(newClassCreation);
addLinkedPosition(KEY_TYPE_NAME, newNameNode, rewrite.getASTRewrite(), true);
return newClassCreation;
}
private ClassInstanceCreation createPrivilegedActionCreation(ASTRewrite rewrite, ClassInstanceCreation classLoaderCreation) {
AST ast = rewrite.getAST();
ClassInstanceCreation privilegedActionCreation = ast.newClassInstanceCreation();
ParameterizedType privilegedActionType = createPrivilegedActionType(rewrite, classLoaderCreation);
AnonymousClassDeclaration anonymousClassDeclaration = createAnonymousClassDeclaration(rewrite, classLoaderCreation);
privilegedActionCreation.setType(privilegedActionType);
privilegedActionCreation.setAnonymousClassDeclaration(anonymousClassDeclaration);
return privilegedActionCreation;
}
/**
* Creates and returns a new MethodDeclaration that represents the factory method to be used in
* place of direct calls to the constructor in question.
*
* @param ast An AST used as a factory for various AST nodes
* @param ctorBinding binding for the constructor being wrapped
* @param unitRewriter the ASTRewrite to be used
* @return the new method declaration
* @throws CoreException if an exception occurs while accessing its corresponding resource
*/
private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) throws CoreException{
MethodDeclaration newMethod= ast.newMethodDeclaration();
SimpleName newMethodName= ast.newSimpleName(fNewMethodName);
ClassInstanceCreation newCtorCall= ast.newClassInstanceCreation();
ReturnStatement ret= ast.newReturnStatement();
Block body= ast.newBlock();
List<Statement> stmts= body.statements();
String retTypeName= ctorBinding.getName();
createFactoryMethodSignature(ast, newMethod);
newMethod.setName(newMethodName);
newMethod.setBody(body);
ITypeBinding declaringClass= fCtorBinding.getDeclaringClass();
ITypeBinding[] ctorOwnerTypeParameters= declaringClass.getTypeParameters();
setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast);
newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC));
setCtorTypeArguments(newCtorCall, retTypeName, ctorOwnerTypeParameters, ast);
createFactoryMethodConstructorArgs(ast, newCtorCall);
if (Modifier.isAbstract(declaringClass.getModifiers())) {
AnonymousClassDeclaration decl= ast.newAnonymousClassDeclaration();
IMethodBinding[] unimplementedMethods= getUnimplementedMethods(declaringClass);
CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fCUHandle.getJavaProject());
ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fFactoryCU, decl.getStartPosition(), fImportRewriter);
for (int i= 0; i < unimplementedMethods.length; i++) {
IMethodBinding unImplementedMethod= unimplementedMethods[i];
MethodDeclaration newMethodDecl= StubUtility2.createImplementationStub(fCUHandle, unitRewriter, fImportRewriter, context, unImplementedMethod, unImplementedMethod.getDeclaringClass()
.getName(), settings, false);
decl.bodyDeclarations().add(newMethodDecl);
}
newCtorCall.setAnonymousClassDeclaration(decl);
}
ret.setExpression(newCtorCall);
stmts.add(ret);
return newMethod;
}