Java Code Examples for javax.lang.model.element.Modifier#PUBLIC

The following examples show how to use javax.lang.model.element.Modifier#PUBLIC . 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: MethodSpecGeneratorTest.java    From web3j with Apache License 2.0 7 votes vote down vote up
@Test
public void testGenerate() {
    List<ParameterSpec> parameterSpec =
            Collections.singletonList(
                    ParameterSpec.builder(Web3j.class, "web3j", Modifier.FINAL).build());
    String javaPoetStringFormat1 = "$T $L = $S";
    Object[] replacementValues1 = new Object[] {String.class, "hello ", "Hello how are you"};
    String javaPoetStringFormat2 = "$T $L = $T.build()";
    Object[] replacementValues2 = new Object[] {Web3j.class, "web3j", Web3j.class};
    Map<String, Object[]> statementBody = new LinkedHashMap<>();
    statementBody.put(javaPoetStringFormat1, replacementValues1);
    statementBody.put(javaPoetStringFormat2, replacementValues2);
    MethodSpecGenerator methodSpecGenerator =
            new MethodSpecGenerator(
                    "unitTest", Test.class, Modifier.PUBLIC, parameterSpec, statementBody);
    MethodSpec generatedMethodSpec = methodSpecGenerator.generate();
    assertEquals(
            "@org.junit.jupiter.api.Test\n"
                    + "public void unitTest(final org.web3j.protocol.Web3j web3j) throws java.lang.Exception {\n"
                    + "  java.lang.String hello  = \"Hello how are you\";\n"
                    + "  org.web3j.protocol.Web3j web3j = org.web3j.protocol.Web3j.build();\n"
                    + "}\n",
            generatedMethodSpec.toString());
}
 
Example 2
Source File: ClazzAs.java    From sundrio with Apache License 2.0 6 votes vote down vote up
public TypeDef apply(final TypeDef item) {
    final Modifier[] modifiers = item.isAbstract()
            ? new Modifier[]{Modifier.PUBLIC, Modifier.ABSTRACT}
            : new Modifier[]{Modifier.PUBLIC};

    final TypeDef editable = EDITABLE.apply(item);
    return new TypeDefBuilder(BUILDER.apply(item))
            .withAnnotations()
            .accept(new TypedVisitor<MethodBuilder>() {
        public void visit(MethodBuilder builder) {
            if (builder.getName() != null && builder.getName().equals("build")) {
                builder.withModifiers(TypeUtils.modifiersToInt(modifiers));
                builder.withReturnType(editable.toInternalReference());
                builder.withBlock(new Block(new Provider<List<Statement>>() {
                    @Override
                    public List<Statement> get() {
                        return toBuild(editable, editable);
                    }
                }));
            }
        }
    }).build();
}
 
Example 3
Source File: EntityGenerator.java    From requery with Apache License 2.0 6 votes vote down vote up
private Modifier[] generatedMemberModifiers(Modifier... modifiers) {
    Modifier visibility = null;
    switch (entity.propertyVisibility()) {
        case PUBLIC:
            visibility = Modifier.PUBLIC;
            break;
        case PRIVATE:
            if (entity.isEmbedded()) {
                visibility = Modifier.PROTECTED;
            } else {
                visibility = Modifier.PRIVATE;
            }
            break;
        case PACKAGE:
            break;
    }
    ArrayList<Modifier> list = new ArrayList<>();
    if (visibility != null) {
        list.add(visibility);
    }
    Collections.addAll(list, modifiers);
    return list.toArray(new Modifier[list.size()]);
}
 
Example 4
Source File: InternalClassDiscoveryIntegrationTest.java    From Akatsuki with Apache License 2.0 5 votes vote down vote up
@DataPoint
public static APITestScenario createSimpleSource() {
	TestSource source = new TestSource("test", generateClassName(), Modifier.PUBLIC);
	source.appendFields(new RetainedTestField(String.class).createFieldSpec(),
			new ArgTestField(Integer.class).createFieldSpec())
			.appendTransformation((b, s) -> b.superclass(MockedFragment.class));

	return base -> new APITestBase(base, source);
}
 
Example 5
Source File: VisibilityChanged.java    From revapi with Apache License 2.0 5 votes vote down vote up
private Modifier getVisibility(Element t) {
    for (Modifier m : t.getModifiers()) {
        if (m == Modifier.PUBLIC || m == Modifier.PROTECTED || m == Modifier.PRIVATE) {
            return m;
        }
    }

    return null;
}
 
Example 6
Source File: NodeFactoryGenerator.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private void addConstants() {
  Modifier[] modifiers = {Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL};
  nodeFactory.addField(FieldSpec.builder(Object.class, RETIRED_STRONG_KEY, modifiers)
      .initializer("new Object()")
      .build());
  nodeFactory.addField(FieldSpec.builder(Object.class, DEAD_STRONG_KEY, modifiers)
      .initializer("new Object()")
      .build());
  nodeFactory.addField(FieldSpec.builder(rawReferenceKeyType, RETIRED_WEAK_KEY, modifiers)
      .initializer("new $T(null, null)", rawReferenceKeyType)
      .build());
  nodeFactory.addField(FieldSpec.builder(rawReferenceKeyType, DEAD_WEAK_KEY, modifiers)
      .initializer("new $T(null, null)", rawReferenceKeyType)
      .build());
}
 
