Java Code Examples for javax.lang.model.util.Elements#getDocComment()

The following examples show how to use javax.lang.model.util.Elements#getDocComment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: T6358786.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
        String srcdir = System.getProperty("test.src");
        File file = new File(srcdir, args[0]);
        List<String> options = Arrays.asList(
            "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED"
        );
        JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, options, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
        Elements elements = task.getElements();
        for (Element clazz : task.enter(task.parse())) {
            String doc = elements.getDocComment(clazz);
            if (doc == null)
                throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
            System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
        }
    }
}
 
Example 2
Source File: T6358786.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 3
Source File: T6358786.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 4
Source File: T6358786.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 5
Source File: JavadocExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
/** Get the class javadoc from the given {@link TypeElement}. */
@Nullable
public static String getClassJavadoc(Elements elements, TypeElement typeElement) {
  final String unsanitizedJavadoc = elements.getDocComment(typeElement);

  if (unsanitizedJavadoc == null || unsanitizedJavadoc.isEmpty()) {
    return null;
  }

  final String javadoc = JAVADOC_SANITIZER.matcher(unsanitizedJavadoc).replaceAll("");
  final int firstPropJavadocIndex = javadoc.indexOf("@prop ");

  return firstPropJavadocIndex < 0 ? javadoc : javadoc.substring(0, firstPropJavadocIndex);
}
 
Example 6
Source File: JavadocExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
public static ImmutableList<PropJavadocModel> getPropJavadocs(
    Elements elements, TypeElement typeElement) {
  final String unsanitizedJavadoc = elements.getDocComment(typeElement);

  if (unsanitizedJavadoc == null || unsanitizedJavadoc.isEmpty()) {
    return ImmutableList.of();
  }

  final String javadoc = JAVADOC_SANITIZER.matcher(unsanitizedJavadoc).replaceAll("");

  final String[] propJavadocs = javadoc.split("@prop ");
  final List<PropJavadocModel> propJavadocModels = new ArrayList<>(propJavadocs.length);

  for (int i = 1, size = propJavadocs.length; i < size; i++) {
    final String propJavadoc = propJavadocs[i];
    // Each prop comment line look like:
    // @prop propName comment for the prop.
    final String[] propJavadocContents = propJavadoc.split(" ", 2);

    if (propJavadocContents.length == 2) {
      propJavadocModels.add(
          new PropJavadocModel(
              propJavadocContents[0], propJavadocContents[1].replace('\n', ' ')));
    }
  }

  return ImmutableList.copyOf(propJavadocModels);
}
 
