Example usage for org.eclipse.jdt.core IType getAnnotation

List of usage examples for org.eclipse.jdt.core IType getAnnotation

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getAnnotation.

Prototype

IAnnotation getAnnotation(String name);

Source Link

Document

Returns the annotation with the given name declared on this element.

Usage

From source file:cn.ieclipse.adt.ext.jdt.JavaSelection.java

License:Apache License

public List<TypeMapping> getTypeMappings() {
    ArrayList<TypeMapping> list = new ArrayList<TypeMapping>();
    ICompilationUnit[] arrays = units.toArray(new ICompilationUnit[] {});
    for (ICompilationUnit unit : arrays) {
        IType[] types;//  w  w  w. j  av a 2 s. co m
        try {
            types = unit.getAllTypes();
        } catch (JavaModelException e) {
            // TODO
            e.printStackTrace();
            continue;
        }
        for (IType type : types) {
            IAnnotation nt = type.getAnnotation("Table");
            if (nt != null && nt.exists()) {
                IMemberValuePair[] tVars;
                String table = "";
                try {
                    tVars = nt.getMemberValuePairs();
                    for (IMemberValuePair tVar : tVars) {
                        if ("name".equals(tVar.getMemberName())) {
                            table = (String) tVar.getValue();
                        }
                    }
                    list.add(new TypeMapping(table, type));
                } catch (JavaModelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }
    return list;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JaxWsUtils.java

License:Open Source License

/**
 * Searches all the Java types from a Java project that are annotated with @WebService.
 *
 * @param jp the containing Java project
 * @param monitor the progress monitor//from   w  ww. ja  v  a2  s .c  om
 * @param includeClasses true to include classes in the search results
 * @param includeInterfaces true to include the interfaces in the search results
 * @return a Map
 * <p>
 * Key = the qualified name of the annotated type<br />
 * Value = the associated service name (in the target WSDL)
 * </p>
 *
 * @throws CoreException if the search could not be launched
 */
public static Map<String, String> getJaxAnnotatedJavaTypes(IJavaProject jp, IProgressMonitor monitor,
        final boolean includeClasses, final boolean includeInterfaces) throws CoreException {

    jp.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
    jp.getProject().build(IncrementalProjectBuilder.FULL_BUILD, monitor);

    final Map<String, String> classNameToServiceName = new HashMap<String, String>();
    SearchPattern pattern = SearchPattern.createPattern(WEB_WS_ANNOTATION, IJavaSearchConstants.ANNOTATION_TYPE,
            IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

    // This is what we do when we find a match
    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {

            // We get the Java type that is annotated with @WebService
            if (match.getElement() instanceof IType) {

                // Find the annotation
                IType type = (IType) match.getElement();
                if (type.isInterface() && includeInterfaces || type.isClass() && includeClasses) {

                    IAnnotation ann = type.getAnnotation(WEB_WS_ANNOTATION);
                    if (!ann.exists())
                        ann = type.getAnnotation(SHORT_WEB_WS_ANNOTATION);

                    // Get the service name and store it
                    if (ann.exists()) {
                        String serviceName = null;
                        for (IMemberValuePair pair : ann.getMemberValuePairs()) {
                            if ("serviceName".equalsIgnoreCase(pair.getMemberName())) {
                                serviceName = (String) pair.getValue();
                                break;
                            }
                        }

                        if (serviceName == null)
                            serviceName = type.getElementName() + "Service";

                        classNameToServiceName.put(type.getFullyQualifiedName(), serviceName);
                    }
                }
            }
        }
    };

    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            SearchEngine.createJavaSearchScope(new IJavaElement[] { jp }, false), requestor, monitor);

    return classNameToServiceName;
}

From source file:com.google.gdt.eclipse.appengine.rpc.markers.quickfixes.CreateRequestFactoryMethodProposal.java

License:Open Source License

protected MethodDeclaration createMethodDeclaration(ASTRewrite rewriter) {

    StringBuffer buf = new StringBuffer();
    try {//www  .  jav a  2  s.c  o m
        IMethod method = (IMethod) serviceMethod.resolveBinding().getJavaElement();
        projectEntities = RequestFactoryUtils.findTypes(requestContextType.getJavaProject(), RpcType.ENTITY);
        proxies = RequestFactoryUtils.findTypes(method.getJavaProject(), RpcType.PROXY);
        entityList = RequestFactoryCodegenUtils.getEntitiesInMethodSignature(method, projectEntities);

        buf.append(RequestFactoryCodegenUtils.constructMethodSignature(method, projectEntities));

        List<String> entityName = new ArrayList<String>();
        for (IType entity : entityList) {
            entityName.add(entity.getFullyQualifiedName());
        }

        for (IType proxy : proxies) {
            IAnnotation annotation = proxy.getAnnotation("ProxyForName"); //$NON-NLS-N$
            IMemberValuePair[] values = annotation.getMemberValuePairs();
            for (IMemberValuePair pair : values) {
                if (pair.getMemberName().equals("value")) {
                    String typeName = (String) pair.getValue();
                    if (entityName.contains(typeName)) {
                        // entity has proxy, remove from list
                        removeFromEntityList(typeName);
                    }
                }
            }
        }
        // if any proxies were created, add those methods too
        for (IType entity : entityList) {
            String methodString = RequestFactoryCodegenUtils.constructRequestForEntity(entity.getElementName());
            buf.append(methodString);
        }
    } catch (JavaModelException e) {
        AppEngineRPCPlugin.log(e);
        return null;
    }

    MethodDeclaration methodDeclaration = (MethodDeclaration) rewriter.createStringPlaceholder(
            CodegenUtils.format(buf.toString(), CodeFormatter.K_CLASS_BODY_DECLARATIONS),
            ASTNode.METHOD_DECLARATION);
    return methodDeclaration;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.RequestFactoryUtils.java

License:Open Source License

public static IType getProxyForEntity(String entityName, IJavaProject project) throws JavaModelException {
    List<IType> proxies = RequestFactoryUtils.findTypes(project, RpcType.PROXY);
    for (IType type : proxies) {
        IAnnotation annotation = type.getAnnotation("ProxyForName"); //$NON-NLS-N$
        if (annotation.exists()) {
            IMemberValuePair[] values = annotation.getMemberValuePairs();
            if (values[0].getValue().equals(entityName)) {
                return type;
            }/*  w w w.j  a va  2  s .  c  o m*/
        }
    }

    return null;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.RequestFactoryUtils.java

License:Open Source License

public static IType getProxyForEntity(String entityName, List<IType> proxies) throws JavaModelException {
    for (IType type : proxies) {
        IAnnotation annotation = type.getAnnotation("ProxyForName"); //$NON-NLS-N$
        if (annotation.exists()) {
            IMemberValuePair[] values = annotation.getMemberValuePairs();
            if (values[0].getValue().equals(entityName)) {
                return type;
            }/*from   www. j a  v  a2  s .c  o  m*/
        }
    }

    return null;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.RequestFactoryUtils.java

License:Open Source License

public static IType getRequestForService(String serviceName, IJavaProject project) throws JavaModelException {

    List<IType> requests = RequestFactoryUtils.findTypes(project, RpcType.REQUEST);
    for (IType type : requests) {
        IAnnotation annotation = type.getAnnotation("ServiceName"); //$NON-NLS-N$
        if (annotation.exists()) {
            IMemberValuePair[] values = annotation.getMemberValuePairs();
            if (values[0].getValue().equals(serviceName)) {
                return type;
            }//from  w  w w  . j  a va  2s  . co  m
        }
    }

    return null;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.RequestFactoryUtils.java

License:Open Source License

public static IType getRequestForService(String serviceName, List<IType> requests) throws JavaModelException {
    if (requests == null) {
        return null;
    }/*from   w  w w . ja  v  a 2  s. com*/

    for (IType type : requests) {
        IAnnotation annotation = type.getAnnotation("ServiceName"); //$NON-NLS-N$
        if (annotation.exists()) {
            IMemberValuePair[] values = annotation.getMemberValuePairs();
            if (values[0].getValue().equals(serviceName)) {
                return type;
            }
        }
    }

    return null;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.RequestFactoryUtils.java

License:Open Source License

private static void determineType(IType type, List<IType> entities, List<IType> proxys,
        HashMap<String, IType> requests) throws JavaModelException {

    if (type.isInterface()) {
        List<String> interfaces = Arrays.asList(type.getSuperInterfaceNames());
        if (interfaces.contains("ValueProxy") //$NON-NLS-1$
                || interfaces.contains("EntityProxy")) { //$NON-NLS-2$
            proxys.add(type);/*from  www.ja  v  a2  s  . c  om*/
            return;
        }
        if (interfaces.contains("RequestContext")) { //$NON-NLS-N$
            IAnnotation annotation = type.getAnnotation("ServiceName"); //$NON-NLS-N$
            if (annotation.exists()) {
                IMemberValuePair[] values = annotation.getMemberValuePairs();
                requests.put((String) values[0].getValue(), type);
                return;
            }
        }
        return;
    }
    IAnnotation[] annotations = type.getAnnotations();
    for (IAnnotation annotation : annotations) {
        if (isEntityAnnotation(annotation)) {
            if (!isFrameworkClass(type)) {
                entities.add(type);
            }
        }
    }
}

From source file:com.tsc9526.monalisa.plugin.eclipse.generator.SourceUnit.java

License:Open Source License

private org.jboss.tools.ws.jaxrs.core.jdt.Annotation getAnnotation(IType type, String annotationClassName,
        CompilationUnit cu) {/*from   w w  w  .  j  a  v  a  2  s  . co m*/
    try {
        if (cu == null) {
            ICompilationUnit icu = org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.getCompilationUnit(type);
            cu = org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.parse(icu, null);
        }
        return org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils
                .resolveAnnotation(type.getAnnotation(annotationClassName), cu);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jboss.tools.cdi.core.CDIUtil.java

License:Open Source License

/**
 * Returns the set of interfaces annotated @Local for the session bean.
 * Returns an empty set if there is no such interfaces or if the bean class (or any supper class) annotated @LocalBean.   
 * //from w ww  .  j av  a  2  s .com
 * @param bean
 * @return
 */
public static Set<IType> getLocalInterfaces(ISessionBean bean) {
    Set<IType> sourceTypes = new HashSet<IType>();
    try {
        for (IParametedType type : bean.getLegalTypes()) {
            IType sourceType = type.getType();
            if (sourceType == null) {
                continue;
            }
            // Check if the class annotated @LocalBean
            IAnnotation[] annotations = sourceType.getAnnotations();
            for (IAnnotation annotation : annotations) {
                if (CDIConstants.LOCAL_BEAN_ANNOTATION_TYPE_NAME.equals(annotation.getElementName())
                        || "LocalBean".equals(annotation.getElementName())) {
                    return Collections.emptySet();
                }
            }
            if (sourceType.isInterface()) {
                IAnnotation annotation = sourceType.getAnnotation(CDIConstants.LOCAL_ANNOTATION_TYPE_NAME);
                if (!annotation.exists()) {
                    annotation = sourceType.getAnnotation("Local"); //$NON-NLS-N1
                }
                if (annotation.exists() && CDIConstants.LOCAL_ANNOTATION_TYPE_NAME
                        .equals(EclipseJavaUtil.resolveType(sourceType, "Local"))) { //$NON-NLS-N1
                    sourceTypes.add(sourceType);
                }
            }
        }
    } catch (JavaModelException e) {
        CDICorePlugin.getDefault().logError(e);
    }
    return sourceTypes;
}