use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project liferay-ide by liferay.
the class JavaFileJDT method findCatchExceptions.
@Override
public List<SearchResult> findCatchExceptions(String[] exceptions) {
List<SearchResult> searchResults = new ArrayList<>();
_ast.accept(new ASTVisitor() {
@Override
public boolean visit(CatchClause node) {
SingleVariableDeclaration exception = node.getException();
String exceptionTypeName = exception.getType().toString();
boolean retVal = false;
for (String exceptionType : exceptions) {
if (exceptionTypeName.equals(exceptionType)) {
int startLine = _ast.getLineNumber(exception.getStartPosition());
int startOffset = exception.getStartPosition();
int endLine = _ast.getLineNumber(exception.getStartPosition() + exception.getLength());
int endOffset = exception.getStartPosition() + exception.getLength();
searchResults.add(createSearchResult(exceptionTypeName, startOffset, endOffset, startLine, endLine, true));
retVal = true;
}
}
return retVal;
}
});
return searchResults;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method createParameters.
// private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl) {
// return createParameters(project, imports, context, ast, binding, paramNames, decl, null);
// }
private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl, IAnnotationBinding defaultNullness) {
boolean is50OrHigher = JavaModelUtil.is50OrHigher(project);
List<SingleVariableDeclaration> parameters = decl.parameters();
ITypeBinding[] params = binding.getParameterTypes();
if (paramNames == null || paramNames.length < params.length) {
paramNames = StubUtility.suggestArgumentNames(project, binding);
}
for (int i = 0; i < params.length; i++) {
SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
ITypeBinding type = params[i];
type = replaceWildcardsAndCaptures(type);
if (!is50OrHigher) {
type = type.getErasure();
var.setType(imports.addImport(type, ast, context, TypeLocation.PARAMETER));
} else if (binding.isVarargs() && type.isArray() && i == params.length - 1) {
var.setVarargs(true);
/*
* Varargs annotations are special.
* Example:
* foo(@O Object @A [] @B ... arg)
* => @B is not an annotation on the array dimension that constitutes the vararg.
* It's the type annotation of the *innermost* array dimension.
*/
int dimensions = type.getDimensions();
@SuppressWarnings("unchecked") List<Annotation>[] dimensionAnnotations = (List<Annotation>[]) new List<?>[dimensions];
for (int dim = 0; dim < dimensions; dim++) {
dimensionAnnotations[dim] = new ArrayList<>();
for (IAnnotationBinding annotation : type.getTypeAnnotations()) {
dimensionAnnotations[dim].add(imports.addAnnotation(annotation, ast, context));
}
type = type.getComponentType();
}
Type elementType = imports.addImport(type, ast, context);
if (dimensions == 1) {
var.setType(elementType);
} else {
ArrayType arrayType = ast.newArrayType(elementType, dimensions - 1);
List<Dimension> dimensionNodes = arrayType.dimensions();
for (int dim = 0; dim < dimensions - 1; dim++) {
// all except the innermost dimension
Dimension dimension = dimensionNodes.get(dim);
dimension.annotations().addAll(dimensionAnnotations[dim]);
}
var.setType(arrayType);
}
List<Annotation> varargTypeAnnotations = dimensionAnnotations[dimensions - 1];
var.varargsAnnotations().addAll(varargTypeAnnotations);
} else {
var.setType(imports.addImport(type, ast, context, TypeLocation.PARAMETER));
}
var.setName(ast.newSimpleName(paramNames[i]));
IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project, defaultNullness)) {
var.modifiers().add(imports.addAnnotation(annotation, ast, context));
}
}
parameters.add(var);
}
return parameters;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method createImplementationStub.
public static MethodDeclaration createImplementationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String[] parameterNames, ITypeBinding targetType, CodeGenerationSettings settings, boolean inInterface, IBinding contextBinding, boolean snippetStringSupport) throws CoreException {
Assert.isNotNull(imports);
Assert.isNotNull(rewrite);
AST ast = rewrite.getAST();
String type = Bindings.getTypeQualifiedName(targetType);
IJavaProject javaProject = unit.getJavaProject();
IAnnotationBinding nullnessDefault = null;
if (contextBinding != null && JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
nullnessDefault = findNullnessDefault(contextBinding, javaProject);
}
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(getImplementationModifiers(ast, binding, inInterface, imports, context, nullnessDefault));
decl.setName(ast.newSimpleName(binding.getName()));
decl.setConstructor(false);
ITypeBinding bindingReturnType = binding.getReturnType();
bindingReturnType = StubUtility2.replaceWildcardsAndCaptures(bindingReturnType);
if (JavaModelUtil.is50OrHigher(javaProject)) {
createTypeParameters(imports, context, ast, binding, decl);
} else {
bindingReturnType = bindingReturnType.getErasure();
}
decl.setReturnType2(imports.addImport(bindingReturnType, ast, context, TypeLocation.RETURN_TYPE));
List<SingleVariableDeclaration> parameters = createParameters(javaProject, imports, context, ast, binding, parameterNames, decl, nullnessDefault);
createThrownExceptions(decl, binding, imports, context, ast);
String delimiter = unit.findRecommendedLineSeparator();
int modifiers = binding.getModifiers();
ITypeBinding declaringType = binding.getDeclaringClass();
// $NON-NLS-1$
ITypeBinding typeObject = ast.resolveWellKnownType("java.lang.Object");
if (!inInterface || (declaringType != typeObject && JavaModelUtil.is18OrHigher(javaProject))) {
// generate a method body
Map<String, String> options = javaProject.getOptions(true);
Block body = ast.newBlock();
decl.setBody(body);
// $NON-NLS-1$
String bodyStatement = "";
if (Modifier.isAbstract(modifiers)) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, decl.getReturnType2(), decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
} else {
SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
if (declaringType.isInterface()) {
ITypeBinding supertype = Bindings.findImmediateSuperTypeInHierarchy(targetType, declaringType.getTypeDeclaration().getQualifiedName());
if (supertype == null) {
// should not happen, but better use the type we have rather than failing
supertype = declaringType;
}
if (supertype.isInterface()) {
String qualifier = imports.addImport(supertype.getTypeDeclaration(), context);
Name name = ASTNodeFactory.newName(ast, qualifier);
invocation.setQualifier(name);
}
}
invocation.setName(ast.newSimpleName(binding.getName()));
for (SingleVariableDeclaration varDecl : parameters) {
invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
}
Expression expression = invocation;
Type returnType = decl.getReturnType2();
if (returnType instanceof PrimitiveType && ((PrimitiveType) returnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
bodyStatement = ASTNodes.asFormattedString(ast.newExpressionStatement(expression), 0, delimiter, options);
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
}
if (bodyStatement != null) {
StringBuilder placeHolder = new StringBuilder();
if (snippetStringSupport) {
placeHolder.append("${0");
if (!bodyStatement.isEmpty()) {
placeHolder.append(":");
}
bodyStatement = CompletionUtils.sanitizeCompletion(bodyStatement);
}
placeHolder.append(bodyStatement);
if (snippetStringSupport) {
placeHolder.append("}");
}
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder.toString(), ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
/* jdt.ls doesn't create comments at that point
if (settings != null && settings.createComments) {
String string= CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
if (string != null) {
Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
*/
// According to JLS8 9.2, an interface doesn't implicitly declare non-public members of Object,
// and JLS8 9.6.4.4 doesn't allow @Override for these methods (clone and finalize).
boolean skipOverride = inInterface && declaringType == typeObject && !Modifier.isPublic(modifiers);
if (!skipOverride) {
addOverrideAnnotation(settings, javaProject, rewrite, imports, decl, binding.getDeclaringClass().isInterface(), null);
}
return decl;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project eclipse.jdt.ls by eclipse.
the class ASTNodeFactory method newType.
/**
* Returns the new type node corresponding to the type of the given declaration
* including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
* If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
*
* @param ast The AST to create the resulting type with.
* @param declaration The variable declaration to get the type from
* @param importRewrite the import rewrite to use, or <code>null</code>
* @param context the import rewrite context, or <code>null</code>
* @return a new type node created with the given AST.
*
* @since 3.7.1
*/
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
}
Type type = ASTNodes.getType(declaration);
if (declaration instanceof SingleVariableDeclaration) {
Type type2 = ((SingleVariableDeclaration) declaration).getType();
if (type2 instanceof UnionType) {
ITypeBinding typeBinding = type2.resolveBinding();
if (typeBinding != null) {
if (importRewrite != null) {
type = importRewrite.addImport(typeBinding, ast, context);
return type;
} else {
String qualifiedName = typeBinding.getQualifiedName();
if (qualifiedName.length() > 0) {
type = ast.newSimpleType(ast.newName(qualifiedName));
return type;
}
}
}
// XXX: fallback for intersection types or unresolved types: take first type of union
type = (Type) ((UnionType) type2).types().get(0);
return type;
}
}
type = (Type) ASTNode.copySubtree(ast, type);
List<Dimension> extraDimensions = declaration.extraDimensions();
if (!extraDimensions.isEmpty()) {
ArrayType arrayType;
if (type instanceof ArrayType) {
arrayType = (ArrayType) type;
} else {
arrayType = ast.newArrayType(type, 0);
type = arrayType;
}
arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
}
return type;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project eclipse.jdt.ls by eclipse.
the class NewVariableCorrectionProposal method isEnhancedForStatementVariable.
private boolean isEnhancedForStatementVariable(Statement statement, SimpleName name) {
if (statement instanceof EnhancedForStatement) {
EnhancedForStatement forStatement = (EnhancedForStatement) statement;
SingleVariableDeclaration param = forStatement.getParameter();
// strange recovery, see https://bugs.eclipse.org/180456
return param.getType() == name.getParent();
}
return false;
}
Aggregations