Java Code Examples for org.eclipse.jdt.core.dom.ClassInstanceCreation#getType()

The following examples show how to use org.eclipse.jdt.core.dom.ClassInstanceCreation#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: InferTypeArgumentsConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: IntroduceParameterRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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);
	}
}
 
Example 4
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: MethodExitsFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example 6
Source File: TypeCheckElimination.java    From JDeodorant with MIT License 5 votes vote down vote up
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;
}
 
Example 7
Source File: ReferenceResolvingVisitor.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 4 votes vote down vote up
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;
}