下面列出了org.eclipse.jdt.core.dom.ClassInstanceCreation#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean visit(final ClassInstanceCreation node) {
Assert.isNotNull(node);
if (fCreateInstanceField) {
final AST ast= node.getAST();
final Type type= node.getType();
final ITypeBinding binding= type.resolveBinding();
if (binding != null && binding.getDeclaringClass() != null && !Bindings.equals(binding, fTypeBinding) && fSourceRewrite.getRoot().findDeclaringNode(binding) != null) {
if (!Modifier.isStatic(binding.getModifiers())) {
Expression expression= null;
if (fCodeGenerationSettings.useKeywordThis || fEnclosingInstanceFieldName.equals(fNameForEnclosingInstanceConstructorParameter)) {
final FieldAccess access= ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(ast.newSimpleName(fEnclosingInstanceFieldName));
expression= access;
} else
expression= ast.newSimpleName(fEnclosingInstanceFieldName);
if (node.getExpression() != null)
fSourceRewrite.getImportRemover().registerRemovedNode(node.getExpression());
fSourceRewrite.getASTRewrite().set(node, ClassInstanceCreation.EXPRESSION_PROPERTY, expression, fGroup);
} else
addTypeQualification(type, fSourceRewrite, fGroup);
}
}
return true;
}
@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);
}
private void addParameterInfo(CompilationUnitRewrite cuRewrite) throws JavaModelException {
ITypeBinding typeBinding= Bindings.normalizeForDeclarationUse(fSelectedExpression.resolveTypeBinding(), fSelectedExpression.getAST());
String name= fParameterName != null ? fParameterName : guessedParameterName();
Expression expression= fSelectedExpression instanceof ParenthesizedExpression ? ((ParenthesizedExpression)fSelectedExpression).getExpression() : fSelectedExpression;
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(fSelectedExpression, importRewrite);
String typeName= importRewrite.addImport(typeBinding, importRewriteContext);
String defaultValue= null;
if (expression instanceof ClassInstanceCreation && typeBinding.isParameterizedType()) {
ClassInstanceCreation classInstanceCreation= (ClassInstanceCreation) expression;
Type cicType= classInstanceCreation.getType();
if (cicType instanceof ParameterizedType && ((ParameterizedType) cicType).typeArguments().size() == 0) {
// expand the diamond:
AST ast= cuRewrite.getAST();
Type type= importRewrite.addImport(typeBinding, ast, importRewriteContext);
classInstanceCreation.setType(type); // Should not touch the original AST ...
defaultValue= ASTNodes.asFormattedString(classInstanceCreation, 0, StubUtility.getLineDelimiterUsed(cuRewrite.getCu()), cuRewrite.getCu().getJavaProject().getOptions(true));
classInstanceCreation.setType(cicType); // ... so let's restore it right away.
}
}
if (defaultValue == null) {
defaultValue= fSourceCU.getBuffer().getText(expression.getStartPosition(), expression.getLength());
}
fParameter= ParameterInfo.createInfoForAddedParameter(typeBinding, typeName, name, defaultValue);
if (fArguments == null) {
List<ParameterInfo> parameterInfos= fChangeSignatureProcessor.getParameterInfos();
int parametersCount= parameterInfos.size();
if (parametersCount > 0 && parameterInfos.get(parametersCount - 1).isOldVarargs())
parameterInfos.add(parametersCount - 1, fParameter);
else
parameterInfos.add(fParameter);
}
}
@Override
public boolean visit(ClassInstanceCreation node) {
if (matches(node.resolveConstructorBinding())) {
Type type= node.getType();
fResult.add(new OccurrenceLocation(type.getStartPosition(), type.getLength(), 0, fDescription));
}
return super.visit(node);
}
@Override
public boolean visit(ClassInstanceCreation node) {
if (isExitPoint(node.resolveConstructorBinding())) {
Type name= node.getType();
fResult.add(new OccurrenceLocation(name.getStartPosition(), name.getLength(), 0, fExitDescription));
}
return true;
}
public boolean isTypeCheckMethodStateSetter() {
InheritanceTree tree = null;
if(existingInheritanceTree != null)
tree = existingInheritanceTree;
else if(inheritanceTreeMatchingWithStaticTypes != null)
tree = inheritanceTreeMatchingWithStaticTypes;
if(tree != null) {
DefaultMutableTreeNode root = tree.getRootNode();
DefaultMutableTreeNode leaf = root.getFirstLeaf();
List<String> subclassNames = new ArrayList<String>();
while(leaf != null) {
subclassNames.add((String)leaf.getUserObject());
leaf = leaf.getNextLeaf();
}
Block typeCheckMethodBody = typeCheckMethod.getBody();
List<Statement> statements = typeCheckMethodBody.statements();
if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
List<Statement> statements2 = switchStatement.statements();
ExpressionExtractor expressionExtractor = new ExpressionExtractor();
int matchCounter = 0;
for(Statement statement2 : statements2) {
if(!(statement2 instanceof SwitchCase) && !(statement2 instanceof BreakStatement)) {
List<Expression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(statement2);
if(classInstanceCreations.size() == 1) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)classInstanceCreations.get(0);
Type classInstanceCreationType = classInstanceCreation.getType();
if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
matchCounter++;
}
}
}
}
if(matchCounter == subclassNames.size())
return true;
}
}
return false;
}
public boolean visit(org.eclipse.jdt.core.dom.ThrowStatement node)
{
if (node.getExpression() instanceof ClassInstanceCreation)
{
ClassInstanceCreation cic = (ClassInstanceCreation) node.getExpression();
Type type = cic.getType();
processType(type, TypeReferenceLocation.THROW_STATEMENT, compilationUnit.getLineNumber(node.getStartPosition()),
compilationUnit.getColumnNumber(cic.getStartPosition()), cic.getLength(), node.toString());
}
return super.visit(node);
}
private boolean typeObjectGetterMethodAlreadyExists() {
InheritanceTree tree = typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes();
if(tree != null) {
MethodDeclaration[] contextMethods = sourceTypeDeclaration.getMethods();
DefaultMutableTreeNode rootNode = tree.getRootNode();
String rootClassName = (String)rootNode.getUserObject();
DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
List<String> subclassNames = new ArrayList<String>();
while(leaf != null) {
subclassNames.add((String)leaf.getUserObject());
leaf = leaf.getNextLeaf();
}
for(MethodDeclaration contextMethod : contextMethods) {
Type returnType = contextMethod.getReturnType2();
if(returnType != null) {
if(returnType.resolveBinding().getQualifiedName().equals(rootClassName)) {
Block contextMethodBody = contextMethod.getBody();
if(contextMethodBody != null) {
List<Statement> statements = contextMethodBody.statements();
if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
List<Statement> statements2 = switchStatement.statements();
int matchCounter = 0;
for(Statement statement2 : statements2) {
if(statement2 instanceof ReturnStatement) {
ReturnStatement returnStatement = (ReturnStatement)statement2;
Expression returnStatementExpression = returnStatement.getExpression();
if(returnStatementExpression instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)returnStatementExpression;
Type classInstanceCreationType = classInstanceCreation.getType();
if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
matchCounter++;
}
}
}
}
if(matchCounter == subclassNames.size())
return true;
}
}
}
}
}
}
return false;
}