use of org.eclipse.jdt.core.dom.PrimitiveType in project processing by processing.
the class CompletionGenerator method extracTypeInfo.
/**
* Find the SimpleType from FD, SVD, VDS, etc
*
* @param node
* @return
*/
public static SimpleType extracTypeInfo(ASTNode node) {
if (node == null) {
return null;
}
Type t = extracTypeInfo2(node);
if (t instanceof PrimitiveType) {
return null;
} else if (t instanceof ArrayType) {
ArrayType at = (ArrayType) t;
log("ele type " + at.getElementType() + ", " + at.getElementType().getClass().getName());
if (at.getElementType() instanceof PrimitiveType) {
return null;
} else if (at.getElementType() instanceof SimpleType) {
return (SimpleType) at.getElementType();
} else
return null;
} else if (t instanceof ParameterizedType) {
ParameterizedType pmt = (ParameterizedType) t;
log(pmt.getType() + ", " + pmt.getType().getClass());
if (pmt.getType() instanceof SimpleType) {
return (SimpleType) pmt.getType();
} else
return null;
}
return (SimpleType) t;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project bndtools by bndtools.
the class AbstractBuildErrorDetailsHandler method createMethodMarkerData.
/**
* Create a marker on a Java Method
*
* @param javaProject
* @param className
* - the fully qualified class name (e.g java.lang.String)
* @param methodName
* @param methodSignature
* - signatures are in "internal form" e.g. (Ljava.lang.Integer;[Ljava/lang/String;Z)V
* @param markerAttributes
* - attributes that should be included in the marker, typically a message. The start and end points for
* the marker are added by this method.
* @param hasResolutions
* - true if the marker will have resolutions
* @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
* @throws JavaModelException
*/
public static final MarkerData createMethodMarkerData(IJavaProject javaProject, final String className, final String methodName, final String methodSignature, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
final CompilationUnit ast = createAST(javaProject, className);
if (ast == null)
return null;
ast.accept(new ASTVisitor() {
@Override
public boolean visit(MethodDeclaration methodDecl) {
if (matches(ast, methodDecl, methodName, methodSignature)) {
// Create the marker attribs here
markerAttributes.put(IMarker.CHAR_START, methodDecl.getStartPosition());
markerAttributes.put(IMarker.CHAR_END, methodDecl.getStartPosition() + methodDecl.getLength());
}
return false;
}
private boolean matches(CompilationUnit ast, MethodDeclaration methodDecl, String methodName, String signature) {
if ("<init>".equals(methodName)) {
if (!methodDecl.isConstructor()) {
return false;
}
} else if (!methodDecl.getName().getIdentifier().equals(methodName)) {
return false;
}
return getSignature(ast, methodDecl).equals(signature);
}
private String getSignature(CompilationUnit ast, MethodDeclaration methodDecl) {
StringBuilder signatureBuilder = new StringBuilder("(");
for (@SuppressWarnings("unchecked") Iterator<SingleVariableDeclaration> it = methodDecl.parameters().iterator(); it.hasNext(); ) {
SingleVariableDeclaration decl = it.next();
appendType(ast, signatureBuilder, decl.getType(), decl.getExtraDimensions());
}
signatureBuilder.append(")");
appendType(ast, signatureBuilder, methodDecl.getReturnType2(), 0);
return signatureBuilder.toString();
}
private void appendType(CompilationUnit ast, StringBuilder signatureBuilder, Type typeToAdd, int extraDimensions) {
for (int i = 0; i < extraDimensions; i++) {
signatureBuilder.append('[');
}
Type rovingType = typeToAdd;
if (rovingType == null) {
//A special return type for constructors, nice one Eclipse...
signatureBuilder.append("V");
} else {
if (rovingType.isArrayType()) {
ArrayType type = (ArrayType) rovingType;
int depth = type.getDimensions();
for (int i = 0; i < depth; i++) {
signatureBuilder.append('[');
}
//We still need to add the array component type, which might be primitive or a reference
rovingType = type.getElementType();
}
// Type erasure means that we should ignore parameters
if (rovingType.isParameterizedType()) {
rovingType = ((ParameterizedType) rovingType).getType();
}
if (rovingType.isPrimitiveType()) {
PrimitiveType type = (PrimitiveType) rovingType;
signatureBuilder.append(PRIMITIVES_TO_SIGNATURES.get(type.getPrimitiveTypeCode()));
} else if (rovingType.isSimpleType()) {
SimpleType type = (SimpleType) rovingType;
String name;
if (type.getName().isQualifiedName()) {
name = type.getName().getFullyQualifiedName();
} else {
name = getFullyQualifiedNameForSimpleName(ast, type.getName());
}
name = name.replace('.', '/');
signatureBuilder.append("L").append(name).append(";");
} else if (rovingType.isQualifiedType()) {
QualifiedType type = (QualifiedType) rovingType;
String name = type.getQualifier().toString().replace('.', '/') + '/' + type.getName().getFullyQualifiedName().replace('.', '/');
signatureBuilder.append("L").append(name).append(";");
} else {
throw new IllegalArgumentException("We hit an unknown type " + rovingType);
}
}
}
private String getFullyQualifiedNameForSimpleName(CompilationUnit ast, Name typeName) {
String name = typeName.getFullyQualifiedName();
@SuppressWarnings("unchecked") List<ImportDeclaration> ids = ast.imports();
for (ImportDeclaration id : ids) {
if (id.isStatic())
continue;
if (id.isOnDemand()) {
String packageName = id.getName().getFullyQualifiedName();
try {
if (ast.getJavaElement().getJavaProject().findType(packageName + "." + name) != null) {
name = packageName + '.' + name;
}
} catch (JavaModelException e) {
}
} else {
String importName = id.getName().getFullyQualifiedName();
if (importName.endsWith("." + name)) {
name = importName;
break;
}
}
}
if (name.indexOf('.') < 0) {
try {
if (ast.getJavaElement().getJavaProject().findType(name) == null) {
name = "java.lang." + name;
}
} catch (JavaModelException e) {
}
}
return name;
}
});
if (!markerAttributes.containsKey(IMarker.CHAR_START))
return null;
return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class StubUtility2 method createImplementationStub.
public static MethodDeclaration createImplementationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String[] parameterNames, String type, CodeGenerationSettings settings, boolean inInterface) throws CoreException {
Assert.isNotNull(imports);
Assert.isNotNull(rewrite);
AST ast = rewrite.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(getImplementationModifiers(ast, binding, inInterface, imports, context));
decl.setName(ast.newSimpleName(binding.getName()));
decl.setConstructor(false);
ITypeBinding bindingReturnType = binding.getReturnType();
if (bindingReturnType.isWildcardType()) {
ITypeBinding bound = bindingReturnType.getBound();
bindingReturnType = (bound != null) ? bound : bindingReturnType.getErasure();
}
IJavaProject javaProject = unit.getJavaProject();
if (JavaModelUtil.is50OrHigher(javaProject)) {
createTypeParameters(imports, context, ast, binding, decl);
} else {
bindingReturnType = bindingReturnType.getErasure();
}
decl.setReturnType2(imports.addImport(bindingReturnType, ast, context));
List<SingleVariableDeclaration> parameters = createParameters(javaProject, imports, context, ast, binding, parameterNames, decl);
createThrownExceptions(decl, binding, imports, context, ast);
String delimiter = unit.findRecommendedLineSeparator();
int modifiers = binding.getModifiers();
if (!(inInterface && Modifier.isAbstract(modifiers))) {
// 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();
ITypeBinding declaringType = binding.getDeclaringClass();
if (declaringType.isInterface()) {
String qualifier = imports.addImport(declaringType.getErasure(), context);
Name name = ASTNodeFactory.newName(ast, qualifier);
invocation.setQualifier(name);
}
invocation.setName(ast.newSimpleName(binding.getName()));
SingleVariableDeclaration varDecl = null;
for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
varDecl = iterator.next();
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);
}
}
String placeHolder = CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), false, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
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);
}
}
if (settings != null && settings.overrideAnnotation && JavaModelUtil.is50OrHigher(javaProject)) {
addOverrideAnnotation(javaProject, rewrite, decl, binding);
}
return decl;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class AbstractMethodCorrectionProposal method getStub.
private MethodDeclaration getStub(ASTRewrite rewrite, ASTNode targetTypeDecl) throws CoreException {
AST ast = targetTypeDecl.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
SimpleName newNameNode = getNewName(rewrite);
decl.setConstructor(isConstructor());
addNewModifiers(rewrite, targetTypeDecl, decl.modifiers());
ArrayList<String> takenNames = new ArrayList<String>();
addNewTypeParameters(rewrite, takenNames, decl.typeParameters());
decl.setName(newNameNode);
IVariableBinding[] declaredFields = fSenderBinding.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
// avoid to take parameter names that are equal to field names
takenNames.add(declaredFields[i].getName());
}
//$NON-NLS-1$
String bodyStatement = "";
if (!isConstructor()) {
Type returnType = getNewMethodType(rewrite);
decl.setReturnType2(returnType);
boolean isVoid = returnType instanceof PrimitiveType && PrimitiveType.VOID.equals(((PrimitiveType) returnType).getPrimitiveTypeCode());
if (!fSenderBinding.isInterface() && !isVoid) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
}
}
addNewParameters(rewrite, takenNames, decl.parameters());
addNewExceptions(rewrite, decl.thrownExceptionTypes());
Block body = null;
if (!fSenderBinding.isInterface()) {
body = ast.newBlock();
String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), fSenderBinding.getName(), newNameNode.getIdentifier(), isConstructor(), bodyStatement, String.valueOf('\n'));
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
decl.setBody(body);
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
if (settings.createComments && !fSenderBinding.isAnonymous()) {
String string = CodeGeneration.getMethodComment(getCompilationUnit(), fSenderBinding.getName(), decl, null, String.valueOf('\n'));
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
return decl;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class TypeChangeCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
CompilationUnit newRoot = fAstRoot;
if (boundNode != null) {
// is same CU
declNode = boundNode;
} else {
newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast = declNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = createImportRewrite(newRoot);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
Type type = imports.addImport(fNewType, ast, context);
if (declNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declNode;
Type origReturnType = methodDecl.getReturnType2();
rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
// add javadoc tag
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
if (returnTag == null) {
returnTag = ast.newTagElement();
returnTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
returnTag.fragments().add(commentStart);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
}
}
} else if (declNode instanceof AnnotationTypeMemberDeclaration) {
AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
} else if (declNode instanceof VariableDeclarationFragment) {
ASTNode parent = declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
newField.setType(type);
AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
if (fieldDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newField, parent, null);
} else {
listRewrite.insertAfter(newField, parent, null);
}
} else {
rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
newStat.setType(type);
ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
if (varDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newStat, parent, null);
} else {
listRewrite.insertAfter(newStat, parent, null);
}
} else {
rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (declNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
// set up linked mode
//$NON-NLS-1$
final String KEY_TYPE = "type";
addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
if (fTypeProposals != null) {
for (int i = 0; i < fTypeProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, fTypeProposals[i]);
}
}
return rewrite;
}
return null;
}
Aggregations