List of usage examples for org.eclipse.jdt.core.dom ITypeBinding getJavaElement
public IJavaElement getJavaElement();
From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java
License:Open Source License
private IDSModel processComponent(TypeDeclaration type, ITypeBinding typeBinding, Annotation annotation, IAnnotationBinding annotationBinding, Collection<DSAnnotationProblem> problems) { HashMap<String, Object> params = new HashMap<String, Object>(); for (IMemberValuePairBinding pair : annotationBinding.getDeclaredMemberValuePairs()) { params.put(pair.getName(), pair.getValue()); }/*from ww w. j a v a2s . c o m*/ boolean requiresV12 = false; String implClass = typeBinding.getBinaryName(); String name = implClass; Object value; if ((value = params.get("name")) instanceof String) { //$NON-NLS-1$ name = (String) value; validateComponentName(annotation, name, problems); } Collection<String> services; if ((value = params.get("service")) instanceof Object[]) { //$NON-NLS-1$ Object[] elements = (Object[]) value; services = new LinkedHashSet<String>(elements.length); Map<String, Integer> serviceDuplicates = errorLevel.isNone() ? null : new HashMap<String, Integer>(); for (int i = 0; i < elements.length; ++i) { ITypeBinding serviceType = (ITypeBinding) elements[i]; String serviceName = serviceType.getBinaryName(); if (!errorLevel.isNone()) { if (serviceDuplicates.containsKey(serviceName)) { reportProblem(annotation, "service", i, problems, //$NON-NLS-1$ Messages.AnnotationProcessor_duplicateServiceDeclaration, serviceName); Integer pos = serviceDuplicates.put(serviceName, null); if (pos != null) reportProblem(annotation, "service", pos.intValue(), problems, //$NON-NLS-1$ Messages.AnnotationProcessor_duplicateServiceDeclaration, serviceName); } else { serviceDuplicates.put(serviceName, i); } } services.add(serviceName); validateComponentService(annotation, typeBinding, serviceType, i, problems); } } else { ITypeBinding[] serviceTypes = typeBinding.getInterfaces(); services = new ArrayList<String>(serviceTypes.length); for (int i = 0; i < serviceTypes.length; ++i) { services.add(serviceTypes[i].getBinaryName()); } } String factory = null; if ((value = params.get("factory")) instanceof String) { //$NON-NLS-1$ factory = (String) value; validateComponentFactory(annotation, factory, problems); } Boolean serviceFactory = null; if ((value = params.get("servicefactory")) instanceof Boolean) { //$NON-NLS-1$ serviceFactory = (Boolean) value; } Boolean enabled = null; if ((value = params.get("enabled")) instanceof Boolean) { //$NON-NLS-1$ enabled = (Boolean) value; } Boolean immediate = null; if ((value = params.get("immediate")) instanceof Boolean) { //$NON-NLS-1$ immediate = (Boolean) value; } String[] properties; if ((value = params.get("property")) instanceof Object[]) { //$NON-NLS-1$ Object[] elements = (Object[]) value; ArrayList<String> list = new ArrayList<String>(elements.length); for (int i = 0; i < elements.length; ++i) { if (elements[i] instanceof String) list.add((String) elements[i]); } properties = list.toArray(new String[list.size()]); } else { properties = new String[0]; } String[] propertyFiles; if ((value = params.get("properties")) instanceof Object[]) { //$NON-NLS-1$ Object[] elements = (Object[]) value; ArrayList<String> list = new ArrayList<String>(elements.length); for (int i = 0; i < elements.length; ++i) { if (elements[i] instanceof String) list.add((String) elements[i]); } propertyFiles = list.toArray(new String[list.size()]); validateComponentPropertyFiles(annotation, ((IType) typeBinding.getJavaElement()).getJavaProject().getProject(), propertyFiles, problems); } else { propertyFiles = new String[0]; } String configPolicy = null; if ((value = params.get("configurationPolicy")) instanceof IVariableBinding) { //$NON-NLS-1$ IVariableBinding configPolicyBinding = (IVariableBinding) value; ConfigurationPolicy configPolicyLiteral = ConfigurationPolicy.valueOf(configPolicyBinding.getName()); if (configPolicyLiteral != null) configPolicy = configPolicyLiteral.toString(); } String configPid = null; if ((value = params.get("configurationPid")) instanceof String) { //$NON-NLS-1$ configPid = (String) value; validateComponentConfigPID(annotation, configPid, problems); requiresV12 = true; } DSModel model = new DSModel(new Document(), false); IDSComponent component = model.getDSComponent(); if (name != null) component.setAttributeName(name); if (factory != null) component.setFactory(factory); if (enabled != null) component.setEnabled(enabled.booleanValue()); if (immediate != null) component.setImmediate(immediate.booleanValue()); if (configPolicy != null) component.setConfigurationPolicy(configPolicy); if (configPid != null) component.setXMLAttribute("configuration-pid", configPid); //$NON-NLS-1$ IDSDocumentFactory dsFactory = component.getModel().getFactory(); IDSImplementation impl = dsFactory.createImplementation(); component.setImplementation(impl); impl.setClassName(implClass); if (!services.isEmpty()) { IDSService service = dsFactory.createService(); component.setService(service); for (String serviceName : services) { IDSProvide provide = dsFactory.createProvide(); service.addProvidedService(provide); provide.setInterface(serviceName); } if (serviceFactory != null) service.setServiceFactory(serviceFactory.booleanValue()); } if (properties.length > 0) { HashMap<String, IDSProperty> map = new HashMap<String, IDSProperty>(properties.length); for (int i = 0; i < properties.length; ++i) { String propertyStr = properties[i]; String[] pair = propertyStr.split("=", 2); //$NON-NLS-1$ int colon = pair[0].indexOf(':'); String propertyName, propertyType; if (colon == -1) { propertyName = pair[0]; propertyType = null; } else { propertyName = pair[0].substring(0, colon); propertyType = pair[0].substring(colon + 1); } String propertyValue = pair.length > 1 ? pair[1].trim() : null; IDSProperty property = map.get(propertyName); if (property == null) { // create a new property property = dsFactory.createProperty(); component.addPropertyElement(property); map.put(propertyName, property); property.setPropertyName(propertyName); property.setPropertyType(propertyType); property.setPropertyValue(propertyValue); validateComponentProperty(annotation, propertyName, propertyType, propertyValue, i, problems); } else { // property exists; make it multi-valued String content = property.getPropertyElemBody(); if (content == null) { content = property.getPropertyValue(); property.setPropertyElemBody(content); property.setPropertyValue(null); } if (!errorLevel.isNone()) { String expected = property.getPropertyType() == null || property.getPropertyType().isEmpty() || String.class.getSimpleName().equals(property.getPropertyType()) ? Messages.AnnotationProcessor_stringOrEmpty : property.getPropertyType(); String actual = propertyType == null || String.class.getSimpleName().equals(propertyType) ? Messages.AnnotationProcessor_stringOrEmpty : propertyType; if (!actual.equals(expected)) reportProblem(annotation, "property", i, problems, //$NON-NLS-1$ NLS.bind(Messages.AnnotationProcessor_inconsistentComponentPropertyType, actual, expected), actual); else validateComponentProperty(annotation, propertyName, propertyType, propertyValue, i, problems); } if (propertyValue != null) property.setPropertyElemBody(content + "\n" + pair[1]); //$NON-NLS-1$ } } } if (propertyFiles.length > 0) { for (String propertyFile : propertyFiles) { IDSProperties propertiesElement = dsFactory.createProperties(); component.addPropertiesElement(propertiesElement); propertiesElement.setEntry(propertyFile); } } String activate = null; Annotation activateAnnotation = null; String deactivate = null; Annotation deactivateAnnotation = null; String modified = null; Annotation modifiedAnnotation = null; ArrayList<IDSReference> references = new ArrayList<IDSReference>(); HashMap<String, Annotation> referenceNames = new HashMap<String, Annotation>(); for (MethodDeclaration method : type.getMethods()) { for (Object modifier : method.modifiers()) { if (!(modifier instanceof Annotation)) continue; Annotation methodAnnotation = (Annotation) modifier; IAnnotationBinding methodAnnotationBinding = methodAnnotation.resolveAnnotationBinding(); if (methodAnnotationBinding == null) { if (debug.isDebugging()) debug.trace( String.format("Unable to resolve binding for annotation: %s", methodAnnotation)); //$NON-NLS-1$ continue; } String annotationName = methodAnnotationBinding.getAnnotationType().getQualifiedName(); if (ACTIVATE_ANNOTATION.equals(annotationName)) { if (activate == null) { activate = method.getName().getIdentifier(); activateAnnotation = methodAnnotation; validateLifeCycleMethod(methodAnnotation, "activate", method, problems); //$NON-NLS-1$ } else if (!errorLevel.isNone()) { reportProblem(methodAnnotation, null, problems, Messages.AnnotationProcessor_duplicateActivateMethod, method.getName().getIdentifier()); if (activateAnnotation != null) { reportProblem(activateAnnotation, null, problems, Messages.AnnotationProcessor_duplicateActivateMethod, activate); activateAnnotation = null; } } continue; } if (DEACTIVATE_ANNOTATION.equals(annotationName)) { if (deactivate == null) { deactivate = method.getName().getIdentifier(); deactivateAnnotation = methodAnnotation; validateLifeCycleMethod(methodAnnotation, "deactivate", method, problems); //$NON-NLS-1$ } else if (!errorLevel.isNone()) { reportProblem(methodAnnotation, null, problems, Messages.AnnotationProcessor_duplicateDeactivateMethod, method.getName().getIdentifier()); if (deactivateAnnotation != null) { reportProblem(deactivateAnnotation, null, problems, Messages.AnnotationProcessor_duplicateDeactivateMethod, deactivate); deactivateAnnotation = null; } } continue; } if (MODIFIED_ANNOTATION.equals(annotationName)) { if (modified == null) { modified = method.getName().getIdentifier(); modifiedAnnotation = methodAnnotation; validateLifeCycleMethod(methodAnnotation, "modified", method, problems); //$NON-NLS-1$ } else if (!errorLevel.isNone()) { reportProblem(methodAnnotation, null, problems, Messages.AnnotationProcessor_duplicateModifiedMethod, method.getName().getIdentifier()); if (modifiedAnnotation != null) { reportProblem(modifiedAnnotation, null, problems, Messages.AnnotationProcessor_duplicateModifiedMethod, modified); modifiedAnnotation = null; } } continue; } if (REFERENCE_ANNOTATION.equals(annotationName)) { IMethodBinding methodBinding = method.resolveBinding(); if (methodBinding == null) { if (debug.isDebugging()) debug.trace(String.format("Unable to resolve binding for method: %s", method)); //$NON-NLS-1$ } else { requiresV12 |= processReference(method, methodBinding, methodAnnotation, methodAnnotationBinding, dsFactory, references, referenceNames, problems); } continue; } } } if (activate != null) component.setActivateMethod(activate); if (deactivate != null) component.setDeactivateMethod(deactivate); if (modified != null) component.setModifiedeMethod(modified); if (!references.isEmpty()) { // references must be declared in ascending lexicographical order of their names Collections.sort(references, REF_NAME_COMPARATOR); for (IDSReference reference : references) { component.addReference(reference); } } String xmlns = null; if ((value = params.get("xmlns")) instanceof String) { //$NON-NLS-1$ xmlns = (String) value; validateComponentXMLNS(annotation, xmlns, requiresV12, problems); } else if (requiresV12) { xmlns = NAMESPACE_1_2; } if (xmlns != null) component.setNamespace(xmlns); return model; }
From source file:ca.mcgill.cs.swevo.jayfx.ASTCrawler.java
License:Open Source License
/** * Converts a type binding to a class element or an enum element. * /*from w w w .ja va 2s.co m*/ * @param pBinding * The binding to convert. Cannot be null. * @return A class or enum element representing this binding. Cannot be * null. */ private static IElement convertBinding(final ITypeBinding pBinding) { ASTCrawler.checkForNull(pBinding); IJavaElement elem = null; try { elem = pBinding.getJavaElement(); } catch (final NullPointerException E) { System.out.println("Bug in eclipse encountered for: " + pBinding.getName()); return null; } return FlyweightElementFactory.getElement(Category.CLASS, pBinding.getBinaryName()); }
From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.JavaMessage.java
License:Open Source License
/** * Returns true if this message was passed within a try block that catches the given * type.//from ww w . j a v a2 s . c o m * @param type * @return */ public boolean catches(IType type) { if (tries == null) { return false; } for (TryStatement statement : tries) { for (Object o : statement.catchClauses()) { CatchClause catcher = (CatchClause) o; ITypeBinding binding = catcher.getException().getType().resolveBinding(); if (binding != null) { IType caughtType = (IType) binding.getJavaElement(); if (caughtType != null) { try { ITypeHierarchy hierarchy = caughtType.newSupertypeHierarchy(new NullProgressMonitor()); if (caughtType.equals(type) || hierarchy.contains(type)) { return true; } } catch (JavaModelException e) { } } } } } return false; }
From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java
License:Open Source License
public boolean visit(ClassInstanceCreation node) { if (!(message instanceof ICall)) return false; if (containsMessage(node)) { ICall call = (ICall) message;//from w ww . jav a 2s. c om IMethodBinding binding = node.resolveConstructorBinding(); if (binding != null) { binding = binding.getMethodDeclaration(); if (binding != null) { IJavaElement element = binding.getJavaElement(); if (element instanceof IMethod) { try { IMethod jm = (IMethod) element; //get the target method. ITraceClassMethod am = call.getTarget().getActivation().getMethod(); if (JavaSearchUtils.getFullyQualifiedName(jm.getDeclaringType(), true) .equals(am.getTraceClass().getName())) { String types[] = Signature.getParameterTypes(am.getSignature()); IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types); if (jm.isSimilar(testMethod)) { this.node = node; try { if (document .getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1)) //look for a better match. return true; } catch (BadLocationException e) { } return false; } } } catch (NullPointerException e) { return true; } } else { //try to match just on the class name ITypeBinding typeBinding = binding.getDeclaringClass(); IJavaElement je = typeBinding.getJavaElement(); if (je instanceof IType) { IType type = (IType) je; try { ITraceClassMethod am = call.getTarget().getActivation().getMethod(); if (JavaSearchUtils.getFullyQualifiedName(type, true) .equals(am.getTraceClass().getName())) { this.node = node; try { if (document .getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1)) //look for a better match. return true; } catch (BadLocationException e) { } return false; } } catch (NullPointerException e) { return true; } } } } } return true; } return false; }
From source file:com.codenvy.ide.ext.java.server.JavadocFinder.java
License:Open Source License
private void addValue(StringBuffer buf, IJavaElement element, Object value) throws URISyntaxException { // Note: To be bug-compatible with Javadoc from Java 5/6/7, we currently don't escape HTML tags in String-valued annotations. if (value instanceof ITypeBinding) { ITypeBinding typeBinding = (ITypeBinding) value; IJavaElement type = typeBinding.getJavaElement(); if (type == null) { buf.append(typeBinding.getName()); } else {/*from ww w . ja v a 2s . c om*/ String uri = JavaElementLinks.createURI(baseHref, type); String name = type.getElementName(); addLink(buf, uri, name); } buf.append(".class"); //$NON-NLS-1$ } else if (value instanceof IVariableBinding) { // only enum constants IVariableBinding variableBinding = (IVariableBinding) value; IJavaElement variable = variableBinding.getJavaElement(); String uri = JavaElementLinks.createURI(baseHref, variable); String name = variable.getElementName(); addLink(buf, uri, name); } else if (value instanceof IAnnotationBinding) { IAnnotationBinding annotationBinding = (IAnnotationBinding) value; addAnnotation(buf, element, annotationBinding); } else if (value instanceof String) { buf.append(ASTNodes.getEscapedStringLiteral((String) value)); } else if (value instanceof Character) { buf.append(ASTNodes.getEscapedCharacterLiteral((Character) value)); } else if (value instanceof Object[]) { Object[] values = (Object[]) value; buf.append('{'); for (int i = 0; i < values.length; i++) { if (i > 0) { buf.append(JavaElementLabels.COMMA_STRING); } addValue(buf, element, values[i]); } buf.append('}'); } else { // primitive types (except char) or null buf.append(String.valueOf(value)); } }
From source file:com.google.gwt.eclipse.core.uibinder.validators.UiBinderJavaValidator.java
License:Open Source License
private static IType getType(TypeDeclaration typeDecl) { if (typeDecl == null) { return null; }/*from ww w . ja va2 s.co m*/ ITypeBinding typeBinding = typeDecl.resolveBinding(); if (typeBinding == null) { return null; } IJavaElement javaElement = typeBinding.getJavaElement(); return (javaElement instanceof IType ? (IType) javaElement : null); }
From source file:com.halware.nakedide.eclipse.ext.outline.OutlineViewContentProvider.java
License:Open Source License
protected Object[] doGetElements(TypeDeclaration typeDeclaration) { Map<String, IMethod> methodsByName = new TreeMap<String, IMethod>(); ITypeBinding typeBinding = typeDeclaration.resolveBinding(); if (typeBinding == null) { return new Object[] {}; }/* w ww . j av a 2 s . c o m*/ IType type = (IType) typeBinding.getJavaElement(); appendMethods(type, methodsByName); Map<String, NakedObjectMember> membersByName = new TreeMap<String, NakedObjectMember>(); for (Object bodyDeclarationObj : typeDeclaration.bodyDeclarations()) { BodyDeclaration bodyDeclaration = (BodyDeclaration) bodyDeclarationObj; if (!(bodyDeclaration instanceof MethodDeclaration)) { continue; } MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration; MemberType memberType = null; String memberName = MethodNameUtils.asPropertyName(methodDeclaration); if (memberName != null) { memberType = MemberType.PROPERTY; } else { memberName = MethodNameUtils.asCollectionName(methodDeclaration); if (memberName != null) { memberType = MemberType.COLLECTION; } else { memberName = MethodNameUtils.asActionName(methodDeclaration); if (memberName != null) { memberType = MemberType.ACTION; } else { memberName = MethodNameUtils.asReservedMethodName(methodDeclaration); if (memberName != null) { memberType = MemberType.RESERVED; } } } } if (memberName == null) { continue; } NakedObjectMember nakedObjectMember = membersByName.get(memberName); if (nakedObjectMember == null) { nakedObjectMember = new NakedObjectMember(getCompilationUnitOwner().getCompilationUnit(), memberName, methodDeclaration, memberType, methodsByName); membersByName.put(nakedObjectMember.getMemberName(), nakedObjectMember); } } return membersByName.values().toArray(); }
From source file:com.motorola.studio.android.generateviewbylayout.MethodBodyVisitor.java
License:Apache License
private void checkInflatedViewNameOnFields(MethodInvocation node) { //check if this method invocation is binded to an assignment ASTNode nodeParent = node.getParent(); while ((nodeParent != null) && (inflatedViewName == null)) { if (nodeParent instanceof Assignment) { Assignment assignment = (Assignment) nodeParent; Expression lhs = assignment.getLeftHandSide(); ITypeBinding binding = lhs.resolveTypeBinding(); IJavaElement javaElement = binding.getJavaElement(); if ((javaElement != null) && (lhs instanceof SimpleName)) { IType type = (IType) javaElement.getAdapter(IType.class); if (type != null) { try { if (JDTUtils.isSubclass(type, "android.view.View")) { inflatedViewName = ((SimpleName) lhs).getFullyQualifiedName(); }//from www.jav a2 s . co m } catch (JavaModelException e) { // do nothing } } } } nodeParent = nodeParent.getParent(); } }
From source file:com.motorola.studio.android.generateviewbylayout.SaveStateVisitor.java
License:Apache License
/** * Restored views are in form/*from ww w. j a v a 2 s . c o m*/ * <variable>.<viewSetMethod>(preferences.<preferenceGetMethod>("property", <defaultValue>)); * @param node */ private void identifyRestoredView(MethodInvocation node) { Expression expression = node.getExpression(); if (expression instanceof SimpleName) { ITypeBinding binding = ((SimpleName) expression).resolveTypeBinding(); IJavaElement javaElement = binding.getJavaElement(); if (javaElement != null) { try { IType type = (IType) javaElement.getAdapter(IType.class); if (JDTUtils.isSubclass(type, "android.view.View")) { viewIds.add(((SimpleName) expression).getFullyQualifiedName()); } } catch (JavaModelException e) { StudioLogger.warn(CodeUtilsActivator.PLUGIN_ID, "Unable to identify if " + binding.getName() + " is a subclass of android.view.View", e); } } } }
From source file:com.motorola.studio.android.generateviewbylayout.SaveStateVisitor.java
License:Apache License
/** * Saved views are in form// w w w . ja v a 2s. co m * editor.<propertySetMethod>("property", <variable>.<viewGetMethod>); * @param node */ private void identifySavedView(MethodInvocation node) { Expression expression = node.getExpression(); if (expression instanceof SimpleName) { ITypeBinding binding = ((SimpleName) expression).resolveTypeBinding(); IJavaElement javaElement = binding.getJavaElement(); if (javaElement != null) { try { IType type = (IType) javaElement.getAdapter(IType.class); if (JDTUtils.isSubclass(type, "android.view.View")) { viewIds.add(((SimpleName) expression).getFullyQualifiedName()); } } catch (JavaModelException e) { StudioLogger.warn(CodeUtilsActivator.PLUGIN_ID, "Unable to identify if " + binding.getName() + " is a subclass of android.view.View", e); } } } }