Example 7
Source File: ModifierOrderer.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link javax.lang.model.element.Modifier} for the given token kind, or {@code
 * null}.
 */
private static Modifier getModifier(TokenKind kind) {
  if (kind == null) {
    return null;
  }
  switch (kind) {
    case PUBLIC:
      return Modifier.PUBLIC;
    case PROTECTED:
      return Modifier.PROTECTED;
    case PRIVATE:
      return Modifier.PRIVATE;
    case ABSTRACT:
      return Modifier.ABSTRACT;
    case STATIC:
      return Modifier.STATIC;
    case DEFAULT:
      return Modifier.DEFAULT;
    case FINAL:
      return Modifier.FINAL;
    case TRANSIENT:
      return Modifier.TRANSIENT;
    case VOLATILE:
      return Modifier.VOLATILE;
    case SYNCHRONIZED:
      return Modifier.SYNCHRONIZED;
    case NATIVE:
      return Modifier.NATIVE;
    case STRICTFP:
      return Modifier.STRICTFP;
    default:
      return null;
  }
}
 
Example 8
Source File: InternalClassDiscoveryIntegrationTest.java    From Akatsuki with Apache License 2.0 5 votes vote down vote up
@DataPoint
public static APITestScenario createSourceWithInnerClass() {
	TestSource enclosingClass = new TestSource("test", generateClassName(), Modifier.PUBLIC);
	TestSource source = new TestSource(null, generateClassName(), Modifier.PUBLIC);
	source.appendFields(new RetainedTestField(String.class).createFieldSpec(),
			new ArgTestField(Integer.class).createFieldSpec())
			.appendTransformation((b, s) -> b.superclass(MockedFragment.class));
	enclosingClass.innerClasses(true, source);
	return base -> new APITestBase(base, source, enclosingClass, new TestSource[] {});
}
 
Example 9
Source File: ProcessingUtils.java    From aircon with MIT License 5 votes vote down vote up
public static Modifier getVisibilityModifier(Element element) {
	switch (Visibility.ofElement(element)) {
		case PRIVATE:
			return Modifier.PRIVATE;
		case PROTECTED:
			return Modifier.PROTECTED;
		case PUBLIC:
			return Modifier.PUBLIC;
	}

	return null;
}
 
Example 10
Source File: FunSpecGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
public FunSpecGenerator(
        String testMethodName,
        Map<String, Object[]> statementBody,
        List<ParameterSpec> testMethodParameters) {
    this.statementBody = statementBody;
    this.testMethodName = testMethodName;
    this.testMethodAnnotation = Test.class;
    this.testMethodModifier = Modifier.PUBLIC;
    this.testMethodParameters = testMethodParameters;
}
 
Example 11
Source File: FunSpecGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
public FunSpecGenerator(String testMethodName, Map<String, Object[]> statementBody) {
    this.statementBody = statementBody;
    this.testMethodName = testMethodName;
    this.testMethodAnnotation = Test.class;
    this.testMethodModifier = Modifier.PUBLIC;
    this.testMethodParameters = Collections.emptyList();
}
 
Example 12
Source File: MethodSpecGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
public MethodSpecGenerator(
        String testMethodName,
        Map<String, Object[]> statementBody,
        List<ParameterSpec> testMethodParameters) {
    this.statementBody = statementBody;
    this.testMethodName = testMethodName;
    this.testMethodAnnotation = Test.class;
    this.testMethodModifier = Modifier.PUBLIC;
    this.testMethodParameters = testMethodParameters;
}
 
Example 13
Source File: MethodSpecGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
public MethodSpecGenerator(String testMethodName, Map<String, Object[]> statementBody) {
    this.statementBody = statementBody;
    this.testMethodName = testMethodName;
    this.testMethodAnnotation = Test.class;
    this.testMethodModifier = Modifier.PUBLIC;
    this.testMethodParameters = Collections.emptyList();
}
 
Example 14
Source File: ModifierOrderer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
     * Returns the {@link Modifier} for the given token kind, or {@code
     * null}.
     */
    private static Modifier getModifier(TokenKind kind) {
        if (kind == null) {
            return null;
        }
        switch (kind) {
            case PUBLIC:
                return Modifier.PUBLIC;
            case PROTECTED:
                return Modifier.PROTECTED;
            case PRIVATE:
                return Modifier.PRIVATE;
            case ABSTRACT:
                return Modifier.ABSTRACT;
            case STATIC:
                return Modifier.STATIC;
            // TODO: 22-Jul-17 unsupported lambdas expr
//            case DEFAULT:
//                return Modifier.DEFAULT;
            case FINAL:
                return Modifier.FINAL;
            case TRANSIENT:
                return Modifier.TRANSIENT;
            case VOLATILE:
                return Modifier.VOLATILE;
            case SYNCHRONIZED:
                return Modifier.SYNCHRONIZED;
            case NATIVE:
                return Modifier.NATIVE;
            case STRICTFP:
                return Modifier.STRICTFP;
            default:
                return null;
        }
    }
 
