diff options
| author | Manju Mathew | 2013-02-11 17:09:24 +0000 |
|---|---|---|
| committer | Dani Megert | 2013-02-11 17:10:44 +0000 |
| commit | 20c463d7a212c4c913f3549bdc61fa97cd43f1d0 (patch) | |
| tree | 4e4d33648b94e90f23e4b3e430a84dd8bfa03209 | |
| parent | af70de53efe25fa20d00bb1fd9c1b16ee2cf3a84 (diff) | |
| download | eclipse.jdt.ui-20c463d7a212c4c913f3549bdc61fa97cd43f1d0.tar.gz eclipse.jdt.ui-20c463d7a212c4c913f3549bdc61fa97cd43f1d0.tar.xz eclipse.jdt.ui-20c463d7a212c4c913f3549bdc61fa97cd43f1d0.zip | |
Fixed bug 395835: Consolidate the code which finds the Javadoc for an
element.
4 files changed, 146 insertions, 252 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.java index d77313eefb..141b2d5e8c 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -54,7 +54,6 @@ final class InfoViewMessages extends NLS { public static String JavadocView_error_noBrowser_message; public static String JavadocView_error_noBrowser_doNotWarn; public static String JavadocView_error_gettingJavadoc; - public static String JavadocView_constantValue_hexValue; static { NLS.initializeMessages(BUNDLE_NAME, InfoViewMessages.class); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.properties index 11f492da4e..7e6ee034c1 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.properties +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/InfoViewMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2012 IBM Corporation and others. +# Copyright (c) 2000, 2013 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -40,5 +40,3 @@ JavadocView_error_noBrowser_title= Javadoc view JavadocView_error_noBrowser_message= The HTML Browser widget could not be created. The standard text widget will be used instead. JavadocView_error_noBrowser_doNotWarn= Do not show this message again. JavadocView_error_gettingJavadoc= <em>Note: An exception occurred while getting the Javadoc. See log for details.</em> -# The first parameter is the constant value string and the second is its hex representation -JavadocView_constantValue_hexValue= {0} [{1}] diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/JavadocView.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/JavadocView.java index 483e67a91a..97c3053165 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/JavadocView.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/infoviews/JavadocView.java @@ -13,15 +13,10 @@ *******************************************************************************/ package org.eclipse.jdt.internal.ui.infoviews; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.net.URL; -import org.osgi.framework.Bundle; - import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; import org.eclipse.swt.browser.Browser; @@ -33,7 +28,6 @@ import org.eclipse.swt.events.ControlEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Composite; @@ -46,7 +40,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; -import org.eclipse.core.runtime.Platform; import org.eclipse.core.resources.IFile; @@ -129,14 +122,11 @@ import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.IBinding; import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.NodeFinder; -import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; -import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations; import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; -import org.eclipse.jdt.internal.corext.util.JdtFlags; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.IContextMenuConstants; @@ -714,39 +704,9 @@ public class JavadocView extends AbstractInfoView { if (fgStyleSheetLoaded) return; fgStyleSheetLoaded= true; - fgStyleSheet= loadStyleSheet(); + fgStyleSheet= JavadocHover.loadStyleSheet("/JavadocViewStyleSheet.css"); //$NON-NLS-1$ } - private static String loadStyleSheet() { - Bundle bundle= Platform.getBundle(JavaPlugin.getPluginId()); - URL styleSheetURL= bundle.getEntry("/JavadocViewStyleSheet.css"); //$NON-NLS-1$ - if (styleSheetURL == null) - return null; - - BufferedReader reader= null; - try { - reader= new BufferedReader(new InputStreamReader(styleSheetURL.openStream())); - StringBuffer buffer= new StringBuffer(1500); - String line= reader.readLine(); - while (line != null) { - buffer.append(line); - buffer.append('\n'); - line= reader.readLine(); - } - - FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0]; - return HTMLPrinter.convertTopLevelFont(buffer.toString(), fontData); - } catch (IOException ex) { - JavaPlugin.log(ex); - return null; - } finally { - try { - if (reader != null) - reader.close(); - } catch (IOException e) { - } - } - } /* * @see AbstractInfoView#createActions() @@ -1212,6 +1172,7 @@ public class JavadocView extends AbstractInfoView { return buffer.toString(); } + /** * Gets the label for the given member. * @@ -1225,18 +1186,8 @@ public class JavadocView extends AbstractInfoView { if (member.getElementType() == IJavaElement.FIELD && constantValue != null) { label.append(constantValue); } - - String imageName= null; - if (allowImage) { - URL imageUrl= JavaPlugin.getDefault().getImagesOnFSRegistry().getImageURL(member); - if (imageUrl != null) { - imageName= imageUrl.toExternalForm(); - } - } - - StringBuffer buf= new StringBuffer(); - JavadocHover.addImageAndLabel(buf, member, imageName, 16, 16, label.toString(), 20, 2); - return buf.toString(); + String imageName= allowImage ? JavadocHover.getImageName(member) : null; + return JavadocHover.getImageAndLabel(member, imageName, label.toString()).toString(); } @@ -1382,7 +1333,7 @@ public class JavadocView extends AbstractInfoView { */ private String computeFieldConstant(IWorkbenchPart activePart, ISelection selection, IField resolvedField, IProgressMonitor monitor) { - if (!isStaticFinal(resolvedField)) + if (!JavadocHover.isStaticFinal(resolvedField)) return null; Object constantValue; @@ -1401,7 +1352,7 @@ public class JavadocView extends AbstractInfoView { } if (constantValue != null) - return getFormattedAssignmentOperator(preferenceProject) + formatCompilerConstantValue(constantValue); + return JavadocHover.getFormattedAssignmentOperator(preferenceProject) + formatCompilerConstantValue(constantValue); return null; } @@ -1457,67 +1408,30 @@ public class JavadocView extends AbstractInfoView { } /** - * Tells whether the given member is static final. - * <p> - * XXX: Copied from {@link JavadocHover}. - * </p> - * @param member the member to test - * @return <code>true</code> if static final - * @since 3.4 - */ - private static boolean isStaticFinal(IJavaElement member) { - if (member.getElementType() != IJavaElement.FIELD) - return false; - - IField field= (IField)member; - try { - return JdtFlags.isFinal(field) && JdtFlags.isStatic(field); - } catch (JavaModelException e) { - JavaPlugin.log(e); - return false; - } - } - - /** - * Returns the constant value for a field that is referenced by the currently active type. - * This method does may not run in the main UI thread. - * <p> - * XXX: This method was part of the JavadocHover#getConstantValue(IField field, IRegion hoverRegion) - * method (lines 299-314). - * </p> + * Returns the constant value for a field that is referenced by the currently active type. This + * method does may not run in the main UI thread. + * * @param activeType the type that is currently active - * @param field the field that is being referenced (usually not declared in <code>activeType</code>) + * @param field the field that is being referenced (usually not declared in + * <code>activeType</code>) * @param selection the region in <code>activeType</code> that contains the field reference * @param monitor a progress monitor - * + * * @return the constant value for the given field or <code>null</code> if none * @since 3.4 */ private static Object getConstantValueFromActiveEditor(ITypeRoot activeType, IField field, ITextSelection selection, IProgressMonitor monitor) { - Object constantValue= null; - CompilationUnit unit= SharedASTProvider.getAST(activeType, SharedASTProvider.WAIT_ACTIVE_ONLY, monitor); if (unit == null) return null; ASTNode node= NodeFinder.perform(unit, selection.getOffset(), selection.getLength()); - if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) { - IBinding binding= ((SimpleName)node).resolveBinding(); - if (binding != null && binding.getKind() == IBinding.VARIABLE) { - IVariableBinding variableBinding= (IVariableBinding)binding; - if (field.equals(variableBinding.getJavaElement())) { - constantValue= variableBinding.getConstantValue(); - } - } - } - return constantValue; + return JavadocHover.getVariableBindingConstValue(node, field); } /** * Returns the string representation of the given constant value. - * <p> - * XXX: In {@link JavadocHover} this method was part of JavadocHover#getConstantValue lines 318-361. - * </p> + * * @param constantValue the constant value * @return the string representation of the given constant value. * @since 3.4 @@ -1536,73 +1450,11 @@ public class JavadocView extends AbstractInfoView { result.append('"'); return result.toString(); - } else if (constantValue instanceof Character) { - String constantResult= '\'' + constantValue.toString() + '\''; - - char charValue= ((Character) constantValue).charValue(); - String hexString= Integer.toHexString(charValue); - StringBuffer hexResult= new StringBuffer("\\u"); //$NON-NLS-1$ - for (int i= hexString.length(); i < 4; i++) { - hexResult.append('0'); - } - hexResult.append(hexString); - return formatWithHexValue(constantResult, hexResult.toString()); - - } else if (constantValue instanceof Byte) { - int byteValue= ((Byte) constantValue).intValue() & 0xFF; - return formatWithHexValue(constantValue, "0x" + Integer.toHexString(byteValue)); //$NON-NLS-1$ - - } else if (constantValue instanceof Short) { - int shortValue= ((Short) constantValue).shortValue() & 0xFFFF; - return formatWithHexValue(constantValue, "0x" + Integer.toHexString(shortValue)); //$NON-NLS-1$ - - } else if (constantValue instanceof Integer) { - int intValue= ((Integer) constantValue).intValue(); - return formatWithHexValue(constantValue, "0x" + Integer.toHexString(intValue)); //$NON-NLS-1$ - - } else if (constantValue instanceof Long) { - long longValue= ((Long) constantValue).longValue(); - return formatWithHexValue(constantValue, "0x" + Long.toHexString(longValue)); //$NON-NLS-1$ - } else { - return constantValue.toString(); + return JavadocHover.getHexConstantValue(constantValue); } } - /** - * Creates and returns a formatted message for the given - * constant with its hex value. - * <p> - * XXX: Copied from {@link JavadocHover}. - * </p> - * - * @param constantValue the constant value - * @param hexValue the hex value - * @return a formatted string with constant and hex values - * @since 3.4 - */ - private static String formatWithHexValue(Object constantValue, String hexValue) { - return Messages.format(InfoViewMessages.JavadocView_constantValue_hexValue, new String[] { constantValue.toString(), hexValue }); - } - - /** - * Returns the assignment operator string with the project's formatting applied to it. - * <p> - * XXX: This method was extracted from JavadocHover#getInfoText method. - * </p> - * @param javaProject the Java project whose formatting options will be used. - * @return the formatted assignment operator string. - * @since 3.4 - */ - private static String getFormattedAssignmentOperator(IJavaProject javaProject) { - StringBuffer buffer= new StringBuffer(); - if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, true))) - buffer.append(' '); - buffer.append('='); - if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR, true))) - buffer.append(' '); - return buffer.toString(); - } /** * see also org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.addLinkListener(BrowserInformationControl) diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/JavadocHover.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/JavadocHover.java index 283b4d2ee1..c9f0835313 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/JavadocHover.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/JavadocHover.java @@ -747,31 +747,29 @@ public class JavadocHover extends AbstractJavaEditorTextHover { if (constantValue != null) { constantValue= HTMLPrinter.convertToHTMLContentWithWhitespace(constantValue); IJavaProject javaProject= element.getJavaProject(); - if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, true))) - label.append(' '); - label.append('='); - if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR, true))) - label.append(' '); + label.append(getFormattedAssignmentOperator(javaProject)); label.append(constantValue); } } - + // if (element.getElementType() == IJavaElement.METHOD) { // IMethod method= (IMethod)element; // //TODO: add default value for annotation type members, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=249016 // } + String imageName= allowImage ? getImageName(element) : null; + return getImageAndLabel(element, imageName, label.toString()).toString(); + } + + + public static String getImageName(IJavaElement element) { String imageName= null; - if (allowImage) { - URL imageUrl= JavaPlugin.getDefault().getImagesOnFSRegistry().getImageURL(element); - if (imageUrl != null) { - imageName= imageUrl.toExternalForm(); - } + URL imageUrl= JavaPlugin.getDefault().getImagesOnFSRegistry().getImageURL(element); + if (imageUrl != null) { + imageName= imageUrl.toExternalForm(); } - StringBuffer buf= new StringBuffer(); - addImageAndLabel(buf, element, imageName, 16, 16, label.toString(), 20, 2); - return buf.toString(); + return imageName; } private static long getHeaderFlags(IJavaElement element) { @@ -787,10 +785,14 @@ public class JavadocHover extends AbstractJavaEditorTextHover { } } - /* + /** + * Tells whether the given field is static final. + * + * @param field the member to test + * @return <code>true</code> if static final * @since 3.4 */ - private static boolean isStaticFinal(IField field) { + public static boolean isStaticFinal(IField field) { try { return JdtFlags.isFinal(field) && JdtFlags.isStatic(field); } catch (JavaModelException e) { @@ -815,53 +817,15 @@ public class JavadocHover extends AbstractJavaEditorTextHover { ASTNode node= getHoveredASTNode(editorInputElement, hoverRegion); if (node == null) return null; - - Object constantValue= null; - if (node.getNodeType() == ASTNode.SIMPLE_NAME) { - IBinding binding= ((SimpleName)node).resolveBinding(); - if (binding != null && binding.getKind() == IBinding.VARIABLE) { - IVariableBinding variableBinding= (IVariableBinding)binding; - if (field.equals(variableBinding.getJavaElement())) { - constantValue= variableBinding.getConstantValue(); - } - } - } + + Object constantValue= getVariableBindingConstValue(node, field); if (constantValue == null) return null; if (constantValue instanceof String) { return ASTNodes.getEscapedStringLiteral((String) constantValue); - - } else if (constantValue instanceof Character) { - String constantResult= ASTNodes.getEscapedCharacterLiteral(((Character) constantValue).charValue()); - - char charValue= ((Character) constantValue).charValue(); - String hexString= Integer.toHexString(charValue); - StringBuffer hexResult= new StringBuffer("\\u"); //$NON-NLS-1$ - for (int i= hexString.length(); i < 4; i++) { - hexResult.append('0'); - } - hexResult.append(hexString); - return formatWithHexValue(constantResult, hexResult.toString()); - - } else if (constantValue instanceof Byte) { - int byteValue= ((Byte) constantValue).intValue() & 0xFF; - return formatWithHexValue(constantValue, "0x" + Integer.toHexString(byteValue)); //$NON-NLS-1$ - - } else if (constantValue instanceof Short) { - int shortValue= ((Short) constantValue).shortValue() & 0xFFFF; - return formatWithHexValue(constantValue, "0x" + Integer.toHexString(shortValue)); //$NON-NLS-1$ - - } else if (constantValue instanceof Integer) { - int intValue= ((Integer) constantValue).intValue(); - return formatWithHexValue(constantValue, "0x" + Integer.toHexString(intValue)); //$NON-NLS-1$ - - } else if (constantValue instanceof Long) { - long longValue= ((Long) constantValue).longValue(); - return formatWithHexValue(constantValue, "0x" + Long.toHexString(longValue)); //$NON-NLS-1$ - } else { - return constantValue.toString(); + return getHexConstantValue(constantValue); } } @@ -895,8 +859,9 @@ public class JavadocHover extends AbstractJavaEditorTextHover { * @since 3.4 */ private static String getStyleSheet() { - if (fgStyleSheet == null) - fgStyleSheet= loadStyleSheet(); + if (fgStyleSheet == null) { + fgStyleSheet= loadStyleSheet("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$ + } String css= fgStyleSheet; if (css != null) { FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0]; @@ -907,40 +872,51 @@ public class JavadocHover extends AbstractJavaEditorTextHover { } /** - * Loads and returns the Javadoc hover style sheet. + * Loads and returns the style sheet associated with either Javadoc hover or the view. + * + * @param styleSheetName the style sheet name of either the Javadoc hover or the view * @return the style sheet, or <code>null</code> if unable to load * @since 3.4 */ - private static String loadStyleSheet() { + public static String loadStyleSheet(String styleSheetName) { Bundle bundle= Platform.getBundle(JavaPlugin.getPluginId()); - URL styleSheetURL= bundle.getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$ - if (styleSheetURL != null) { - BufferedReader reader= null; + URL styleSheetURL= bundle.getEntry(styleSheetName); + if (styleSheetURL == null) + return null; + + BufferedReader reader= null; + try { + reader= new BufferedReader(new InputStreamReader(styleSheetURL.openStream())); + StringBuffer buffer= new StringBuffer(1500); + String line= reader.readLine(); + while (line != null) { + buffer.append(line); + buffer.append('\n'); + line= reader.readLine(); + } + + FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0]; + return HTMLPrinter.convertTopLevelFont(buffer.toString(), fontData); + } catch (IOException ex) { + JavaPlugin.log(ex); + return ""; //$NON-NLS-1$ + } finally { try { - reader= new BufferedReader(new InputStreamReader(styleSheetURL.openStream())); - StringBuffer buffer= new StringBuffer(1500); - String line= reader.readLine(); - while (line != null) { - buffer.append(line); - buffer.append('\n'); - line= reader.readLine(); - } - return buffer.toString(); - } catch (IOException ex) { - JavaPlugin.log(ex); - return ""; //$NON-NLS-1$ - } finally { - try { - if (reader != null) - reader.close(); - } catch (IOException e) { - } + if (reader != null) + reader.close(); + } catch (IOException e) { + //ignore } } - return null; } - public static void addImageAndLabel(StringBuffer buf, IJavaElement element, String imageSrcPath, int imageWidth, int imageHeight, String label, int labelLeft, int labelTop) { + public static String getImageAndLabel(IJavaElement element, String imageSrcPath, String label) { + StringBuffer buf= new StringBuffer(); + int imageWidth= 16; + int imageHeight= 16; + int labelLeft= 20; + int labelTop= 2; + buf.append("<div style='word-wrap: break-word; position: relative; "); //$NON-NLS-1$ if (imageSrcPath != null) { @@ -984,6 +960,7 @@ public class JavadocHover extends AbstractJavaEditorTextHover { buf.append(label); buf.append("</div>"); //$NON-NLS-1$ + return buf.toString(); } public static void addAnnotations(StringBuffer buf, IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) { @@ -1175,4 +1152,72 @@ public class JavadocHover extends AbstractJavaEditorTextHover { private static StringBuffer addLink(StringBuffer buf, String uri, String label) { return buf.append(JavaElementLinks.createLink(uri, label)); } + + /** + * Returns the assignment operator string with the project's formatting applied to it. + * + * @param javaProject the Java project whose formatting options will be used. + * @return the formatted assignment operator string. + * @since 3.4 + */ + public static String getFormattedAssignmentOperator(IJavaProject javaProject) { + StringBuffer buffer= new StringBuffer(); + if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, true))) + buffer.append(' '); + buffer.append('='); + if (JavaCore.INSERT.equals(javaProject.getOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR, true))) + buffer.append(' '); + return buffer.toString(); + } + + + + public static String getHexConstantValue(Object constantValue) { + if (constantValue instanceof Character) { + String constantResult= '\'' + constantValue.toString() + '\''; + + char charValue= ((Character) constantValue).charValue(); + String hexString= Integer.toHexString(charValue); + StringBuffer hexResult= new StringBuffer("\\u"); //$NON-NLS-1$ + for (int i= hexString.length(); i < 4; i++) { + hexResult.append('0'); + } + hexResult.append(hexString); + return formatWithHexValue(constantResult, hexResult.toString()); + + } else if (constantValue instanceof Byte) { + int byteValue= ((Byte) constantValue).intValue() & 0xFF; + return formatWithHexValue(constantValue, "0x" + Integer.toHexString(byteValue)); //$NON-NLS-1$ + + } else if (constantValue instanceof Short) { + int shortValue= ((Short) constantValue).shortValue() & 0xFFFF; + return formatWithHexValue(constantValue, "0x" + Integer.toHexString(shortValue)); //$NON-NLS-1$ + + } else if (constantValue instanceof Integer) { + int intValue= ((Integer) constantValue).intValue(); + return formatWithHexValue(constantValue, "0x" + Integer.toHexString(intValue)); //$NON-NLS-1$ + + } else if (constantValue instanceof Long) { + long longValue= ((Long) constantValue).longValue(); + return formatWithHexValue(constantValue, "0x" + Long.toHexString(longValue)); //$NON-NLS-1$ + + } else { + return constantValue.toString(); + } + } + + public static Object getVariableBindingConstValue(ASTNode node, IField field) { + if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) { + IBinding binding= ((SimpleName) node).resolveBinding(); + if (binding != null && binding.getKind() == IBinding.VARIABLE) { + IVariableBinding variableBinding= (IVariableBinding) binding; + if (field.equals(variableBinding.getJavaElement())) { + return variableBinding.getConstantValue(); + } + } + } + return null; + } + + } |
