use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project flux by eclipse.
the class ASTNodes method isExplicitlyTypedLambda.
private static boolean isExplicitlyTypedLambda(Expression expression) {
if (!(expression instanceof LambdaExpression))
return false;
LambdaExpression lambda = (LambdaExpression) expression;
List<VariableDeclaration> parameters = lambda.parameters();
if (parameters.isEmpty())
return true;
return parameters.get(0) instanceof SingleVariableDeclaration;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project flux by eclipse.
the class StubUtility method getMethodComment.
/*
* Don't use this method directly, use CodeGeneration.
* This method should work with all AST levels.
* @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
*/
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
if (decl.isConstructor()) {
templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
} else if (needsTarget) {
if (delegate)
templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
else
templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
}
Template template = getCodeTemplate(templateName, cu.getJavaProject());
if (template == null) {
return null;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
context.setCompilationUnitVariables(cu);
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
if (!decl.isConstructor()) {
context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
}
if (needsTarget) {
if (delegate)
context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
else
context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
}
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
if (buffer == null)
return null;
String str = buffer.getString();
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
// look if Javadoc tags have to be added
TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
if (position == null) {
return str;
}
IDocument textBuffer = new Document(str);
List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
String[] typeParamNames = new String[typeParams.size()];
for (int i = 0; i < typeParamNames.length; i++) {
TypeParameter elem = typeParams.get(i);
typeParamNames[i] = elem.getName().getIdentifier();
}
List<SingleVariableDeclaration> params = decl.parameters();
String[] paramNames = new String[params.size()];
for (int i = 0; i < paramNames.length; i++) {
SingleVariableDeclaration elem = params.get(i);
paramNames[i] = elem.getName().getIdentifier();
}
String[] exceptionNames = getExceptionNames(decl);
String returnType = null;
if (!decl.isConstructor()) {
returnType = ASTNodes.asString(getReturnType(decl));
}
int[] tagOffsets = position.getOffsets();
for (int i = tagOffsets.length - 1; i >= 0; i--) {
// from last to first
try {
insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
} catch (BadLocationException e) {
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
}
}
return textBuffer.get();
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project flux 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 generator by mybatis.
the class MethodSignatureStringifier method visitParameters.
@SuppressWarnings("unchecked")
private void visitParameters(MethodDeclaration node) {
for (Iterator<SingleVariableDeclaration> it = node.parameters().iterator(); it.hasNext(); ) {
SingleVariableDeclaration v = it.next();
v.accept(this);
if (it.hasNext()) {
buffer.append(',');
}
}
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final MethodDeclaration it) {
Javadoc _javadoc = it.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
it.getJavadoc().accept(this);
}
final Function1<ASTNode, StringBuffer> _function = (ASTNode node) -> {
StringBuffer _xifexpression = null;
if ((node instanceof MethodDeclaration)) {
StringBuffer _xifexpression_1 = null;
boolean _isConstructor = ((MethodDeclaration) node).isConstructor();
boolean _not = (!_isConstructor);
if (_not) {
StringBuffer _xifexpression_2 = null;
boolean _isOverrideMethod = this._aSTFlattenerUtils.isOverrideMethod(((MethodDeclaration) node));
if (_isOverrideMethod) {
_xifexpression_2 = this.appendToBuffer("override ");
} else {
_xifexpression_2 = this.appendToBuffer("def ");
}
_xifexpression_1 = _xifexpression_2;
}
_xifexpression = _xifexpression_1;
}
return _xifexpression;
};
final Function1<ASTNode, StringBuffer> afterAnnotationProcessingCallback = _function;
this.appendModifiers(it, it.modifiers(), afterAnnotationProcessingCallback);
boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
if (_isPackageVisibility) {
ASTNode _parent = it.getParent();
if ((_parent instanceof TypeDeclaration)) {
ASTNode _parent_1 = it.getParent();
boolean _isInterface = ((TypeDeclaration) _parent_1).isInterface();
boolean _not = (!_isInterface);
if (_not) {
this.appendToBuffer("package ");
}
}
}
boolean _isConstructor = it.isConstructor();
if (_isConstructor) {
this.appendToBuffer(" new");
}
boolean _isEmpty = it.typeParameters().isEmpty();
boolean _not_1 = (!_isEmpty);
if (_not_1) {
boolean _isConstructor_1 = it.isConstructor();
if (_isConstructor_1) {
this.addProblem(it, "Type parameters for constructors are not supported");
}
this.appendTypeParameters(it.typeParameters());
}
boolean _isConstructor_2 = it.isConstructor();
boolean _not_2 = (!_isConstructor_2);
if (_not_2) {
Type _returnType2 = it.getReturnType2();
boolean _tripleNotEquals_1 = (_returnType2 != null);
if (_tripleNotEquals_1) {
it.getReturnType2().accept(this);
} else {
this.appendToBuffer("void");
}
this.appendSpaceToBuffer();
it.getName().accept(this);
}
this.appendToBuffer("(");
final Consumer<SingleVariableDeclaration> _function_1 = (SingleVariableDeclaration p) -> {
Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), p.getName());
if ((_isAssignedInBody).booleanValue()) {
if ((it.isConstructor() && (!it.getBody().statements().isEmpty()))) {
final Expression firstInBody = IterableExtensions.<Expression>head(this._aSTFlattenerUtils.findAssignmentsInBlock(it.getBody(), p));
if ((firstInBody != null)) {
ConstructorInvocation _findParentOfType = this._aSTFlattenerUtils.<ConstructorInvocation>findParentOfType(firstInBody, ConstructorInvocation.class);
boolean _tripleNotEquals_2 = (_findParentOfType != null);
if (_tripleNotEquals_2) {
this.addProblem(p, "Final parameter modified in constructor call");
} else {
SuperConstructorInvocation _findParentOfType_1 = this._aSTFlattenerUtils.<SuperConstructorInvocation>findParentOfType(firstInBody, SuperConstructorInvocation.class);
boolean _tripleNotEquals_3 = (_findParentOfType_1 != null);
if (_tripleNotEquals_3) {
this.addProblem(p, "Final parameter modified in super constructor call");
}
}
}
}
final VariableDeclarationFragment varFrag = p.getAST().newVariableDeclarationFragment();
varFrag.setName(p.getAST().newSimpleName(p.getName().toString()));
AST _aST = p.getAST();
SimpleName _name = p.getName();
String _plus = (_name + "_finalParam_");
p.setName(_aST.newSimpleName(_plus));
varFrag.setInitializer(p.getAST().newSimpleName(p.getName().toString()));
final VariableDeclarationStatement varDecl = p.getAST().newVariableDeclarationStatement(varFrag);
ASTNode _createInstance = p.getAST().createInstance(SimpleType.class);
final Type typeCopy = ((Type) _createInstance);
varDecl.setType(typeCopy);
it.getBody().statements().add(0, varDecl);
}
};
ListExtensions.<SingleVariableDeclaration>reverseView(it.parameters()).forEach(_function_1);
this.visitAllSeparatedByComma(it.parameters());
this.appendToBuffer(")");
this.appendExtraDimensions(it.getExtraDimensions());
List<? extends ASTNode> throwsTypes = CollectionLiterals.<ASTNode>newArrayList();
boolean _java8orHigher = this.java8orHigher();
boolean _not_3 = (!_java8orHigher);
if (_not_3) {
throwsTypes = it.thrownExceptions();
} else {
throwsTypes = this._aSTFlattenerUtils.genericChildListProperty(it, "thrownExceptionTypes");
}
boolean _isEmpty_1 = throwsTypes.isEmpty();
boolean _not_4 = (!_isEmpty_1);
if (_not_4) {
this.appendToBuffer(" throws ");
this.visitAllSeparatedByComma(throwsTypes);
}
this.appendSpaceToBuffer();
Block _body = it.getBody();
boolean _tripleNotEquals_2 = (_body != null);
if (_tripleNotEquals_2) {
it.getBody().accept(this);
} else {
this.appendLineWrapToBuffer();
}
return false;
}
Aggregations