use of org.eclipse.jdt.core.ILocalVariable in project che by eclipse.
the class ParameterGuesser method createVariable.
public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException {
int variableType;
int elementType = element.getElementType();
String elementName = element.getElementName();
String typeSignature;
switch(elementType) {
case IJavaElement.FIELD:
{
IField field = (IField) element;
if (field.getDeclaringType().equals(enclosingType)) {
variableType = Variable.FIELD;
} else {
variableType = Variable.INHERITED_FIELD;
}
if (field.isResolved()) {
typeSignature = new BindingKey(field.getKey()).toSignature();
} else {
typeSignature = field.getTypeSignature();
}
break;
}
case IJavaElement.LOCAL_VARIABLE:
{
ILocalVariable locVar = (ILocalVariable) element;
variableType = Variable.LOCAL;
typeSignature = locVar.getTypeSignature();
break;
}
case IJavaElement.METHOD:
{
IMethod method = (IMethod) element;
if (isMethodToSuggest(method)) {
if (method.getDeclaringType().equals(enclosingType)) {
variableType = Variable.METHOD;
} else {
variableType = Variable.INHERITED_METHOD;
}
if (method.isResolved()) {
typeSignature = Signature.getReturnType(new BindingKey(method.getKey()).toSignature());
} else {
typeSignature = method.getReturnType();
}
//$NON-NLS-1$
elementName = elementName + "()";
} else {
return null;
}
break;
}
default:
return null;
}
String type = Signature.toString(typeSignature);
boolean isAutoboxMatch = isPrimitiveType(expectedType) != isPrimitiveType(type);
return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore, NO_TRIGGERS, getImageDescriptor(element));
}
use of org.eclipse.jdt.core.ILocalVariable in project che by eclipse.
the class JavaElementLabelComposer method appendMethodLabel.
/**
* Appends the label for a method. Considers the M_* flags.
*
* @param method the element to render
* @param flags the rendering flags. Flags with names starting with 'M_' are considered.
*/
public void appendMethodLabel(IMethod method, long flags) {
try {
BindingKey resolvedKey = getFlag(flags, JavaElementLabels.USE_RESOLVED) && method.isResolved() ? new BindingKey(method.getKey()) : null;
String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;
// type parameters
if (getFlag(flags, JavaElementLabels.M_PRE_TYPE_PARAMETERS)) {
if (resolvedKey != null) {
if (resolvedKey.isParameterizedMethod()) {
String[] typeArgRefs = resolvedKey.getTypeArguments();
if (typeArgRefs.length > 0) {
appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
fBuffer.append(' ');
}
} else {
String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
if (typeParameterSigs.length > 0) {
appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
fBuffer.append(' ');
}
}
} else if (method.exists()) {
ITypeParameter[] typeParameters = method.getTypeParameters();
if (typeParameters.length > 0) {
appendTypeParametersLabels(typeParameters, flags);
fBuffer.append(' ');
}
}
}
// return type
if (getFlag(flags, JavaElementLabels.M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) {
String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
appendTypeSignatureLabel(method, returnTypeSig, flags);
fBuffer.append(' ');
}
// qualification
if (getFlag(flags, JavaElementLabels.M_FULLY_QUALIFIED)) {
appendTypeLabel(method.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
fBuffer.append('.');
}
fBuffer.append(getElementName(method));
// constructor type arguments
if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS) && method.exists() && method.isConstructor()) {
if (resolvedSig != null && resolvedKey.isParameterizedType()) {
BindingKey declaringType = resolvedKey.getDeclaringType();
if (declaringType != null) {
String[] declaringTypeArguments = declaringType.getTypeArguments();
appendTypeArgumentSignaturesLabel(method, declaringTypeArguments, flags);
}
}
}
// parameters
fBuffer.append('(');
String[] declaredParameterTypes = method.getParameterTypes();
if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES)) {
String[] types = null;
int nParams = 0;
boolean renderVarargs = false;
boolean isPolymorphic = false;
if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES)) {
if (resolvedSig != null) {
types = Signature.getParameterTypes(resolvedSig);
} else {
types = declaredParameterTypes;
}
nParams = types.length;
renderVarargs = method.exists() && Flags.isVarargs(method.getFlags());
if (renderVarargs && resolvedSig != null && declaredParameterTypes.length == 1 && JavaModelUtil.isPolymorphicSignature(method)) {
renderVarargs = false;
isPolymorphic = true;
}
}
String[] names = null;
if (getFlag(flags, JavaElementLabels.M_PARAMETER_NAMES) && method.exists()) {
names = method.getParameterNames();
if (isPolymorphic) {
// handled specially below
} else if (types == null) {
nParams = names.length;
} else {
// types != null
if (nParams != names.length) {
if (resolvedSig != null && types.length > names.length) {
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99137
nParams = names.length;
String[] typesWithoutSyntheticParams = new String[nParams];
System.arraycopy(types, types.length - nParams, typesWithoutSyntheticParams, 0, nParams);
types = typesWithoutSyntheticParams;
} else {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=101029
// JavaPlugin.logErrorMessage("JavaElementLabels: Number of param types(" + nParams + ") != number of names(" + names.length + "): " + method.getElementName()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
// no names rendered
names = null;
}
}
}
}
ILocalVariable[] annotatedParameters = null;
if (nParams > 0 && getFlag(flags, JavaElementLabels.M_PARAMETER_ANNOTATIONS)) {
annotatedParameters = method.getParameters();
}
for (int i = 0; i < nParams; i++) {
if (i > 0) {
fBuffer.append(JavaElementLabels.COMMA_STRING);
}
if (annotatedParameters != null && i < annotatedParameters.length) {
appendAnnotationLabels(annotatedParameters[i].getAnnotations(), flags);
}
if (types != null) {
String paramSig = types[i];
if (renderVarargs && (i == nParams - 1)) {
int newDim = Signature.getArrayCount(paramSig) - 1;
appendTypeSignatureLabel(method, Signature.getElementType(paramSig), flags);
for (int k = 0; k < newDim; k++) {
fBuffer.append('[').append(']');
}
fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
} else {
appendTypeSignatureLabel(method, paramSig, flags);
}
}
if (names != null) {
if (types != null) {
fBuffer.append(' ');
}
if (isPolymorphic) {
fBuffer.append(names[0] + i);
} else {
fBuffer.append(names[i]);
}
}
}
} else {
if (declaredParameterTypes.length > 0) {
fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
}
}
fBuffer.append(')');
if (getFlag(flags, JavaElementLabels.M_EXCEPTIONS)) {
String[] types;
if (resolvedKey != null) {
types = resolvedKey.getThrownExceptions();
} else {
types = method.exists() ? method.getExceptionTypes() : new String[0];
}
if (types.length > 0) {
//$NON-NLS-1$
fBuffer.append(" throws ");
for (int i = 0; i < types.length; i++) {
if (i > 0) {
fBuffer.append(JavaElementLabels.COMMA_STRING);
}
appendTypeSignatureLabel(method, types[i], flags);
}
}
}
if (getFlag(flags, JavaElementLabels.M_APP_TYPE_PARAMETERS)) {
int offset = fBuffer.length();
if (resolvedKey != null) {
if (resolvedKey.isParameterizedMethod()) {
String[] typeArgRefs = resolvedKey.getTypeArguments();
if (typeArgRefs.length > 0) {
fBuffer.append(' ');
appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
}
} else {
String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
if (typeParameterSigs.length > 0) {
fBuffer.append(' ');
appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
}
}
} else if (method.exists()) {
ITypeParameter[] typeParameters = method.getTypeParameters();
if (typeParameters.length > 0) {
fBuffer.append(' ');
appendTypeParametersLabels(typeParameters, flags);
}
}
// if (getFlag(flags, JavaElementLabels.COLORIZE) && offset != fBuffer.length()) {
// fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
// }
}
if (getFlag(flags, JavaElementLabels.M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
int offset = fBuffer.length();
fBuffer.append(JavaElementLabels.DECL_STRING);
String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
appendTypeSignatureLabel(method, returnTypeSig, flags);
// if (getFlag(flags, JavaElementLabels.COLORIZE)) {
// fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
// }
}
// category
if (getFlag(flags, JavaElementLabels.M_CATEGORY) && method.exists())
appendCategoryLabel(method, flags);
// post qualification
if (getFlag(flags, JavaElementLabels.M_POST_QUALIFIED)) {
int offset = fBuffer.length();
fBuffer.append(JavaElementLabels.CONCAT_STRING);
appendTypeLabel(method.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
if (getFlag(flags, JavaElementLabels.COLORIZE)) {
fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
}
}
} catch (JavaModelException e) {
// NotExistsException will not reach this point
JavaPlugin.log(e);
}
}
use of org.eclipse.jdt.core.ILocalVariable in project che by eclipse.
the class ParametersHints method getMethodParametersAsString.
private String getMethodParametersAsString(IMethod method) throws JavaModelException {
ILocalVariable[] parameters = method.getParameters();
int paramsLength = parameters.length;
int index = 0;
StringBuffer buffer = new StringBuffer();
for (ILocalVariable parameter : parameters) {
JavaElementLabels.getLocalVariableLabel(parameter, JavaElementLabels.F_PRE_TYPE_SIGNATURE, buffer);
index++;
if (index < paramsLength) {
buffer.append(", ");
}
}
return buffer.toString();
}
use of org.eclipse.jdt.core.ILocalVariable in project che by eclipse.
the class SourcesFromBytecodeGenerator method generateType.
private void generateType(IType type, StringBuilder builder, String indent) throws JavaModelException {
int flags = 0;
appendAnnotationLabels(type.getAnnotations(), flags, builder, indent.substring(TAB.length()));
builder.append(indent.substring(TAB.length()));
builder.append(getModifiers(type.getFlags(), type.getFlags())).append(' ').append(getJavaType(type)).append(' ').append(type.getElementName());
if (type.isResolved()) {
BindingKey key = new BindingKey(type.getKey());
if (key.isParameterizedType()) {
String[] typeArguments = key.getTypeArguments();
appendTypeArgumentSignaturesLabel(type, typeArguments, flags, builder);
} else {
String[] typeParameters = Signature.getTypeParameters(key.toSignature());
appendTypeParameterSignaturesLabel(typeParameters, builder);
}
} else {
appendTypeParametersLabels(type.getTypeParameters(), flags, builder);
}
if (!"java.lang.Object".equals(type.getSuperclassName()) && !"java.lang.Enum".equals(type.getSuperclassName())) {
builder.append(" extends ");
if (type.getSuperclassTypeSignature() != null) {
// appendTypeSignatureLabel(type, type.getSuperclassTypeSignature(), flags, builder);
builder.append(Signature.toString(type.getSuperclassTypeSignature()));
} else {
builder.append(type.getSuperclassName());
}
}
if (!type.isAnnotation()) {
if (type.getSuperInterfaceNames().length != 0) {
builder.append(" implements ");
String[] signatures = type.getSuperInterfaceTypeSignatures();
if (signatures.length == 0) {
signatures = type.getSuperInterfaceNames();
}
for (String interfaceFqn : signatures) {
builder.append(Signature.toString(interfaceFqn)).append(", ");
}
builder.delete(builder.length() - 2, builder.length());
}
}
builder.append(" {\n");
List<IField> fields = new ArrayList<>();
if (type.isEnum()) {
builder.append(indent);
for (IField field : type.getFields()) {
if (field.isEnumConstant()) {
builder.append(field.getElementName()).append(", ");
} else {
fields.add(field);
}
}
if (", ".equals(builder.substring(builder.length() - 2))) {
builder.delete(builder.length() - 2, builder.length());
}
builder.append(";\n");
} else {
fields.addAll(Arrays.asList(type.getFields()));
}
for (IField field : fields) {
if (Flags.isSynthetic(field.getFlags())) {
continue;
}
appendAnnotationLabels(field.getAnnotations(), flags, builder, indent);
builder.append(indent).append(getModifiers(field.getFlags(), type.getFlags()));
if (builder.charAt(builder.length() - 1) != ' ') {
builder.append(' ');
}
builder.append(Signature.toCharArray(field.getTypeSignature().toCharArray())).append(' ').append(field.getElementName());
if (field.getConstant() != null) {
builder.append(" = ");
if (field.getConstant() instanceof String) {
builder.append('"').append(field.getConstant()).append('"');
} else {
builder.append(field.getConstant());
}
}
builder.append(";\n");
}
builder.append('\n');
for (IMethod method : type.getMethods()) {
if (method.getElementName().equals("<clinit>") || Flags.isSynthetic(method.getFlags())) {
continue;
}
appendAnnotationLabels(method.getAnnotations(), flags, builder, indent);
BindingKey resolvedKey = method.isResolved() ? new BindingKey(method.getKey()) : null;
String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;
builder.append(indent).append(getModifiers(method.getFlags(), type.getFlags()));
if (builder.charAt(builder.length() - 1) != ' ') {
builder.append(' ');
}
if (resolvedKey != null) {
if (resolvedKey.isParameterizedMethod()) {
String[] typeArgRefs = resolvedKey.getTypeArguments();
if (typeArgRefs.length > 0) {
appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags, builder);
builder.append(' ');
}
} else {
String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
if (typeParameterSigs.length > 0) {
appendTypeParameterSignaturesLabel(typeParameterSigs, builder);
builder.append(' ');
}
}
} else if (method.exists()) {
ITypeParameter[] typeParameters = method.getTypeParameters();
if (typeParameters.length > 0) {
appendTypeParametersLabels(typeParameters, flags, builder);
builder.append(' ');
}
}
if (!method.isConstructor()) {
String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
appendTypeSignatureLabel(method, returnTypeSig, 0, builder);
builder.append(' ');
// builder.append(Signature.toCharArray(method.getReturnType().toCharArray())).append(' ');
}
builder.append(method.getElementName());
builder.append('(');
for (ILocalVariable variable : method.getParameters()) {
builder.append(Signature.toString(variable.getTypeSignature()));
builder.append(' ').append(variable.getElementName()).append(", ");
}
if (builder.charAt(builder.length() - 1) == ' ') {
builder.delete(builder.length() - 2, builder.length());
}
builder.append(')');
String[] exceptionTypes = method.getExceptionTypes();
if (exceptionTypes != null && exceptionTypes.length != 0) {
builder.append(' ').append("throws ");
for (String exceptionType : exceptionTypes) {
builder.append(Signature.toCharArray(exceptionType.toCharArray())).append(", ");
}
builder.delete(builder.length() - 2, builder.length());
}
if (type.isInterface() || type.isAnnotation()) {
builder.append(";\n\n");
} else {
builder.append(" {").append(METHOD_BODY).append("}\n\n");
}
}
for (IType iType : type.getTypes()) {
generateType(iType, builder, indent + indent);
}
builder.append(indent.substring(TAB.length()));
builder.append("}\n");
}
use of org.eclipse.jdt.core.ILocalVariable in project che by eclipse.
the class SourceTypeConverter method convert.
/*
* Convert a method source element into a parsed method/constructor declaration
*/
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
AbstractMethodDeclaration method;
/* only source positions available */
int start = methodInfo.getNameSourceStart();
int end = methodInfo.getNameSourceEnd();
/* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
on behalf of a 1.4 project we must internalize type variables properly in order to be able to
recognize usages of them in the method signature, to apply substitutions and thus to be able to
detect overriding in the presence of generics. If we simply drop them, when the method signature
refers to the type parameter, we won't know it should be bound to the type parameter and perform
incorrect lookup and may mistakenly end up with missing types
*/
TypeParameter[] typeParams = null;
char[][] typeParameterNames = methodInfo.getTypeParameterNames();
if (typeParameterNames != null) {
int parameterCount = typeParameterNames.length;
if (parameterCount > 0) {
// method's type parameters must be null if no type parameter
char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
typeParams = new TypeParameter[parameterCount];
for (int i = 0; i < parameterCount; i++) {
typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
}
}
}
int modifiers = methodInfo.getModifiers();
if (methodInfo.isConstructor()) {
ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
decl.bits &= ~ASTNode.IsDefaultConstructor;
method = decl;
decl.typeParameters = typeParams;
} else {
MethodDeclaration decl;
if (methodInfo.isAnnotationMethod()) {
AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);
/* conversion of default value */
SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
if ((this.flags & FIELD_INITIALIZATION) != 0) {
if (hasDefaultValue) {
char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
if (defaultValueSource != null) {
Expression expression = parseMemberValue(defaultValueSource);
if (expression != null) {
annotationMethodDeclaration.defaultValue = expression;
}
} else {
// could not retrieve the default value
hasDefaultValue = false;
}
}
}
if (hasDefaultValue)
modifiers |= ClassFileConstants.AccAnnotationDefault;
decl = annotationMethodDeclaration;
} else {
decl = new MethodDeclaration(compilationResult);
}
// convert return type
decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);
// type parameters
decl.typeParameters = typeParams;
method = decl;
}
method.selector = methodHandle.getElementName().toCharArray();
boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
method.sourceStart = start;
method.sourceEnd = end;
method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();
// convert 1.5 specific constructs only if compliance is 1.5 or above
if (this.has1_5Compliance) {
/* convert annotations */
method.annotations = convertAnnotations(methodHandle);
}
/* convert arguments */
String[] argumentTypeSignatures = methodHandle.getParameterTypes();
char[][] argumentNames = methodInfo.getArgumentNames();
int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
if (argumentCount > 0) {
ILocalVariable[] parameters = methodHandle.getParameters();
long position = ((long) start << 32) + end;
method.arguments = new Argument[argumentCount];
for (int i = 0; i < argumentCount; i++) {
TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
if (isVarargs && i == argumentCount - 1) {
typeReference.bits |= ASTNode.IsVarArgs;
}
method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault);
// convert 1.5 specific constructs only if compliance is 1.5 or above
if (this.has1_5Compliance) {
/* convert annotations */
method.arguments[i].annotations = convertAnnotations(parameters[i]);
}
}
}
/* convert thrown exceptions */
char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
if (exceptionCount > 0) {
method.thrownExceptions = new TypeReference[exceptionCount];
for (int i = 0; i < exceptionCount; i++) {
method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
}
}
/* convert local and anonymous types */
if ((this.flags & LOCAL_TYPE) != 0) {
IJavaElement[] children = methodInfo.getChildren();
int typesLength = children.length;
if (typesLength != 0) {
Statement[] statements = new Statement[typesLength];
for (int i = 0; i < typesLength; i++) {
SourceType type = (SourceType) children[i];
TypeDeclaration localType = convert(type, compilationResult);
if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
expression.type = localType.superclass;
localType.superclass = null;
localType.superInterfaces = null;
localType.allocation = expression;
statements[i] = expression;
} else {
statements[i] = localType;
}
}
method.statements = statements;
}
}
return method;
}
Aggregations