Example usage for javax.lang.model.element Element getClass

List of usage examples for javax.lang.model.element Element getClass

Introduction

In this page you can find the example usage for javax.lang.model.element Element getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:adalid.util.meta.MetaJavaCompiler.java

private static void scanAnalyzedElements(Iterable<? extends Element> elements, String tabs) {
    for (Element element : elements) {
        logger.info(tabs + "element" + CL + element.getKind() + SP + element.getSimpleName() + LP
                + element.getClass() + RP + element);
        logger.info(tabs + "enclosing element" + CL + element.getEnclosingElement());
        if (element instanceof ClassSymbol) {
            ClassSymbol classSymbol = (ClassSymbol) element;
        }/*  ww w  .  j  a  va  2 s  .c  om*/
        if (element instanceof MethodSymbol) {
            MethodSymbol methodSymbol = (MethodSymbol) element;
            logger.info(tabs + "method symbol" + CL + methodSymbol.getReturnType() + SP
                    + methodSymbol.getSimpleName() + LP + StringUtils.join(methodSymbol.getParameters(), CM)
                    + RP + StringUtils.join(methodSymbol.getThrownTypes(), CM));
        }
        if (element instanceof VarSymbol) {
            VarSymbol varSymbol = (VarSymbol) element;
        }
        List<? extends Element> enclosedElements = element.getEnclosedElements();
        if (enclosedElements != null && !enclosedElements.isEmpty()) {
            logger.info(tabs + "enclosed elements" + CL + StringUtils.join(enclosedElements, CM));
            scanAnalyzedElements(enclosedElements, tabs + "\t");
        }
        logger.info("");
    }
}

From source file:com.dspot.declex.action.Actions.java

private void createInformationForAction(String actionHolder, boolean isExternal) {

    TypeElement typeElement = env.getProcessingEnvironment().getElementUtils().getTypeElement(actionHolder);
    TypeElement generatedHolder = env.getProcessingEnvironment().getElementUtils()
            .getTypeElement(TypeUtils.getGeneratedClassName(typeElement, env));

    ActionFor actionForAnnotation = null;
    try {//from  w  w w  . j ava  2 s.  com
        actionForAnnotation = typeElement.getAnnotation(ActionFor.class);
    } catch (Exception e) {
        LOGGER.error("An error occurred processing the @ActionFor annotation", e);
    }

    if (actionForAnnotation != null) {

        for (String name : actionForAnnotation.value()) {

            ACTION_HOLDER_ELEMENT_FOR_ACTION.put("$" + name, typeElement);

            //Get model info
            final ActionInfo actionInfo = new ActionInfo(actionHolder);
            actionInfo.isGlobal = actionForAnnotation.global();
            actionInfo.isTimeConsuming = actionForAnnotation.timeConsuming();

            if (isExternal) {
                actionInfo.generated = false;
            }

            //This will work only for cached classes
            if (generatedHolder != null) {
                for (Element elem : generatedHolder.getEnclosedElements()) {
                    if (elem instanceof ExecutableElement) {
                        final String elemName = elem.getSimpleName().toString();
                        final List<? extends VariableElement> params = ((ExecutableElement) elem)
                                .getParameters();

                        if (elemName.equals("onViewChanged") && params.size() == 1 && params.get(0).asType()
                                .toString().equals(HasViews.class.getCanonicalName())) {
                            actionInfo.handleViewChanges = true;
                            break;
                        }
                    }
                }
            }

            addAction(name, actionHolder, actionInfo, false);

            String javaDoc = env.getProcessingEnvironment().getElementUtils().getDocComment(typeElement);
            actionInfo.setReferences(javaDoc);

            List<DeclaredType> processors = annotationHelper.extractAnnotationClassArrayParameter(typeElement,
                    ActionFor.class.getCanonicalName(), "processors");

            //Load processors
            if (processors != null) {
                for (DeclaredType processor : processors) {

                    Class<ActionProcessor> processorClass = null;

                    try {

                        ClassLoader loader = classLoaderForProcessor.get(processor.toString());
                        if (loader != null) {
                            processorClass = (Class<ActionProcessor>) Class.forName(processor.toString(), true,
                                    loader);
                        } else {
                            processorClass = (Class<ActionProcessor>) Class.forName(processor.toString());
                        }

                    } catch (ClassNotFoundException e) {

                        Element element = env.getProcessingEnvironment().getElementUtils()
                                .getTypeElement(processor.toString());
                        if (element == null) {
                            LOGGER.error("Processor \"" + processor.toString() + "\" couldn't be loaded",
                                    typeElement);
                        } else {

                            try {
                                //Get the file from which the class was loaded
                                java.lang.reflect.Field field = element.getClass().getField("classfile");
                                field.setAccessible(true);
                                JavaFileObject classfile = (JavaFileObject) field.get(element);

                                String jarUrl = classfile.toUri().toURL().toString();
                                jarUrl = jarUrl.substring(0, jarUrl.lastIndexOf('!') + 2);

                                //Create or use a previous created class loader for the given file
                                ClassLoader loader;
                                if (classLoaderForProcessor.containsKey(jarUrl)) {
                                    loader = classLoaderForProcessor.get(jarUrl);
                                } else {
                                    loader = new URLClassLoader(new URL[] { new URL(jarUrl) },
                                            Actions.class.getClassLoader());
                                    classLoaderForProcessor.put(processor.toString(), loader);
                                    classLoaderForProcessor.put(jarUrl, loader);
                                }

                                processorClass = (Class<ActionProcessor>) Class.forName(processor.toString(),
                                        true, loader);

                            } catch (Throwable e1) {
                                LOGGER.error("Processor \"" + processor.toString() + "\" couldn't be loaded: "
                                        + e1.getMessage(), typeElement);
                            }

                        }

                    } catch (ClassCastException e) {
                        LOGGER.error("Processor \"" + processor.toString() + "\" is not an Action Processor",
                                typeElement);
                    }

                    if (processorClass != null) {
                        try {
                            actionInfo.processors.add(processorClass.newInstance());
                        } catch (Throwable e) {
                            LOGGER.info("Processor \"" + processor.toString() + "\" couldn't be instantiated",
                                    typeElement);
                        }
                    }

                }
            }

            createInformationForMethods(typeElement, actionInfo);
        }

    }

}

From source file:pt.ist.vaadinframework.annotation.EmbeddedAnnotationProcessor.java

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

    final Set<String> actions = new HashSet<String>();

    final File file = new File(LOG_FILENAME);
    if (file.exists()) {
        try {/*from  w  w w  .  j ava  2s .co m*/
            final String contents = FileUtils.readFileToString(file);
            for (final String line : contents.split(ENTRY_SEPERATOR)) {
                actions.add(line);
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(EmbeddedComponent.class);
    for (final Element element : elements) {
        if (element instanceof TypeElement) {
            final TypeElement typeElement = (TypeElement) element;
            actions.add(typeElement.getQualifiedName().toString());
        } else {
            System.out.println("Skipping processing of element: " + element.getClass().getName()
                    + ", this type was not expected!");
        }
    }

    FileWriter fileWriter = null;
    try {
        fileWriter = new FileWriter(LOG_FILENAME, false);
        for (final String action : actions) {
            fileWriter.append(action);
            fileWriter.write(ENTRY_SEPERATOR);
        }
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }

    return true;
}