Example 7
Source File: NullAwayNativeModels.java    From NullAway with MIT License 5 votes vote down vote up
static void elementStuff(Element e, Elements elems) {
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  e.getAnnotation(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  elems.getPackageElement(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  elems.getTypeElement(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  elems.getDocComment(null);
}
 
Example 8
Source File: T6358786.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 9
Source File: T6358786.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 10
Source File: T6358786.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 11
Source File: T6358786.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 12
Source File: ConfigElementFactory.java    From aircon with MIT License 4 votes vote down vote up
public static ConfigElement create(final ClassName providerClassName, Element configClass, VariableElement element, Elements elementsUtils, final Types types, final Map<String, ExecutableElement> defaultValueProviders, final Map<String, ExecutableElement> adapters, final Map<String, ExecutableElement> validators, final Map<String, ExecutableElement> mocks) {
	final ConfigFieldParser parser = new ConfigFieldParser(element, types);

	if (!parser.isConfigField()) {
		return null;
	}

	final String name = element.getSimpleName()
	                           .toString()
	                           .toUpperCase();
	final String key = (String) element.getConstantValue();
	final String javadoc = elementsUtils.getDocComment(element);

	final ConfigKind configKind = parser.getConfigKind();
	final TypeName rawType = parser.getRawType();
	final TypeName type = parser.getType();

	Object defaultValue = parser.getDefaultValue();

	final TypeMirror sourceTypeMirror = parser.getSourceTypeMirror(configClass);
	if (sourceTypeMirror == null) {
		return null;
	}

	final TypeMirror sourceIdentifierTypeMirror = parser.getSourceIdentifierTypeMirror(sourceTypeMirror, elementsUtils, types);
	final boolean mutable = parser.isMutable();

	final Number minValue = parser.getMinValue();
	final Number maxValue = parser.getMaxValue();
	final RangeFallbackPolicy minValueFallbackPolicy = parser.getMinValueFallbackPolicy();
	final RangeFallbackPolicy maxValueFallbackPolicy = parser.getMaxValueFallbackPolicy();

	final ConfigAuxMethod defaultValueProvider = ConfigAuxMethod.from(defaultValueProviders.get(key));
	final ConfigAuxMethod adapter = ConfigAuxMethod.from(adapters.get(key));
	final ConfigAuxMethod validator = ConfigAuxMethod.from(validators.get(key));
	final ConfigAuxMethod mock = ConfigAuxMethod.from(mocks.get(key));

	final ConfigElement.Properties properties = new ConfigElement.Properties(name, key, providerClassName, TypeName.get(configClass.asType()), javadoc, sourceTypeMirror, sourceIdentifierTypeMirror, type, rawType, defaultValue, parser.getDefaultValueConfig(), parser.getDefaultValueResId(), defaultValueProvider, mutable, adapter, validator, mock);

	switch (configKind) {
		case TIME:
			return new TimeConfigElement(properties, minValue, maxValue, minValueFallbackPolicy, maxValueFallbackPolicy, parser.getDefaultValueTimeUnit());
		case ENUM:
			return new EnumConfigElement(properties, parser.getEnumClass(), parser.getRandomizerValue());
		case JSON:
			return new JsonConfigElement(properties);
		case COLOR:
			return new ColorConfigElement(properties);
		case STRING:
			return new StringConfigElement(properties, parser.isEnforceNonEmpty());
		case URL:
			return new UrlConfigElement(properties, parser.isEnforceNonEmpty());
		case TEXT:
			return new TextConfigElement(properties, parser.isEnforceNonEmpty());
		case STRING_SET:
			return new StringSetConfigElement(properties);
		case NUMBER:
			return new NumberConfigElement(properties, minValue, maxValue, minValueFallbackPolicy, maxValueFallbackPolicy);
		case CUSTOM:
			return new CustomConfigElement(properties, parser.getAnnotationType());
		case PRIMITIVE:
		default:
			return new PrimitiveConfigElement(properties);
	}
}
 
Example 13
Source File: BundleAnnotationProcessor.java    From jlibs with Apache License 2.0 4 votes vote down vote up
public void generateProperties(BufferedWriter props) throws IOException{
    Elements elemUtil = Environment.get().getElementUtils();
    for(Map.Entry<Element, Map<String, ExecutableElement>> methods : classes.entrySet()){
        writeComments(props, "-------------------------------------------------[ "+methods.getKey().getSimpleName()+" ]---------------------------------------------------");
        props.newLine();

        for(Map.Entry<String, ExecutableElement> entry : methods.getValue().entrySet()){
            String key = entry.getKey();
            ExecutableElement method = entry.getValue();

            String doc = elemUtil.getDocComment(method);
            String methodDoc = ModelUtil.getMethodDoc(doc);
            if(!StringUtil.isEmpty(methodDoc))
                writeComments(props, " "+methodDoc);

            int i = 0;
            Map<String, String> paramDocs = ModelUtil.getMethodParamDocs(doc);
            for(VariableElement param : method.getParameters()){
                String paramName = param.getSimpleName().toString();
                String paramDoc = paramDocs.get(paramName);
                if(StringUtil.isEmpty(paramDoc))
                    writeComments(props, " {"+i+"} "+paramName);
                else
                    writeComments(props, " {"+i+"} "+paramName+" ==> "+paramDoc);
                i++;
            }

            AnnotationMirror messageMirror = ModelUtil.getAnnotationMirror(method, Message.class);
            String value = ModelUtil.getAnnotationValue(method, messageMirror, "value");

            try{
                new MessageFormat(value);
            }catch(IllegalArgumentException ex){
                throw new AnnotationError(method, messageMirror, ModelUtil.getRawAnnotationValue(method, messageMirror, "value"), "Invalid Message Format: "+ex.getMessage());
            }

            NavigableSet<Integer> args = findArgs(value);
            int argCount = args.size()==0 ? 0 : (args.last()+1);
            if(argCount!=method.getParameters().size())
                throw new AnnotationError(method, "no of args in message format doesn't match with the number of parameters this method accepts");
            for(i=0; i<argCount; i++){
                if(!args.remove(i))
                    throw new AnnotationError(method, messageMirror, "{"+i+"} is missing in message");
            }

            writeProperty(props, key, value);
            props.newLine();
        }
    }
}
 
Example 14
Source File: AnnotatedMixins.java    From Mixin with MIT License 4 votes vote down vote up
@Override
public String getJavadoc(Element element) {
    Elements elements = this.processingEnv.getElementUtils();
    return elements.getDocComment(element);
}