Example 15
Source File: PsiModifierExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
private static Modifier psiModifierToModifier(PsiElement psiModifier) {
  switch (psiModifier.getText()) {
    case PsiModifier.ABSTRACT:
      return Modifier.ABSTRACT;
    case PsiModifier.FINAL:
      return Modifier.FINAL;
    case PsiModifier.NATIVE:
      return Modifier.NATIVE;
    case PsiModifier.PRIVATE:
      return Modifier.PRIVATE;
    case PsiModifier.PROTECTED:
      return Modifier.PROTECTED;
    case PsiModifier.PUBLIC:
      return Modifier.PUBLIC;
    case PsiModifier.STATIC:
      return Modifier.STATIC;
    case PsiModifier.STRICTFP:
      return Modifier.STRICTFP;
    case PsiModifier.SYNCHRONIZED:
      return Modifier.SYNCHRONIZED;
    case PsiModifier.TRANSIENT:
      return Modifier.TRANSIENT;
    case PsiModifier.VOLATILE:
      return Modifier.VOLATILE;
    default:
      // TODO better error message, ideally w/ line number
      throw new ComponentsProcessingException(
          "Unexpected Modifier, modifier is: " + psiModifier.getText());
  }
}
 
Example 16
Source File: ModifierOrderer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
     * Returns the {@link Modifier} for the given token kind, or {@code
     * null}.
     */
    private static Modifier getModifier(TokenKind kind) {
        if (kind == null) {
            return null;
        }
        switch (kind) {
            case PUBLIC:
                return Modifier.PUBLIC;
            case PROTECTED:
                return Modifier.PROTECTED;
            case PRIVATE:
                return Modifier.PRIVATE;
            case ABSTRACT:
                return Modifier.ABSTRACT;
            case STATIC:
                return Modifier.STATIC;
            // TODO: 22-Jul-17 unsupported lambdas expr
//            case DEFAULT:
//                return Modifier.DEFAULT;
            case FINAL:
                return Modifier.FINAL;
            case TRANSIENT:
                return Modifier.TRANSIENT;
            case VOLATILE:
                return Modifier.VOLATILE;
            case SYNCHRONIZED:
                return Modifier.SYNCHRONIZED;
            case NATIVE:
                return Modifier.NATIVE;
            case STRICTFP:
                return Modifier.STRICTFP;
            default:
                return null;
        }
    }
 
Example 17
Source File: PhysicalJavaFileObject.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Modifier getAccessLevel() {
	return Modifier.PUBLIC;
}
 
Example 18
Source File: ClazzAs.java    From sundrio with Apache License 2.0 4 votes vote down vote up
public TypeDef apply(TypeDef item) {
    Modifier[] modifiers = item.isAbstract()
            ? new Modifier[]{Modifier.PUBLIC, Modifier.ABSTRACT}
            : new Modifier[]{Modifier.PUBLIC};

    TypeDef editableType = TypeAs.EDITABLE.apply(item);
    final TypeDef builderType = TypeAs.BUILDER.apply(item);

    List<Method> constructors = new ArrayList<Method>();
    List<Method> methods = new ArrayList<Method>();

    for (Method constructor : item.getConstructors()) {
        constructors.add(superConstructorOf(constructor, editableType));
    }

    Method edit = new MethodBuilder()
            .withModifiers(TypeUtils.modifiersToInt(modifiers))
            .withReturnType(builderType.toInternalReference())
            .withName("edit")
            .withNewBlock()
                .addToStatements(new StringStatement(new Provider<String>() {
                    @Override
                    public String get() {
                        return "return new " + builderType.getName() + "(this);";
                    }
                }))
            .endBlock()
            .build();

    methods.add(edit);

    //We need to treat the editable classes as buildables themselves.
    return CodegenContext.getContext().getDefinitionRepository().register(
            BuilderContextManager.getContext().getBuildableRepository().register(new TypeDefBuilder(editableType)
                    .withAnnotations()
                    .withModifiers(TypeUtils.modifiersToInt(modifiers))
                    .withConstructors(constructors)
                    .withMethods(methods)
                    .addToAttributes(BUILDABLE_ENABLED, true)
                    .addToAttributes(GENERATED, true) // We want to know that its a generated type...
                    .build())
    );
}
 
Example 19
Source File: LocalCacheContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public Modifier[] publicFinalModifiers() {
  return isFinal
      ? new Modifier[] { Modifier.PUBLIC }
      : new Modifier[] { Modifier.PUBLIC, Modifier.FINAL };
}
 
Example 20
Source File: NodeContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public Modifier[] publicFinalModifiers() {
  return isFinal
      ? new Modifier[] { Modifier.PUBLIC }
      : new Modifier[] { Modifier.PUBLIC, Modifier.FINAL };
}