下面列出了org.eclipse.jdt.core.dom.ClassInstanceCreation#arguments ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean visit(ClassInstanceCreation node) {
if (inFrame) {
List arguments = node.arguments();
if (arguments.size() > 0) {
for (int i = 0; i < arguments.size(); i++) {
Expression exp = (Expression) arguments.get(i);
if (exp instanceof SimpleName) {
AbstractDebugVariableCodeMining<IJavaStackFrame> m = new JavaDebugElementCodeMining(
(SimpleName) exp, fFrame, viewer, provider);
minings.add(m);
}
}
}
}
return super.visit(node);
}
@Override
public boolean visit(ClassInstanceCreation node) {
/*
* if (Utils.isGeneratedByLombok(node)) { return super.visit(node); }
*/
if (showParameterName || showParameterType) {
List arguments = node.arguments();
if (arguments.size() > 0 && acceptMethod(node)) {
for (int i = 0; i < arguments.size(); i++) {
Expression exp = (Expression) arguments.get(i);
if (showParameterOnlyForLiteral && !isLiteral(exp)) {
continue;
}
minings.add(new JavaMethodParameterCodeMining(node, exp, i, cu, provider, showParameterName,
showParameterType, showParameterByUsingFilters));
}
}
}
return super.visit(node);
}
protected MethodInvocation createValueOfInvocation(ASTRewrite rewrite, CompilationUnit compilationUnit,
ClassInstanceCreation primitiveTypeCreation) {
Assert.isNotNull(rewrite);
Assert.isNotNull(primitiveTypeCreation);
final AST ast = rewrite.getAST();
MethodInvocation valueOfInvocation = ast.newMethodInvocation();
valueOfInvocation.setName(ast.newSimpleName(VALUE_OF_METHOD_NAME));
ITypeBinding binding = primitiveTypeCreation.getType().resolveBinding();
if (isStaticImport()) {
addStaticImports(rewrite, compilationUnit, binding.getQualifiedName() + "." + VALUE_OF_METHOD_NAME);
} else {
valueOfInvocation.setExpression(ast.newSimpleName(binding.getName()));
}
List<?> arguments = primitiveTypeCreation.arguments();
List<Expression> newArguments = valueOfInvocation.arguments();
for (Object argument : arguments) {
Expression expression = (Expression) rewrite.createCopyTarget((ASTNode) argument);
newArguments.add(expression);
}
return valueOfInvocation;
}
@SuppressWarnings("rawtypes")
@Override
public boolean visit(ClassInstanceCreation node) {
List args = node.arguments();
Map<String, Integer> scopeBindings = getNodeScopes().get(node);
List<String> argTypes = translateArgsToTypes(args, scopeBindings);
String type = getNameOfType(node.getType());
if (!methodStack.empty()) {
MethodDeclaration currentMethod = methodStack.peek();
MethodDecl methodDecl = getMethodDecl(currentMethod);
List<MethodInvokRef> invoks = methodInvoks.get(methodDecl);
if (invoks == null) {
invoks = new ArrayList<>();
methodInvoks.put(methodDecl, invoks);
}
MethodInvokRef methodInvokRef = new MethodInvokRef(removeSpecialSymbols(node.getType().toString()),
type, "", args
.size(), node.getStartPosition(), argTypes, node.getLength(), true,
getReturnType(node));
invoks.add(methodInvokRef);
}
return super.visit(node);
}
public ObjectCreation(CompilationUnit cu, String filePath, ClassInstanceCreation creation) {
this.locationInfo = new LocationInfo(cu, filePath, creation, CodeElementType.CLASS_INSTANCE_CREATION);
this.type = UMLType.extractTypeObject(cu, filePath, creation.getType(), 0);
this.typeArguments = creation.arguments().size();
this.arguments = new ArrayList<String>();
List<Expression> args = creation.arguments();
for(Expression argument : args) {
this.arguments.add(argument.toString());
}
if(creation.getExpression() != null) {
this.expression = creation.getExpression().toString();
}
if(creation.getAnonymousClassDeclaration() != null) {
this.anonymousClassDeclaration = creation.getAnonymousClassDeclaration().toString();
}
}
@Override
public void endVisit(ClassInstanceCreation node) {
Expression receiver= node.getExpression();
Type createdType= node.getType();
ConstraintVariable2 typeCv;
if (node.getAnonymousClassDeclaration() == null) {
typeCv= getConstraintVariable(createdType);
} else {
typeCv= fTCModel.makeImmutableTypeVariable(createdType.resolveBinding(), null);
setConstraintVariable(createdType, typeCv);
}
setConstraintVariable(node, typeCv);
IMethodBinding methodBinding= node.resolveConstructorBinding();
Map<String, IndependentTypeVariable2> methodTypeVariables= createMethodTypeArguments(methodBinding);
List<Expression> arguments= node.arguments();
doVisitMethodInvocationArguments(methodBinding, arguments, receiver, methodTypeVariables, createdType);
}
protected void replaceThisExpressionWithContextParameterInClassInstanceCreationArguments(Statement newStatement, AST subclassAST, ASTRewrite subclassRewriter) {
ExpressionExtractor expressionExtractor = new ExpressionExtractor();
List<Expression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(newStatement);
for(Expression creation : classInstanceCreations) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)creation;
List<Expression> arguments = classInstanceCreation.arguments();
for(Expression argument : arguments) {
if(argument instanceof ThisExpression) {
String parameterName = sourceTypeDeclaration.getName().getIdentifier();
parameterName = parameterName.substring(0,1).toLowerCase() + parameterName.substring(1,parameterName.length());
ListRewrite argumentsRewrite = subclassRewriter.getListRewrite(classInstanceCreation, ClassInstanceCreation.ARGUMENTS_PROPERTY);
argumentsRewrite.replace(argument, subclassAST.newSimpleName(parameterName), null);
}
}
}
}
private void replaceThisExpressionWithSourceClassParameterInClassInstanceCreationArguments(MethodDeclaration newMethodDeclaration, ASTRewrite targetRewriter) {
ExpressionExtractor extractor = new ExpressionExtractor();
List<Expression> classInstanceCreations = extractor.getClassInstanceCreations(newMethodDeclaration.getBody());
for(Expression creation : classInstanceCreations) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)creation;
List<Expression> arguments = classInstanceCreation.arguments();
for(Expression argument : arguments) {
if(argument instanceof ThisExpression) {
SimpleName parameterName = null;
if(!additionalArgumentsAddedToMovedMethod.contains("this")) {
parameterName = addSourceClassParameterToMovedMethod(newMethodDeclaration, targetRewriter);
}
else {
AST ast = newMethodDeclaration.getAST();
String sourceTypeName = sourceTypeDeclaration.getName().getIdentifier();
parameterName = ast.newSimpleName(sourceTypeName.replaceFirst(Character.toString(sourceTypeName.charAt(0)), Character.toString(Character.toLowerCase(sourceTypeName.charAt(0)))));
}
ListRewrite argumentRewrite = targetRewriter.getListRewrite(classInstanceCreation, ClassInstanceCreation.ARGUMENTS_PROPERTY);
argumentRewrite.replace(argument, parameterName, null);
}
}
}
}
private ClassInstanceCreate visit(ClassInstanceCreation node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
ClassInstanceCreate classInstanceCreate = new ClassInstanceCreate(startLine, endLine, node);
if(node.getExpression() != null){
Expr expression = (Expr) process(node.getExpression());
expression.setParent(classInstanceCreate);
classInstanceCreate.setExpression(expression);
}
if(node.getAnonymousClassDeclaration() != null){
AnonymousClassDecl anonymousClassDecl = (AnonymousClassDecl) process(node.getAnonymousClassDeclaration());
anonymousClassDecl.setParent(classInstanceCreate);
classInstanceCreate.setAnonymousClassDecl(anonymousClassDecl);
}
List<Expr> arguments = new ArrayList<>();
for(Object object : node.arguments()){
Expr arg = (Expr) process((ASTNode) object);
arg.setParent(classInstanceCreate);
arguments.add(arg);
}
classInstanceCreate.setArguments(arguments);
classInstanceCreate.setClassType(node.getType());
classInstanceCreate.setType(node.getType());
return classInstanceCreate;
}
public static String processClassInstanceCreation(ClassInstanceCreation node) {
StringBuilder sb = new StringBuilder();
sb.append("new").append(" ");
sb.append(node.getType().toString());
List<Expression> arguments = node.arguments();
if(arguments.size() > 0) {
for(int i=0; i<arguments.size()-1; i++)
sb.append(arguments.get(i).toString()).append(", ");
sb.append(arguments.get(arguments.size()-1).toString());
}
sb.append(")");
return sb.toString();
}
public Expression createDefaultExpression(List<Expression> invocationArguments, ParameterInfo addedInfo, List<ParameterInfo> parameterInfos, MethodDeclaration enclosingMethod, boolean isRecursive, CompilationUnitRewrite cuRewrite) {
final AST ast= cuRewrite.getAST();
final ASTRewrite rewrite= cuRewrite.getASTRewrite();
if (isRecursive && canReuseParameterObject(invocationArguments, addedInfo, parameterInfos, enclosingMethod)) {
return ast.newSimpleName(addedInfo.getNewName());
}
ClassInstanceCreation classCreation= ast.newClassInstanceCreation();
int startPosition= enclosingMethod != null ? enclosingMethod.getStartPosition() : cuRewrite.getRoot().getStartPosition();
ContextSensitiveImportRewriteContext context= fParameterObjectFactory.createParameterClassAwareContext(fCreateAsTopLevel, cuRewrite, startPosition);
classCreation.setType(fParameterObjectFactory.createType(fCreateAsTopLevel, cuRewrite, startPosition));
List<Expression> constructorArguments= classCreation.arguments();
for (Iterator<ParameterInfo> iter= parameterInfos.iterator(); iter.hasNext();) {
ParameterInfo pi= iter.next();
if (isValidField(pi)) {
if (pi.isOldVarargs()) {
boolean isLastParameter= !iter.hasNext();
constructorArguments.addAll(computeVarargs(invocationArguments, pi, isLastParameter, cuRewrite, context));
} else {
Expression exp= invocationArguments.get(pi.getOldIndex());
importNodeTypes(exp, cuRewrite, context);
constructorArguments.add(moveNode(exp, rewrite));
}
}
}
return classCreation;
}
/**
* Create the list of actual arguments to the constructor call that is
* encapsulated inside the factory method, and associate the arguments
* with the given constructor call object.
* @param ast utility object used to create AST nodes
* @param newCtorCall the newly-generated constructor call to be wrapped inside
* the factory method
*/
private void createFactoryMethodConstructorArgs(AST ast, ClassInstanceCreation newCtorCall) {
List<Expression> argList= newCtorCall.arguments();
for(int i=0; i < fArgTypes.length; i++) {
ASTNode ctorArg= ast.newSimpleName(fFormalArgNames[i]);
argList.add((Expression) ctorArg);
}
}
@Override
public ITypeConstraint[] create(ClassInstanceCreation instanceCreation){
List<Expression> arguments= instanceCreation.arguments();
List<ITypeConstraint> result= new ArrayList<ITypeConstraint>(arguments.size());
IMethodBinding methodBinding= instanceCreation.resolveConstructorBinding();
result.addAll(Arrays.asList(getArgumentConstraints(arguments, methodBinding)));
if (instanceCreation.getAnonymousClassDeclaration() == null){
ConstraintVariable constructorVar= fConstraintVariableFactory.makeExpressionOrTypeVariable(instanceCreation, getContext());
ConstraintVariable typeVar= fConstraintVariableFactory.makeRawBindingVariable(instanceCreation.resolveTypeBinding());
result.addAll(Arrays.asList(fTypeConstraintFactory.createDefinesConstraint(constructorVar, typeVar)));
}
return result.toArray(new ITypeConstraint[result.size()]);
}
@Override
public void write(ASTNode node) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) node;
//TODO: Handle type arguments
//TODO: Handle different reference operator used for stack objects
// TODO: Support inner class creation via object.new
if (classInstanceCreation.getExpression() != null)
throw sourceNotSupported("Inner classes not yet supported");
matchAndWrite("new");
copySpaceAndComments();
swiftASTWriters.writeNode(classInstanceCreation.getType());
copySpaceAndComments();
matchAndWrite("(");
copySpaceAndComments();
List<?> arguments = classInstanceCreation.arguments();
boolean first = true;
for (Object object : arguments) {
Expression argument = (Expression) object;
if (! first) {
matchAndWrite(",");
copySpaceAndComments();
}
swiftASTWriters.writeNode(argument);
copySpaceAndComments();
first = false;
}
matchAndWrite(")");
}
@Override
public void write(ClassInstanceCreation classInstanceCreation) {
//TODO: Handle type arguments
//TODO: Handle different reference operator used for stack objects
// TODO: Support inner class creation via object.new
if (classInstanceCreation.getExpression() != null)
throw sourceNotSupported("Inner classes not yet supported");
matchAndWrite("new");
copySpaceAndComments();
writeNode(classInstanceCreation.getType());
copySpaceAndComments();
matchAndWrite("(");
copySpaceAndComments();
List<?> arguments = classInstanceCreation.arguments();
boolean first = true;
for (Object object : arguments) {
Expression argument = (Expression) object;
if (! first) {
matchAndWrite(",");
copySpaceAndComments();
}
writeNode(argument);
copySpaceAndComments();
first = false;
}
matchAndWrite(")");
}